fix: handle request errors in nameserver retry loop#121
Open
zane-byte-dev wants to merge 5 commits into
Open
Conversation
…try-exception-handling fix: catch httpclient exceptions in updateNameServerAddressList retry loop
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds retry support (with delay) to updateNameServerAddressList() when the HTTP request fails, and introduces tests to validate retry + error behavior.
Changes:
- Add retry loop error handling with 1s delay between attempts in
RemotingClient.updateNameServerAddressList(). - Add tests covering transient request failures and “all retries fail” behavior.
- Ignore
package-lock.jsonin.gitignore.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| test/remoting_client.test.js | Adds unit tests for retrying and final error propagation. |
| lib/remoting_client.js | Implements retry-on-throw behavior with sleep between attempts and throws last error. |
| .gitignore | Adds package-lock.json to ignored files. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const sleep = require('mz-modules/sleep'); |
Comment on lines
+174
to
176
| if (retries > 0) { | ||
| await sleep(1000); | ||
| } |
Comment on lines
168
to
+173
| } | ||
| this.logger.info('[mq:remoting_client] fetch name server addresses successfully, address: %s', addrs); | ||
| return; | ||
| lastErr = new Error('[mq:remoting_client] fetch name server addresses failed, ret.statusCode: ' + ret.status); | ||
| } catch (e) { | ||
| lastErr = e; | ||
| this.logger.warn('[mq:remoting_client] fetch name server addresses error, retries remaining: %d, %s', retries, e.message); | ||
| } |
Comment on lines
+170
to
+173
| } catch (e) { | ||
| lastErr = e; | ||
| this.logger.warn('[mq:remoting_client] fetch name server addresses error, retries remaining: %d, %s', retries, e.message); | ||
| } |
| } | ||
| } | ||
| throw new Error('[mq:remoting_client] fetch name server addresses failed, ret.statusCode: ' + ret.status); | ||
| throw lastErr; |
| }); | ||
| const client = new RemotingClient(Object.assign({ httpclient }, config, { nameSrv: null, onsAddr: 'http://127.0.0.1:0/nsaddr' })); | ||
| await client.updateNameServerAddressList(); | ||
| assert(count === 3); |
| } | ||
| this.logger.info('[mq:remoting_client] fetch name server addresses successfully, address: %s', addrs); | ||
| return; | ||
| lastErr = new Error('[mq:remoting_client] fetch name server addresses failed, ret.statusCode: ' + ret.status); |
Comment on lines
+156
to
176
| try { | ||
| const ret = await this.httpclient.request(url, { | ||
| timeout: this.options.connectTimeout || 10000, | ||
| }); | ||
| if (ret.status === 200) { | ||
| const addrs = ret.data.toString().trim(); | ||
| const newList = addrs.split(';'); | ||
| if (newList.length) { | ||
| this._namesrvAddrList = newList; | ||
| } | ||
| this.logger.info('[mq:remoting_client] fetch name server addresses successfully, address: %s', addrs); | ||
| return; | ||
| } | ||
| this.logger.info('[mq:remoting_client] fetch name server addresses successfully, address: %s', addrs); | ||
| return; | ||
| lastErr = new Error('[mq:remoting_client] fetch name server addresses failed, ret.statusCode: ' + ret.status); | ||
| } catch (e) { | ||
| lastErr = e; | ||
| this.logger.warn('[mq:remoting_client] fetch name server addresses error, retries remaining: %d, %s', retries, e.message); | ||
| } | ||
| if (retries > 0) { | ||
| await sleep(1000); | ||
| } |
Comment on lines
+178
to
+192
| it('should retry updateNameServerAddressList when request throws', async () => { | ||
| let count = 0; | ||
| mm(httpclient, 'request', async () => { | ||
| count++; | ||
| if (count < 3) { | ||
| throw new Error('mock connect timeout'); | ||
| } | ||
| return { | ||
| status: 200, | ||
| data: '127.0.0.1:9876;127.0.0.2:9876', | ||
| }; | ||
| }); | ||
| const client = new RemotingClient(Object.assign({ httpclient }, config, { nameSrv: null, onsAddr: 'http://127.0.0.1:0/nsaddr' })); | ||
| await client.updateNameServerAddressList(); | ||
| assert(count === 3); |
Comment on lines
+2
to
+3
| "name": "@ali/ali-ons", | ||
| "version": "3.12.3", |
Comment on lines
+60
to
+61
| "publishConfig": { | ||
| "registry": "https://registry.anpm.alibaba-inc.com" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #118 — the retry loop added for nameserver address fetching only retries
on non-200 status codes. If the HTTP request itself throws (timeout, ECONNREFUSED,
ECONNRESET, etc.), the error propagates immediately without retrying.
This PR:
Changes
lib/remoting_client.js: add try-catch + backoff delay in the retry looptest/remoting_client.test.js: add test for retry on request timeoutTest plan