File tree Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Original file line number Diff line number Diff line change @@ -240,13 +240,28 @@ func (b *roundRobinBalancer) Next(c echo.Context) *ProxyTarget {
240
240
return b .targets [0 ]
241
241
}
242
242
243
- // reset the index if out of bounds
244
- if b .i >= len (b .targets ) {
245
- b .i = 0
243
+ var i int
244
+ const lastIdxKey = "_round_robin_last_index"
245
+ // This request is a retry, start from the index of the previous
246
+ // target to ensure we don't attempt to retry the request with
247
+ // the same failed target
248
+ if c .Get (lastIdxKey ) != nil {
249
+ i = c .Get (lastIdxKey ).(int )
250
+ i ++
251
+ if i >= len (b .targets ) {
252
+ i = 0
253
+ }
254
+ // This is a first time request, use the global index
255
+ } else {
256
+ i = b .i
257
+ b .i ++
258
+ if b .i >= len (b .targets ) {
259
+ b .i = 0
260
+ }
246
261
}
247
- t := b . targets [ b . i ]
248
- b . i ++
249
- return t
262
+
263
+ c . Set ( lastIdxKey , i )
264
+ return b . targets [ i ]
250
265
}
251
266
252
267
// Proxy returns a Proxy middleware.
Original file line number Diff line number Diff line change @@ -621,7 +621,7 @@ func TestProxyRetryWithBackendTimeout(t *testing.T) {
621
621
req := httptest .NewRequest (http .MethodGet , "/" , nil )
622
622
rec := httptest .NewRecorder ()
623
623
e .ServeHTTP (rec , req )
624
- assert .Contains (t , [] int { 200 , 502 } , rec .Code )
624
+ assert .Equal (t , 200 , rec .Code )
625
625
}()
626
626
}
627
627
You can’t perform that action at this time.
0 commit comments