Skip to content

Commit 062a68e

Browse files
committed
fix rate limit error message, and rename err log field
1 parent a537bfd commit 062a68e

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *Client) FetchUserPermission(ctx context.Context, projectID uint64, user
136136
userPerm, resourceAccess, err = c.permCache.GetUserPermission(ctx, projectID, userID)
137137
if err != nil {
138138
// log the error, but don't stop
139-
c.logger.With("error", err).Error("FetchUserPermission failed to query the permCache")
139+
c.logger.With("err", err).Error("FetchUserPermission failed to query the permCache")
140140
}
141141
}
142142

@@ -186,7 +186,7 @@ func (c *Client) SpendQuota(ctx context.Context, quota *proto.AccessQuota, compu
186186
}
187187
if event != nil {
188188
if _, err := c.quotaClient.NotifyEvent(ctx, quota.AccessKey.ProjectID, event); err != nil {
189-
c.logger.With("error", err, "op", "use_access_key", "event", event).Error("-> quota control: failed to notify")
189+
c.logger.With("err", err, "op", "use_access_key", "event", event).Error("-> quota control: failed to notify")
190190
}
191191
}
192192
return true, nil
@@ -250,7 +250,7 @@ func (c *Client) Run(ctx context.Context) error {
250250
// Start the sync
251251
for range c.ticker.C {
252252
if err := c.usage.SyncUsage(ctx, c.quotaClient, &c.service); err != nil {
253-
c.logger.With("error", err, "op", "run").Error("-> quota control: failed to sync usage")
253+
c.logger.With("err", err, "op", "run").Error("-> quota control: failed to sync usage")
254254
continue
255255
}
256256
c.logger.With("op", "run").Info("-> quota control: synced usage")
@@ -269,7 +269,7 @@ func (c *Client) Stop(timeoutCtx context.Context) {
269269
c.ticker.Stop()
270270
}
271271
if err := c.usage.SyncUsage(timeoutCtx, c.quotaClient, &c.service); err != nil {
272-
c.logger.With("error", err, "op", "run").Error("-> quota control: failed to sync usage")
272+
c.logger.With("err", err, "op", "run").Error("-> quota control: failed to sync usage")
273273
}
274274
c.logger.With("op", "stop").Info("-> quota control: stopped.")
275275
}

middleware/error.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func FailOnUnexpectedError(fn func(w http.ResponseWriter, err error)) func(w htt
1717

1818
func ContinueOnUnexpectedError(log logger.Logger, fn func(w http.ResponseWriter, err error)) func(w http.ResponseWriter, _ *http.Request, next http.Handler, err error) {
1919
return func(w http.ResponseWriter, r *http.Request, next http.Handler, err error) {
20-
log.With("error", err, "op", "quota").Error("-> quotacontrol: unexpected error")
20+
log.With("err", err, "op", "quota").Error("-> quotacontrol: unexpected error")
2121
if !shouldErrorContinue(log, err) {
2222
fn(w, err)
2323
return
@@ -28,7 +28,14 @@ func ContinueOnUnexpectedError(log logger.Logger, fn func(w http.ResponseWriter,
2828

2929
func ContinueOnAnyError(log logger.Logger, fn func(w http.ResponseWriter, err error)) func(w http.ResponseWriter, _ *http.Request, next http.Handler, err error) {
3030
return func(w http.ResponseWriter, r *http.Request, next http.Handler, err error) {
31-
log.With("error", err, "op", "quota").Error("-> quotacontrol: unexpected error -- continuing anyways")
31+
log.With("err", err, "op", "quota").Error("-> quotacontrol: unexpected error -- continuing anyways")
32+
33+
// all errors are okay, except for ErrLimitExceeded
34+
if errors.Is(err, proto.ErrLimitExceeded) {
35+
fn(w, err)
36+
return
37+
}
38+
3239
next.ServeHTTP(w, r.WithContext(WithSkipRateLimit(r.Context())))
3340
}
3441
}
@@ -40,7 +47,7 @@ func shouldErrorContinue(log logger.Logger, err error) bool {
4047
if !errors.As(err, &w) {
4148
// Sample log of unexpected errors (every 10 seconds)
4249
if time.Now().Second()%10 == 0 {
43-
log.With("error", err).Error("quotacontrol: unexpected error, allowing all traffic")
50+
log.With("err", err).Error("quotacontrol: unexpected error, allowing all traffic")
4451
}
4552
return true
4653
}

proto/legacy.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proto
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"net/http"
78
)
@@ -43,15 +44,24 @@ var _StatusCode = map[int]string{
4344
200: "",
4445
}
4546

46-
func RespondWithLegacyError(w http.ResponseWriter, err error) {
47-
rpcErr, ok := err.(WebRPCError)
48-
if !ok {
49-
rpcErr = ErrorWithCause(ErrWebrpcEndpoint, err)
50-
}
47+
func RespondWithLegacyError(rateLimitErrorMessage string) func(w http.ResponseWriter, err error) {
48+
return func(w http.ResponseWriter, err error) {
49+
rpcErr, ok := err.(WebRPCError)
50+
if !ok {
51+
rpcErr = ErrorWithCause(ErrWebrpcEndpoint, err)
52+
}
5153

52-
w.Header().Set("Content-Type", "application/json")
53-
w.WriteHeader(rpcErr.HTTPStatus)
54+
if errors.Is(err, ErrLimitExceeded) {
55+
rpcErr = ErrLimitExceeded
56+
if rateLimitErrorMessage != "" {
57+
rpcErr.Message = rateLimitErrorMessage
58+
}
59+
}
5460

55-
respBody, _ := json.Marshal(rpcErr.getLegacyPayload())
56-
w.Write(respBody)
61+
w.Header().Set("Content-Type", "application/json")
62+
w.WriteHeader(rpcErr.HTTPStatus)
63+
64+
respBody, _ := json.Marshal(rpcErr.getLegacyPayload())
65+
w.Write(respBody)
66+
}
5767
}

quotacontrol.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (q qcHandler) GetAccessQuota(ctx context.Context, accessKey string, now tim
167167
}
168168

169169
if err := q.quotaCache.SetAccessQuota(ctx, &record); err != nil {
170-
q.log.With("error", err).Error("quotacontrol: failed to set access quota in cache")
170+
q.log.With("err", err).Error("quotacontrol: failed to set access quota in cache")
171171
}
172172

173173
return &record, nil
@@ -207,12 +207,12 @@ func (q qcHandler) SetAccessLimit(ctx context.Context, projectID uint64, config
207207
}
208208
accessKeys, err := q.ListAccessKeys(ctx, projectID, proto.Ptr(true), nil)
209209
if err != nil {
210-
q.log.With("error", err).Error("quotacontrol: failed to list access keys")
210+
q.log.With("err", err).Error("quotacontrol: failed to list access keys")
211211
return true, nil
212212
}
213213
for _, access := range accessKeys {
214214
if err := q.quotaCache.DeleteAccessKey(ctx, access.AccessKey); err != nil {
215-
q.log.With("error", err).Error("quotacontrol: failed to delete access quota from cache")
215+
q.log.With("err", err).Error("quotacontrol: failed to delete access quota from cache")
216216
}
217217
}
218218
return true, nil
@@ -407,7 +407,7 @@ func (q qcHandler) GetUserPermission(ctx context.Context, projectID uint64, user
407407
}
408408

409409
if err := q.permCache.SetUserPermission(ctx, projectID, userID, perm, access); err != nil {
410-
q.log.With("error", err).Error("quotacontrol: failed to set user perm in cache")
410+
q.log.With("err", err).Error("quotacontrol: failed to set user perm in cache")
411411
}
412412

413413
return perm, access, nil
@@ -420,7 +420,7 @@ func (q qcHandler) updateAccessKey(ctx context.Context, access *proto.AccessKey)
420420
}
421421

422422
if err := q.quotaCache.DeleteAccessKey(ctx, access.AccessKey); err != nil {
423-
q.log.With("error", err).Error("quotacontrol: failed to delete access quota from cache")
423+
q.log.With("err", err).Error("quotacontrol: failed to delete access quota from cache")
424424
}
425425

426426
return access, nil

0 commit comments

Comments
 (0)