-
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.
- Loading branch information
Showing
6 changed files
with
109 additions
and
7 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
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
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,30 @@ | ||
--- | ||
title: "RateLimit" | ||
date: 2023-01-31T18:50:23+02:00 | ||
draft: false | ||
--- | ||
|
||
## RateLimit | ||
|
||
This filter allows you to set a rate limit on the messages that can go through it. So for example if we don't want to limit the number of messages in a pipe to 5 messages per second we just to set the parameter `rate` to 5. | ||
|
||
### Parameters | ||
|
||
| Parameter | Type | Default | Description | | ||
|--------------|----------|---------|--------------------------------------------------------------------------------------------------| | ||
| **rate** | _STRING_ | "0" | how many event per second you want to have as rate limiter | | ||
|
||
|
||
{{< notice info "Example" >}} | ||
`... | ratelimit(rate="5") | ...` | ||
{{< /notice >}} | ||
|
||
### Output | ||
|
||
The filter will slow down the rate of the messages as specified on the parameter `rate`. | ||
|
||
### Examples | ||
|
||
{{< alert theme="warning" >}} | ||
Soon... | ||
{{< /alert >}} |