Skip to content

Commit

Permalink
RateLimit credits only for Quota
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Jul 10, 2024
1 parent 9bad32c commit 497c31e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 0 additions & 2 deletions middleware/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/0xsequence/quotacontrol/proto"
"github.com/go-chi/httprate"
)

const (
Expand Down Expand Up @@ -205,7 +204,6 @@ func getProjectID(ctx context.Context) (uint64, bool) {

// WithComputeUnits sets the compute units and rate limit increment to the context.
func WithComputeUnits(ctx context.Context, cu int64) context.Context {
ctx = httprate.WithIncrement(ctx, int(cu))
return context.WithValue(ctx, ctxKeyComputeUnits, cu)
}

Expand Down
3 changes: 3 additions & 0 deletions middleware/middleware_ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func RateLimit(rlCfg RLConfig, redisCfg redis.Config) func(next http.Handler) ht
if _, ok := GetService(ctx); ok {
ctx = httprate.WithRequestLimit(ctx, serviceRPM)
} else if q, ok := GetAccessQuota(ctx); ok {
if cu, ok := getComputeUnits(ctx); ok {
ctx = httprate.WithIncrement(ctx, int(cu))
}
ctx = httprate.WithRequestLimit(ctx, int(q.Limit.RateLimit))
} else if _, ok := GetAccount(ctx); ok {
ctx = httprate.WithRequestLimit(ctx, accountRPM)
Expand Down

0 comments on commit 497c31e

Please sign in to comment.