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

Use ratelimiter config in webapi plugins #5190

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion flyteplugins/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
golang.org/x/net v0.22.0
golang.org/x/oauth2 v0.16.0
golang.org/x/time v0.5.0
google.golang.org/api v0.155.0
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
Expand Down Expand Up @@ -128,7 +129,6 @@ require (
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package webapi

import (
"context"
"time"

"golang.org/x/time/rate"
"k8s.io/client-go/util/workqueue"

"github.com/flyteorg/flyte/flyteplugins/go/tasks/errors"
Expand Down Expand Up @@ -161,6 +163,7 @@ func ToPluginPhase(s core.Phase) (Phase, error) {
}

func NewResourceCache(ctx context.Context, name string, client Client, cfg webapi.CachingConfig,
rateCfg webapi.RateLimiterConfig,
scope promutils.Scope) (ResourceCache, error) {

q := ResourceCache{
Expand All @@ -169,7 +172,10 @@ func NewResourceCache(ctx context.Context, name string, client Client, cfg webap
}

autoRefreshCache, err := cache.NewAutoRefreshCache(name, q.SyncResource,
workqueue.DefaultControllerRateLimiter(), cfg.ResyncInterval.Duration, cfg.Workers, cfg.Size,
workqueue.NewMaxOfRateLimiter(
workqueue.NewItemExponentialFailureRateLimiter(5*time.Millisecond, 1000*time.Second),
&workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(rateCfg.QPS), rateCfg.Burst)},
), cfg.ResyncInterval.Duration, cfg.Workers, cfg.Size,
scope.NewSubScope("cache"))

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ func TestNewResourceCache(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
c, err := NewResourceCache(context.Background(), "Cache1", &mocks.Client{}, webapi.CachingConfig{
Size: 10,
}, promutils.NewTestScope())
}, webapi.RateLimiterConfig{QPS: 1, Burst: 1}, promutils.NewTestScope())
assert.NoError(t, err)
assert.NotNil(t, c)
})

t.Run("Error", func(t *testing.T) {
_, err := NewResourceCache(context.Background(), "Cache1", &mocks.Client{}, webapi.CachingConfig{},
webapi.RateLimiterConfig{},
promutils.NewTestScope())
assert.Error(t, err)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
}

resourceCache, err := NewResourceCache(ctx, pluginEntry.ID, p, p.GetConfig().Caching,
iCtx.MetricsScope().NewSubScope("cache"))
p.GetConfig().ReadRateLimiter, iCtx.MetricsScope().NewSubScope("cache"))

Check warning on line 194 in flyteplugins/go/tasks/pluginmachinery/internal/webapi/core.go

View check run for this annotation

Codecov / codecov/patch

flyteplugins/go/tasks/pluginmachinery/internal/webapi/core.go#L194

Added line #L194 was not covered by tests

if err != nil {
return nil, err
Expand Down
Loading