MCPcopy Index your code
hub / github.com/dnote/dnote / Limit

Method Limit

pkg/server/middleware/limit.go:124–139  ·  view source on GitHub ↗

Limit is a middleware to rate limit the handler

(next http.Handler)

Source from the content-addressed store, hash-verified

122
123// Limit is a middleware to rate limit the handler
124func (rl *RateLimiter) Limit(next http.Handler) http.HandlerFunc {
125 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126 identifier := lookupIP(r)
127 limiter := rl.getVisitor(identifier)
128
129 if !limiter.Allow() {
130 http.Error(w, "Too many requests", http.StatusTooManyRequests)
131 log.WithFields(log.Fields{
132 "ip": identifier,
133 }).Warn("Too many requests")
134 return
135 }
136
137 next.ServeHTTP(w, r)
138 })
139}
140
141// ApplyLimit applies rate limit conditionally using the global limiter
142func ApplyLimit(h http.HandlerFunc, rateLimit bool) http.Handler {

Callers 5

TestLimitFunction · 0.95
TestLimit_DifferentIPsFunction · 0.95
newFragmentMethod · 0.80
ApplyLimitFunction · 0.80
paginateFunction · 0.80

Calls 6

getVisitorMethod · 0.95
WithFieldsFunction · 0.92
lookupIPFunction · 0.85
WarnMethod · 0.80
ServeHTTPMethod · 0.80
ErrorMethod · 0.45

Tested by 2

TestLimitFunction · 0.76
TestLimit_DifferentIPsFunction · 0.76