Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate-limiting to bastion feeder #284

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/feedbastion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/transparency-dev/witness/internal/config"
"github.com/transparency-dev/witness/omniwitness"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate"
"gopkg.in/yaml.v3"
"k8s.io/klog/v2"
)
Expand All @@ -40,6 +41,7 @@ var (
bastionURL = flag.String("bastion_url", "https://localhost:8443", "URL of the bastion service")
feed = flag.String("feed", ".*", "RegEx matching log origins to feed to bastion")
loopInterval = flag.Duration("loop_interval", 0, "If set to > 0, runs in looping mode sleeping this duration between feed attempts")
rateLimit = flag.Float64("max_qps", 2, "Defines maximum number of requests/s to send via bastion")
)

type logFeeder struct {
Expand All @@ -53,6 +55,7 @@ func main() {
defer klog.Flush()

ctx := context.Background()
rl := rate.NewLimiter(rate.Limit(*rateLimit), 1)

httpClient := &http.Client{}
insecureHttpClient := &http.Client{Transport: &http.Transport{
Expand Down Expand Up @@ -89,6 +92,9 @@ func main() {
lf := lf
if r.Match([]byte(o)) {
eg.Go(func() error {
if err := rl.Wait(ctx); err != nil {
return err
}
return lf.info.Feeder.FeedFunc()(ctx, lf.cfg, bc, httpClient, *loopInterval)
})
}
Expand Down
Loading