|
| 1 | +package limiter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "sync" |
| 9 | + "sync/atomic" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func makeFakeHandler(ctx context.Context, completeInFlightRequestChan chan struct{}) http.HandlerFunc { |
| 15 | + return func(w http.ResponseWriter, r *http.Request) { |
| 16 | + select { |
| 17 | + case <-ctx.Done(): |
| 18 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 19 | + case <-completeInFlightRequestChan: |
| 20 | + w.WriteHeader(http.StatusOK) |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func doRRandRequest(ctx context.Context, wg *sync.WaitGroup, cl http.Handler) *httptest.ResponseRecorder { |
| 26 | + // If wait for handler is true, it waits until the code is in the handler function |
| 27 | + rr := httptest.NewRecorder() |
| 28 | + // This should never fail unless we're out of memory or something |
| 29 | + req, err := http.NewRequest("GET", "/", nil) |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + req = req.WithContext(ctx) |
| 34 | + |
| 35 | + wg.Add(1) |
| 36 | + go func() { |
| 37 | + // If this code path is meant to make it into the handler, we need a way to figure out if it's there or not |
| 38 | + cl.ServeHTTP(rr, req) |
| 39 | + // If the request was aborted, unblock any waiting goroutines |
| 40 | + wg.Done() |
| 41 | + }() |
| 42 | + |
| 43 | + return rr |
| 44 | +} |
| 45 | + |
| 46 | +func TestConcurrencyLimitUnderLimit(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + |
| 49 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 50 | + defer cancel() |
| 51 | + |
| 52 | + completeInFlightRequestChan := make(chan struct{}) |
| 53 | + handler := makeFakeHandler(ctx, completeInFlightRequestChan) |
| 54 | + cl := NewConcurrencyLimiter(http.Handler(handler), 2) |
| 55 | + |
| 56 | + wg := &sync.WaitGroup{} |
| 57 | + rr1 := doRRandRequest(ctx, wg, cl) |
| 58 | + // This will "release" the request rr1 |
| 59 | + completeInFlightRequestChan <- struct{}{} |
| 60 | + |
| 61 | + // This should never take more than the timeout |
| 62 | + wg.Wait() |
| 63 | + |
| 64 | + // We want to access the response recorder directly, so we don't accidentally get an implicitly correct answer |
| 65 | + if rr1.Code != http.StatusOK { |
| 66 | + t.Fatalf("Want response code %d, got: %d", http.StatusOK, rr1.Code) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestConcurrencyLimitAtLimit(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + completeInFlightRequestChan := make(chan struct{}) |
| 77 | + handler := makeFakeHandler(ctx, completeInFlightRequestChan) |
| 78 | + |
| 79 | + cl := NewConcurrencyLimiter(http.Handler(handler), 2) |
| 80 | + |
| 81 | + wg := &sync.WaitGroup{} |
| 82 | + rr1 := doRRandRequest(ctx, wg, cl) |
| 83 | + rr2 := doRRandRequest(ctx, wg, cl) |
| 84 | + |
| 85 | + completeInFlightRequestChan <- struct{}{} |
| 86 | + completeInFlightRequestChan <- struct{}{} |
| 87 | + |
| 88 | + wg.Wait() |
| 89 | + |
| 90 | + if rr1.Code != http.StatusOK { |
| 91 | + t.Fatalf("Want response code %d, got: %d", http.StatusOK, rr1.Code) |
| 92 | + } |
| 93 | + if rr2.Code != http.StatusOK { |
| 94 | + t.Fatalf("Want response code %d, got: %d", http.StatusOK, rr1.Code) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func count(r *httptest.ResponseRecorder, code200s, code429s *int) { |
| 99 | + switch r.Code { |
| 100 | + case http.StatusTooManyRequests: |
| 101 | + *code429s = *code429s + 1 |
| 102 | + case http.StatusOK: |
| 103 | + *code200s = *code200s + 1 |
| 104 | + default: |
| 105 | + panic(fmt.Sprintf("Unknown code: %d", r.Code)) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestConcurrencyLimitOverLimit(t *testing.T) { |
| 110 | + t.Parallel() |
| 111 | + |
| 112 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 113 | + defer cancel() |
| 114 | + completeInFlightRequestChan := make(chan struct{}, 3) |
| 115 | + handler := makeFakeHandler(ctx, completeInFlightRequestChan) |
| 116 | + |
| 117 | + cl := NewConcurrencyLimiter(http.Handler(handler), 2).(*ConcurrencyLimiter) |
| 118 | + |
| 119 | + wg := &sync.WaitGroup{} |
| 120 | + |
| 121 | + rr1 := doRRandRequest(ctx, wg, cl) |
| 122 | + rr2 := doRRandRequest(ctx, wg, cl) |
| 123 | + for ctx.Err() == nil { |
| 124 | + if requestsStarted := atomic.LoadUint64(&cl.requestsStarted); requestsStarted == 2 { |
| 125 | + break |
| 126 | + } |
| 127 | + time.Sleep(time.Millisecond) |
| 128 | + } |
| 129 | + rr3 := doRRandRequest(ctx, wg, cl) |
| 130 | + for ctx.Err() == nil { |
| 131 | + if requestsStarted := atomic.LoadUint64(&cl.requestsStarted); requestsStarted == 3 { |
| 132 | + break |
| 133 | + } |
| 134 | + time.Sleep(time.Millisecond) |
| 135 | + } |
| 136 | + completeInFlightRequestChan <- struct{}{} |
| 137 | + completeInFlightRequestChan <- struct{}{} |
| 138 | + completeInFlightRequestChan <- struct{}{} |
| 139 | + |
| 140 | + wg.Wait() |
| 141 | + |
| 142 | + code200s := 0 |
| 143 | + code429s := 0 |
| 144 | + count(rr1, &code200s, &code429s) |
| 145 | + count(rr2, &code200s, &code429s) |
| 146 | + count(rr3, &code200s, &code429s) |
| 147 | + if code200s != 2 || code429s != 1 { |
| 148 | + t.Fatalf("code 200s: %d, and code429s: %d", code200s, code429s) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestConcurrencyLimitOverLimitAndRecover(t *testing.T) { |
| 153 | + t.Parallel() |
| 154 | + |
| 155 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 156 | + defer cancel() |
| 157 | + completeInFlightRequestChan := make(chan struct{}, 4) |
| 158 | + handler := makeFakeHandler(ctx, completeInFlightRequestChan) |
| 159 | + cl := NewConcurrencyLimiter(http.Handler(handler), 2).(*ConcurrencyLimiter) |
| 160 | + |
| 161 | + wg := &sync.WaitGroup{} |
| 162 | + |
| 163 | + rr1 := doRRandRequest(ctx, wg, cl) |
| 164 | + rr2 := doRRandRequest(ctx, wg, cl) |
| 165 | + for ctx.Err() == nil { |
| 166 | + if requestsStarted := atomic.LoadUint64(&cl.requestsStarted); requestsStarted == 2 { |
| 167 | + break |
| 168 | + } |
| 169 | + time.Sleep(time.Millisecond) |
| 170 | + } |
| 171 | + // This will 429 |
| 172 | + rr3 := doRRandRequest(ctx, wg, cl) |
| 173 | + for ctx.Err() == nil { |
| 174 | + if requestsStarted := atomic.LoadUint64(&cl.requestsStarted); requestsStarted == 3 { |
| 175 | + break |
| 176 | + } |
| 177 | + time.Sleep(time.Millisecond) |
| 178 | + } |
| 179 | + completeInFlightRequestChan <- struct{}{} |
| 180 | + completeInFlightRequestChan <- struct{}{} |
| 181 | + completeInFlightRequestChan <- struct{}{} |
| 182 | + // Although we could do another wg.Wait here, I don't think we should because |
| 183 | + // it might provide a false sense of confidence |
| 184 | + for ctx.Err() == nil { |
| 185 | + if requestsCompleted := atomic.LoadUint64(&cl.requestsCompleted); requestsCompleted == 3 { |
| 186 | + break |
| 187 | + } |
| 188 | + time.Sleep(time.Millisecond) |
| 189 | + } |
| 190 | + rr4 := doRRandRequest(ctx, wg, cl) |
| 191 | + completeInFlightRequestChan <- struct{}{} |
| 192 | + wg.Wait() |
| 193 | + |
| 194 | + code200s := 0 |
| 195 | + code429s := 0 |
| 196 | + count(rr1, &code200s, &code429s) |
| 197 | + count(rr2, &code200s, &code429s) |
| 198 | + count(rr3, &code200s, &code429s) |
| 199 | + count(rr4, &code200s, &code429s) |
| 200 | + |
| 201 | + if code200s != 3 || code429s != 1 { |
| 202 | + t.Fatalf("code 200s: %d, and code429s: %d", code200s, code429s) |
| 203 | + } |
| 204 | +} |
0 commit comments