Skip to content

Commit

Permalink
Rename headers
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Jul 11, 2024
1 parent e910cd1 commit 62988fe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
10 changes: 4 additions & 6 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/stretchr/testify/require"
)

const RateLimitHeader = "x-ratelimit-limit"

func TestMiddlewareUseAccessKey(t *testing.T) {
auth := jwtauth.New("HS256", []byte("secret"), nil)

Expand Down Expand Up @@ -468,7 +466,7 @@ func TestJWTAccess(t *testing.T) {
require.ErrorIs(t, err, proto.ErrUnauthorizedUser)
assert.False(t, ok)
assert.Equal(t, strconv.FormatInt(limit.FreeMax, 10), headers.Get(middleware.HeaderQuotaLimit))
assert.Equal(t, strconv.FormatInt(limit.RateLimit, 10), headers.Get(RateLimitHeader))
assert.Equal(t, strconv.FormatInt(limit.RateLimit, 10), headers.Get(middleware.HeaderCreditsLimit))
})

server.Store.SetUserPermission(ctx, project, account, proto.UserPermission_READ_WRITE, proto.ResourceAccess{ProjectID: project})
Expand All @@ -478,7 +476,7 @@ func TestJWTAccess(t *testing.T) {
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, strconv.FormatInt(limit.FreeMax, 10), headers.Get(middleware.HeaderQuotaLimit))
assert.Equal(t, strconv.FormatInt(limit.RateLimit, 10), headers.Get(RateLimitHeader))
assert.Equal(t, strconv.FormatInt(limit.RateLimit, 10), headers.Get(middleware.HeaderCreditsLimit))
expectedHits++
})

Expand All @@ -489,7 +487,7 @@ func TestJWTAccess(t *testing.T) {
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, strconv.FormatInt(limit.FreeMax, 10), headers.Get(middleware.HeaderQuotaLimit))
assert.Equal(t, strconv.FormatInt(limit.RateLimit, 10), headers.Get(RateLimitHeader))
assert.Equal(t, strconv.FormatInt(limit.RateLimit, 10), headers.Get(middleware.HeaderCreditsLimit))
expectedHits++
})

Expand Down Expand Up @@ -633,7 +631,7 @@ func TestSession(t *testing.T) {
if success {
assert.NoError(t, err, "%s/%s %+v", service, method, tc)
assert.True(t, ok)
switch v := h.Get(RateLimitHeader); tc.Session {
switch v := h.Get(middleware.HeaderCreditsLimit); tc.Session {
case proto.SessionType_Public:
assert.Equal(t, publicRPM, v)
case proto.SessionType_AccessKey, proto.SessionType_Project:
Expand Down
18 changes: 9 additions & 9 deletions middleware/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
)

const (
HeaderAccessKey = "X-Access-Key"
HeaderOrigin = "Origin"
HeaderQuotaLimit = "Quota-Limit"
HeaderQuotaRemaining = "Quota-Remaining"
HeaderQuotaOverage = "Quota-Overage"
HeaderQuotaCost = "Quota-Cost"
HeaderQuotaRateRemaining = "Quota-Rate-Remaining"
HeaderQuotaRateLimit = "Quota-Rate-Limit"
HeaderQuotaRateReset = "Quota-Rate-Reset"
HeaderAccessKey = "X-Access-Key"
HeaderOrigin = "Origin"
HeaderQuotaLimit = "Quota-Limit"
HeaderQuotaRemaining = "Quota-Remaining"
HeaderQuotaOverage = "Quota-Overage"
HeaderCreditsCost = "Credits-Cost"
HeaderCreditsRemaining = "Credits-Rate-Remaining"
HeaderCreditsLimit = "Credits-Rate-Limit"
HeaderCreditsReset = "Credits-Rate-Reset"
)

// Client is the interface that wraps the basic FetchKeyQuota, GetUsage and SpendQuota methods.
Expand Down
6 changes: 3 additions & 3 deletions middleware/middleware_ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func RateLimit(rlCfg RLConfig, redisCfg redis.Config) func(next http.Handler) ht
return func(next http.Handler) http.Handler {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
swapHeader(h, "X-RateLimit-Limit", HeaderQuotaRateLimit)
swapHeader(h, "X-RateLimit-Remaining", HeaderQuotaRateRemaining)
swapHeader(h, "X-RateLimit-Reset", HeaderQuotaRateReset)
swapHeader(h, "X-RateLimit-Limit", HeaderCreditsLimit)
swapHeader(h, "X-RateLimit-Remaining", HeaderCreditsRemaining)
swapHeader(h, "X-RateLimit-Reset", HeaderCreditsReset)
next.ServeHTTP(w, r)
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions middleware/middleware_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func EnsureUsage(client Client) func(next http.Handler) http.Handler {
next.ServeHTTP(w, r)
return
}
w.Header().Set(HeaderQuotaCost, strconv.FormatInt(cu, 10))
w.Header().Set(HeaderCreditsCost, strconv.FormatInt(cu, 10))

usage, err := client.FetchUsage(ctx, quota, GetTime(ctx))
if err != nil {
Expand Down Expand Up @@ -74,7 +74,7 @@ func SpendUsage(client Client) func(next http.Handler) http.Handler {
next.ServeHTTP(w, r)
return
}
w.Header().Set(HeaderQuotaCost, strconv.FormatInt(cu, 10))
w.Header().Set(HeaderCreditsCost, strconv.FormatInt(cu, 10))

ok, total, err := client.SpendQuota(ctx, quota, cu, GetTime(ctx))
if err != nil && !errors.Is(err, proto.ErrLimitExceeded) {
Expand Down

0 comments on commit 62988fe

Please sign in to comment.