This commit is contained in:
2025-09-28 20:19:45 +03:00
parent 6638ef1b5f
commit 3fd63d5f32
21 changed files with 356 additions and 355 deletions

View File

@@ -64,6 +64,7 @@ end
type RateLimitTransport struct {
http.RoundTripper
redis rueidis.Client
}
func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error) {
@@ -84,7 +85,7 @@ func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error
return nil, fmt.Errorf("sellerId is required in JWT claims")
}
now := time.Now().UnixMilli()
client := *redis.Client
client := t.redis
waitTime, err := tokenBucketScript.Exec(ctx, client, []string{sellerId}, []string{
fmt.Sprintf("%d", now),
@@ -113,7 +114,7 @@ func SyncRateLimitRemaining(ctx context.Context, sellerId string, remaining int)
return fmt.Errorf("invalid sellerId or remaining")
}
now := time.Now().UnixMilli()
client := *redis.Client
client :=
cmds := []rueidis.Completed{
client.B().Set().Key(sellerId + ":capacity").Value(fmt.Sprintf("%d", defaultBucketCapacity)).Ex(time.Minute).Build(),
@@ -167,6 +168,6 @@ func SetRateLimitRetry(ctx context.Context, sellerId string, retrySeconds int, l
return nil
}
func NewRateLimitTransport() *RateLimitTransport {
func NewRateLimitTransport(client rueidis.Client) *RateLimitTransport {
return &RateLimitTransport{RoundTripper: http.DefaultTransport}
}