Skip to content

Commit fbe5a2d

Browse files
author
1911860538
committed
fix(contrib/registry/eureka): improve server picking and retry logic
1 parent fb8e43e commit fbe5a2d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

contrib/registry/eureka/client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ func (e *Client) filterUp(apps ...Application) (res []Instance) {
296296
}
297297

298298
func (e *Client) pickServer(currentTimes int) string {
299-
return e.urls[currentTimes%e.maxRetry]
299+
count := len(e.urls)
300+
if count == 0 {
301+
return ""
302+
}
303+
return e.urls[currentTimes%count]
300304
}
301305

302306
func (e *Client) shuffle() {
@@ -351,6 +355,11 @@ func (e *Client) request(ctx context.Context, method string, params []string, in
351355
}
352356

353357
func (e *Client) do(ctx context.Context, method string, params []string, input io.Reader, output any) error {
358+
if e.maxRetry == 0 {
359+
_, err := e.request(ctx, method, params, input, output, 0)
360+
return err
361+
}
362+
354363
for i := 0; i < e.maxRetry; i++ {
355364
retry, err := e.request(ctx, method, params, input, output, i)
356365
if retry {
@@ -361,5 +370,6 @@ func (e *Client) do(ctx context.Context, method string, params []string, input i
361370
}
362371
return nil
363372
}
373+
364374
return fmt.Errorf("retry after %d times", e.maxRetry)
365375
}

0 commit comments

Comments
 (0)