Skip to content

Commit

Permalink
rollback when updating failed
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Sep 26, 2024
1 parent a78b7ab commit cf11dfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 0 additions & 1 deletion pkg/ratelimit/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func UpdateQPSLimiter(limit float64, burst int) Option {
}
lim, _ := l.limiters.LoadOrStore(label, newLimiter())
return lim.(*limiter).updateQPSConfig(limit, burst)

}
}

Expand Down
20 changes: 16 additions & 4 deletions server/api/service_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func (h *serviceMiddlewareHandler) SetRateLimitConfig(w http.ResponseWriter, r *
h.rd.Text(w, http.StatusBadRequest, "This service is in allow list whose config can not be changed.")
return
}
cfg := h.svr.GetRateLimitConfig().LimiterConfig[serviceLabel]
oldCfg := h.svr.GetRateLimitConfig().Clone()
cfg := oldCfg.LimiterConfig[serviceLabel]
// update concurrency limiter
concurrencyFloat, okc := input["concurrency"].(float64)
if okc {
Expand All @@ -208,7 +209,12 @@ func (h *serviceMiddlewareHandler) SetRateLimitConfig(w http.ResponseWriter, r *
if status&ratelimit.LimiterDeleted != 0 {
cfg := h.svr.GetServiceMiddlewareConfig()
delete(cfg.RateLimitConfig.LimiterConfig, serviceLabel)
h.svr.SetRateLimitConfig(cfg.RateLimitConfig)
if err := h.svr.SetRateLimitConfig(cfg.RateLimitConfig); err != nil {
old := oldCfg.LimiterConfig[serviceLabel]
h.svr.UpdateServiceRateLimiter(serviceLabel, ratelimit.UpdateDimensionConfig(&old))
h.rd.Text(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.Text(w, http.StatusOK, "Rate limiter is deleted.")
return
}
Expand Down Expand Up @@ -245,7 +251,8 @@ func (h *serviceMiddlewareHandler) SetGRPCRateLimitConfig(w http.ResponseWriter,
return
}

cfg := h.svr.GetGRPCRateLimitConfig().LimiterConfig[serviceLabel]
oldCfg := h.svr.GetGRPCRateLimitConfig().Clone()
cfg := oldCfg.LimiterConfig[serviceLabel]
// update concurrency limiter
concurrencyFloat, okc := input["concurrency"].(float64)
if okc {
Expand All @@ -270,7 +277,12 @@ func (h *serviceMiddlewareHandler) SetGRPCRateLimitConfig(w http.ResponseWriter,
if status&ratelimit.LimiterDeleted != 0 {
cfg := h.svr.GetServiceMiddlewareConfig()
delete(cfg.GRPCRateLimitConfig.LimiterConfig, serviceLabel)
h.svr.SetGRPCRateLimitConfig(cfg.GRPCRateLimitConfig)
if err := h.svr.SetGRPCRateLimitConfig(cfg.GRPCRateLimitConfig); err != nil {
old := oldCfg.LimiterConfig[serviceLabel]
h.svr.UpdateGRPCServiceRateLimiter(serviceLabel, ratelimit.UpdateDimensionConfig(&old))
h.rd.Text(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.Text(w, http.StatusOK, "gRPC limiter is deleted.")
return
}
Expand Down

0 comments on commit cf11dfe

Please sign in to comment.