Skip to content

fix: handle request errors in nameserver retry loop#121

Open
zane-byte-dev wants to merge 5 commits into
ali-sdk:masterfrom
zane-byte-dev:fix-name-server-retry
Open

fix: handle request errors in nameserver retry loop#121
zane-byte-dev wants to merge 5 commits into
ali-sdk:masterfrom
zane-byte-dev:fix-name-server-retry

Conversation

@zane-byte-dev

Copy link
Copy Markdown

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:

  • Wraps the HTTP request inside the retry loop with try-catch so thrown errors are also retried
  • Adds a delay between retries to avoid hammering a struggling server
  • Preserves the last error for a meaningful error message when all retries are exhausted
  • Adds test coverage for retry-on-timeout behavior

Changes

  • lib/remoting_client.js: add try-catch + backoff delay in the retry loop
  • test/remoting_client.test.js: add test for retry on request timeout

Test plan

  • Existing tests pass
  • New test: request timeout triggers retry and eventually throws with meaningful error

Copilot AI review requested due to automatic review settings June 8, 2026 02:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.json in .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.

Comment thread lib/remoting_client.js
'use strict';

const assert = require('assert');
const sleep = require('mz-modules/sleep');
Comment thread lib/remoting_client.js
Comment on lines +174 to 176
if (retries > 0) {
await sleep(1000);
}
Comment thread lib/remoting_client.js
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 thread lib/remoting_client.js
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);
}
Comment thread lib/remoting_client.js
}
}
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);
Copilot AI review requested due to automatic review settings June 8, 2026 04:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 5 comments.

Comment thread lib/remoting_client.js
}
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 thread lib/remoting_client.js
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 thread package.json
Comment on lines +2 to +3
"name": "@ali/ali-ons",
"version": "3.12.3",
Comment thread package.json
Comment on lines +60 to +61
"publishConfig": {
"registry": "https://registry.anpm.alibaba-inc.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants