Files
weichen 29f14ac5f2 Add multi-instance groundwork: blobstore seam for artifacts
Details (original message):
Add multi-instance groundwork: blobstore seam for artifacts, PG LISTEN/NOTIFY event fanout, and per-tenant rate limiting with native Redis backend
2026-09-30 02:39:50 +00:00

27 lines
821 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"net/http"
"time"
"agent-system-backend/internal/ratelimit"
)
// 限流中间件(阶段 3):按租户 owner 固定窗口;limit<=0(默认)直接放行。
// 位于认证之后(key=owner);Limiter/LimitLookup 由组合根注入。
func (s *Server) withRateLimit(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.Limiter != nil && s.LimitLookup != nil {
if limit := s.LimitLookup(); limit > 0 {
if !s.Limiter.Allow(r.Context(), "owner:"+s.owner(r), limit, time.Minute) {
http.Error(w, "rate limit exceeded(runtime.rateLimitPerMin)", http.StatusTooManyRequests)
return
}
}
}
next.ServeHTTP(w, r)
})
}
var _ = ratelimit.Memory{} // 接口形状锚点(Limiter 注入自组合根)