ThrottleWithOpts is a middleware that limits number of currently processed requests using passed ThrottleOpts.
(opts ThrottleOpts)
| 42 | |
| 43 | // ThrottleWithOpts is a middleware that limits number of currently processed requests using passed ThrottleOpts. |
| 44 | func ThrottleWithOpts(opts ThrottleOpts) func(http.Handler) http.Handler { |
| 45 | if opts.Limit < 1 { |
| 46 | panic("chi/middleware: Throttle expects limit > 0") |
| 47 | } |
| 48 | |
| 49 | if opts.BacklogLimit < 0 { |
| 50 | panic("chi/middleware: Throttle expects backlogLimit to be positive") |
| 51 | } |
| 52 | |
| 53 | statusCode := opts.StatusCode |
| 54 | if statusCode == 0 { |
| 55 | statusCode = http.StatusTooManyRequests |
| 56 | } |
| 57 | |
| 58 | t := throttler{ |
| 59 | tokens: make(chan token, opts.Limit), |
| 60 | backlogTokens: make(chan token, opts.Limit+opts.BacklogLimit), |
| 61 | backlogTimeout: opts.BacklogTimeout, |
| 62 | statusCode: statusCode, |
| 63 | retryAfterFn: opts.RetryAfterFn, |
| 64 | } |
| 65 | |
| 66 | // Filling tokens. |
| 67 | for i := 0; i < opts.Limit+opts.BacklogLimit; i++ { |
| 68 | if i < opts.Limit { |
| 69 | t.tokens <- token{} |
| 70 | } |
| 71 | t.backlogTokens <- token{} |
| 72 | } |
| 73 | |
| 74 | return func(next http.Handler) http.Handler { |
| 75 | fn := func(w http.ResponseWriter, r *http.Request) { |
| 76 | ctx := r.Context() |
| 77 | |
| 78 | select { |
| 79 | |
| 80 | case <-ctx.Done(): |
| 81 | t.setRetryAfterHeaderIfNeeded(w, true) |
| 82 | http.Error(w, errContextCanceled, t.statusCode) |
| 83 | return |
| 84 | |
| 85 | case btok := <-t.backlogTokens: |
| 86 | defer func() { |
| 87 | t.backlogTokens <- btok |
| 88 | }() |
| 89 | |
| 90 | // Try to get a processing token immediately first |
| 91 | select { |
| 92 | case tok := <-t.tokens: |
| 93 | defer func() { |
| 94 | t.tokens <- tok |
| 95 | }() |
| 96 | next.ServeHTTP(w, r) |
| 97 | return |
| 98 | default: |
| 99 | // No immediate token available, need to wait with timer |
| 100 | } |
| 101 |