Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import (
"github.com/kubevela/workflow/pkg/features"
"github.com/kubevela/workflow/pkg/monitor/watcher"
"github.com/kubevela/workflow/pkg/providers"
httpProvider "github.com/kubevela/workflow/pkg/providers/http"
legacyhttpProvider "github.com/kubevela/workflow/pkg/providers/legacy/http"
"github.com/kubevela/workflow/pkg/types"
"github.com/kubevela/workflow/pkg/utils"
"github.com/kubevela/workflow/pkg/utils/httpguard"
Expand Down Expand Up @@ -150,6 +152,9 @@ func main() {
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
flag.Parse()
wfupgrade.InitCompatibilityCache(context.Background(), wfupgrade.CompatibilityCacheSize)
rootCtx := ctrl.SetupSignalHandler()
httpProvider.InitRateLimiter(rootCtx)
Comment thread
anoop2811 marked this conversation as resolved.
Resolved
legacyhttpProvider.InitRateLimiter(rootCtx)
if logDebug {
_ = flag.Set("v", strconv.Itoa(int(common.LogDebug)))
}
Expand Down Expand Up @@ -324,7 +329,7 @@ func main() {
watcher.StartWorkflowRunMetricsWatcher(informer)

klog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
if err := mgr.Start(rootCtx); err != nil {
klog.Error(err, "problem running manager")
os.Exit(1)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/google/go-cmp v0.7.0
github.com/hashicorp/go-version v1.6.0
github.com/kubevela/kube-trigger v0.1.1-0.20250711201929-51c837aa9bd2
github.com/kubevela/pkg v1.11.1-0.20260722232534-455842713731
github.com/kubevela/pkg v1.11.1-0.20260826024906-a4214d8d3c39
github.com/onsi/ginkgo/v2 v2.23.3
github.com/onsi/gomega v1.36.2
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -79,6 +79,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jellydator/ttlcache/v3 v3.0.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY=
github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
Expand Down Expand Up @@ -320,8 +322,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubevela/kube-trigger v0.1.1-0.20250711201929-51c837aa9bd2 h1:3FvzYw6qJiAu7OLbOK7PuhDLowtKs3Ugw1gQ2E8jz/Y=
github.com/kubevela/kube-trigger v0.1.1-0.20250711201929-51c837aa9bd2/go.mod h1:lGeLpXHVxYPulYtVgGatFHFolkmzSmrdyhIQ7BHHJHs=
github.com/kubevela/pkg v1.11.1-0.20260722232534-455842713731 h1:P9sSZ2YJtyDzDcu28uJ3d0HliiUDjl2VnEZY5Czmo6I=
github.com/kubevela/pkg v1.11.1-0.20260722232534-455842713731/go.mod h1:EmM4VIyU7KxDmPBq9hG4GpSZbGwiM76/W/8paLBk8wY=
github.com/kubevela/pkg v1.11.1-0.20260826024906-a4214d8d3c39 h1:GRG4cDE3ALeIBeyzTQwZKeuGXvUjCd6ulWK5W3C6r60=
github.com/kubevela/pkg v1.11.1-0.20260826024906-a4214d8d3c39/go.mod h1:MZsc/xbV4rZjE2fMWX8+JovRK/zVUb1HfefGNPYrgq8=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
Expand Down
19 changes: 14 additions & 5 deletions pkg/providers/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
"net/http"
neturl "net/url"
"strings"
"sync"
"time"

"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -49,13 +51,10 @@ const (
)

var (
rateLimiter *ratelimiter.RateLimiter
rateLimiterOnce sync.Once
rateLimiter *ratelimiter.RateLimiter
)

func init() {
rateLimiter = ratelimiter.NewRateLimiter(128)
}

// Request .
type Request struct {
Timeout string `json:"timeout,omitempty"`
Expand Down Expand Up @@ -120,10 +119,20 @@ func requestPolicy() httpguard.Policy {
return policy
}

// InitRateLimiter initializes the rate limiter. Must be called once at startup.
func InitRateLimiter(ctx context.Context) {
rateLimiterOnce.Do(func() {
var err error
rateLimiter, err = ratelimiter.NewRateLimiter(ctx, 128)
utilruntime.Must(err)
})
}

func runHTTP(ctx context.Context, params *DoParams) (*DoReturns, error) {
if utilfeature.DefaultMutableFeatureGate.Enabled(features.DisableWorkflowHTTP) {
Comment thread
anoop2811 marked this conversation as resolved.
Resolved
return nil, errors.New("workflow outbound HTTP is disabled by DisableWorkflowHTTP feature gate")
}
InitRateLimiter(context.WithoutCancel(ctx))
var (
err error
header, trailer http.Header
Expand Down
4 changes: 3 additions & 1 deletion pkg/providers/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func TestHttpDo(t *testing.T) {
}

// test ratelimiter
rateLimiter = ratelimiter.NewRateLimiter(1)
var err error
rateLimiter, err = ratelimiter.NewRateLimiter(ctx, 1)
require.NoError(t, err)
limiterTestCases := []struct {
request RequestVars
expectedErr string
Expand Down
19 changes: 14 additions & 5 deletions pkg/providers/legacy/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"

"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime"
Expand All @@ -45,13 +47,10 @@ const (
)

var (
rateLimiter *ratelimiter.RateLimiter
rateLimiterOnce sync.Once
rateLimiter *ratelimiter.RateLimiter
)

func init() {
rateLimiter = ratelimiter.NewRateLimiter(128)
}

// Request .
type Request struct {
Timeout string `json:"timeout,omitempty"`
Expand Down Expand Up @@ -97,7 +96,17 @@ func Do(ctx context.Context, params *DoParams) (*ResponseVars, error) {
return runHTTP(ctx, params)
}

// InitRateLimiter initializes the rate limiter. Must be called once at startup.
func InitRateLimiter(ctx context.Context) {
rateLimiterOnce.Do(func() {
var err error
rateLimiter, err = ratelimiter.NewRateLimiter(ctx, 128)
utilruntime.Must(err)
})
}

func runHTTP(ctx context.Context, params *DoParams) (*ResponseVars, error) {
InitRateLimiter(context.WithoutCancel(ctx))
var (
err error
header, trailer http.Header
Expand Down
4 changes: 3 additions & 1 deletion pkg/providers/legacy/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ func TestHttpDo(t *testing.T) {
}

// test ratelimiter
rateLimiter = ratelimiter.NewRateLimiter(1)
var err error
rateLimiter, err = ratelimiter.NewRateLimiter(ctx, 1)
require.NoError(t, err)
limiterTestCases := []struct {
request RequestVars
expectedErr string
Expand Down
25 changes: 16 additions & 9 deletions pkg/providers/legacy/http/ratelimiter/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,40 @@ limitations under the License.
package ratelimiter

import (
"context"
"time"

"github.com/golang/groupcache/lru"
"github.com/kubevela/pkg/cache"
"golang.org/x/time/rate"
)

// RateLimiter is the rate limiter.
type RateLimiter struct {
store *lru.Cache
store *cache.LRUStore[string, *rate.Limiter]
}

// NewRateLimiter returns a new rate limiter.
func NewRateLimiter(length int) *RateLimiter {
store := lru.New(length)
store.Clear()
return &RateLimiter{store: store}
func NewRateLimiter(ctx context.Context, length int) (*RateLimiter, error) {
store, err := cache.NewLRUStore(ctx, cache.Options[string, *rate.Limiter]{
MaxSize: length,
MaxBytes: 0,
SweepInterval: 0,
})
if err != nil {
return nil, err
}
store.Purge()
return &RateLimiter{store: store}, nil
}

// Allow returns true if the operation is allowed.
func (rl *RateLimiter) Allow(id string, limit int, duration time.Duration) bool {
if l, ok := rl.store.Get(id); ok {
limiter := l.(*rate.Limiter)
if limiter, ok := rl.store.Get(id); ok {
if limiter.Limit() == rate.Every(duration) && limiter.Burst() == limit {
return limiter.Allow()
}
}
limiter := rate.NewLimiter(rate.Every(duration), limit)
rl.store.Add(id, limiter)
rl.store.Put(id, limiter, 0)
return limiter.Allow()
}
4 changes: 3 additions & 1 deletion pkg/providers/legacy/http/ratelimiter/ratelimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ limitations under the License.
package ratelimiter

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestRateLimiter(t *testing.T) {
rl := NewRateLimiter(2)
rl, err := NewRateLimiter(context.Background(), 2)
r := require.New(t)
r.NoError(err)
duration := time.Second
testCases := []struct {
id string
Expand Down
Loading