-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/telegram_feeder
- Loading branch information
Showing
11 changed files
with
361 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package filters | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/Matrix86/driplane/data" | ||
"github.com/evilsocket/islazy/log" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
// RateLimit is a Filter to create a RateLimit number | ||
type RateLimit struct { | ||
Base | ||
|
||
objects int64 | ||
seconds int64 | ||
limiter *rate.Limiter | ||
context context.Context | ||
cancelContext context.CancelFunc | ||
|
||
params map[string]string | ||
} | ||
|
||
// NewRateLimitFilter is the registered method to instantiate a RateLimit Filter | ||
func NewRateLimitFilter(p map[string]string) (Filter, error) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
f := &RateLimit{ | ||
params: p, | ||
seconds: 1, | ||
context: ctx, | ||
cancelContext: cancel, | ||
} | ||
f.cbFilter = f.DoFilter | ||
|
||
if v, ok := p["rate"]; ok { | ||
i, err := strconv.ParseInt(v, 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.objects = i | ||
} | ||
|
||
f.limiter = rate.NewLimiter(rate.Limit(f.objects), int(f.seconds)) | ||
|
||
return f, nil | ||
} | ||
|
||
// DoFilter is the mandatory method used to "filter" the input data.Message | ||
func (f *RateLimit) DoFilter(msg *data.Message) (bool, error) { | ||
if f.objects > 0 { | ||
if err := f.limiter.Wait(f.context); err != nil { | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// OnEvent is called when an event occurs | ||
func (f *RateLimit) OnEvent(event *data.Event) { | ||
if event.Type == "shutdown" { | ||
log.Debug("shutdown event received") | ||
f.cancelContext() | ||
} | ||
} | ||
|
||
// Set the name of the filter | ||
func init() { | ||
register("ratelimit", NewRateLimitFilter) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package filters | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Matrix86/driplane/data" | ||
) | ||
|
||
func TestNewRateLimitFilter(t *testing.T) { | ||
filter, err := NewRateLimitFilter(map[string]string{"rate": "2", "min": "10", "max": "40"}) | ||
if err != nil { | ||
t.Errorf("constructor returned '%s'", err) | ||
} | ||
if e, ok := filter.(*RateLimit); ok { | ||
if e.objects != 2 { | ||
t.Errorf("'output' parameter ignored") | ||
} | ||
} else { | ||
t.Errorf("cannot cast to proper Filter...") | ||
} | ||
|
||
_, err = NewRateLimitFilter(map[string]string{"rate": "random text", "min": "x", "max": "40"}) | ||
if err == nil { | ||
t.Errorf("constructor should return error") | ||
} | ||
} | ||
|
||
func TestRateLimit_DoFilter(t *testing.T) { | ||
filter, err := NewRateLimitFilter(map[string]string{"rate": "5"}) | ||
if err != nil { | ||
t.Errorf("constructor returned '%s'", err) | ||
} | ||
|
||
if e, ok := filter.(*RateLimit); ok { | ||
msg := "this is a test..." | ||
m := data.NewMessage(msg) | ||
_, err := e.DoFilter(m) | ||
if err != nil { | ||
t.Errorf("DoFilter returned an error '%s'", err) | ||
} | ||
if m.GetMessage() != msg { | ||
t.Errorf("DoFilter changed the message") | ||
} | ||
|
||
filter.OnEvent(&data.Event{ | ||
Type: "shutdown", | ||
Content: "shutdown", | ||
}) | ||
v, err := e.DoFilter(m) | ||
if err != nil { | ||
t.Errorf("DoFilter returned an error '%s'", err) | ||
} | ||
if v { | ||
t.Errorf("DoFilter has to return false after the shutdown") | ||
} | ||
|
||
} else { | ||
t.Errorf("cannot cast to proper Filter...") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.