Add event stream module

This commit is contained in:
9seconds
2021-03-15 21:34:53 +03:00
parent abff0cf211
commit b08d945d7c
13 changed files with 225 additions and 15 deletions
+23 -10
View File
@@ -29,14 +29,20 @@ const (
var fireholRegexpComment = regexp.MustCompile(`\s*#.*?$`)
type Firehol struct {
logger mtglib.Logger
rwMutex sync.RWMutex
ctx context.Context
ctxCancel context.CancelFunc
logger mtglib.Logger
rwMutex sync.RWMutex
remoteURLs []string
localFiles []string
httpClient *http.Client
workerPool *ants.Pool
treeV4 *bool_tree.TreeV4
treeV6 *bool_tree.TreeV6
treeV4 *bool_tree.TreeV4
treeV6 *bool_tree.TreeV6
}
func (f *Firehol) Contains(ip net.IP) bool {
@@ -76,7 +82,7 @@ func (f *Firehol) containsIPv6(addr net.IP) bool {
return false
}
func (f *Firehol) Run(ctx context.Context, updateEach time.Duration) {
func (f *Firehol) Run(updateEach time.Duration) {
ticker := time.NewTicker(updateEach)
defer func() {
@@ -88,24 +94,28 @@ func (f *Firehol) Run(ctx context.Context, updateEach time.Duration) {
}
}()
if err := f.update(ctx); err != nil {
if err := f.update(); err != nil {
f.logger.WarningError("cannot update blocklist", err)
}
for {
select {
case <-ctx.Done():
case <-f.ctx.Done():
return
case <-ticker.C:
if err := f.update(ctx); err != nil {
if err := f.update(); err != nil {
f.logger.WarningError("cannot update blocklist", err)
}
}
}
}
func (f *Firehol) update(ctx context.Context) error { // nolint: funlen, cyclop
ctx, cancel := context.WithCancel(ctx)
func (f *Firehol) Shutdown() {
f.ctxCancel()
}
func (f *Firehol) update() error { // nolint: funlen, cyclop
ctx, cancel := context.WithCancel(f.ctx)
defer cancel()
wg := &sync.WaitGroup{}
@@ -314,8 +324,11 @@ func NewFirehol(logger mtglib.Logger, network mtglib.Network,
}
workerPool, _ := ants.NewPool(int(downloadConcurrency))
ctx, cancel := context.WithCancel(context.Background())
return &Firehol{
ctx: ctx,
ctxCancel: cancel,
logger: logger.Named("firehol"),
httpClient: network.MakeHTTPClient(nil),
treeV4: bool_tree.NewTreeV4(),
+16 -5
View File
@@ -1,7 +1,6 @@
package ipblocklist_test
import (
"context"
"io"
"net"
"net/http"
@@ -72,12 +71,15 @@ func (suite *FireholTestSuite) TestLocalFail() {
suite.NoError(err)
go blocklist.Run(context.Background(), time.Hour)
go blocklist.Run(time.Hour)
time.Sleep(500 * time.Millisecond)
suite.False(blocklist.Contains(net.ParseIP("10.0.0.10")))
suite.False(blocklist.Contains(net.ParseIP("127.0.0.1")))
blocklist.Shutdown()
time.Sleep(500 * time.Millisecond)
}
func (suite *FireholTestSuite) TestLocalOk() {
@@ -87,12 +89,15 @@ func (suite *FireholTestSuite) TestLocalOk() {
suite.NoError(err)
go blocklist.Run(context.Background(), time.Hour)
go blocklist.Run(time.Hour)
time.Sleep(500 * time.Millisecond)
suite.True(blocklist.Contains(net.ParseIP("10.0.0.10")))
suite.False(blocklist.Contains(net.ParseIP("127.0.0.1")))
blocklist.Shutdown()
time.Sleep(500 * time.Millisecond)
}
func (suite *FireholTestSuite) TestRemoteFail() {
@@ -102,11 +107,14 @@ func (suite *FireholTestSuite) TestRemoteFail() {
suite.NoError(err)
go blocklist.Run(context.Background(), time.Hour)
go blocklist.Run(time.Hour)
time.Sleep(500 * time.Millisecond)
suite.False(blocklist.Contains(net.ParseIP("10.2.2.2")))
blocklist.Shutdown()
time.Sleep(500 * time.Millisecond)
}
func (suite *FireholTestSuite) TestMixed() {
@@ -123,12 +131,15 @@ func (suite *FireholTestSuite) TestMixed() {
suite.NoError(err)
go blocklist.Run(context.Background(), time.Hour)
go blocklist.Run(time.Hour)
time.Sleep(500 * time.Millisecond)
suite.True(blocklist.Contains(net.ParseIP("10.2.2.2")))
suite.True(blocklist.Contains(net.ParseIP("10.1.0.100")))
blocklist.Shutdown()
time.Sleep(500 * time.Millisecond)
}
func TestFirehol(t *testing.T) {
+1
View File
@@ -9,6 +9,7 @@ import (
type noop struct{}
func (n noop) Contains(ip net.IP) bool { return false }
func (n noop) Shutdown() {}
func NewNoop() mtglib.IPBlocklist {
return noop{}