aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRagnis Armus <ragnis@armus.ee>2019-12-11 22:51:44 +0200
committerRagnis Armus <ragnis@armus.ee>2019-12-11 23:03:18 +0200
commit254cb177eb5abdd9d137d4619aef76f37aa5033e (patch)
tree050b9c52541bad259df84581c2fd221cae9e1aa5
parenta6b0f3aad471c14d3e63d2d49ffb041b85fa5088 (diff)
Remove infinite maxStale option
-rw-r--r--lib/agent.js15
-rw-r--r--lib/agent.test.js37
2 files changed, 8 insertions, 44 deletions
diff --git a/lib/agent.js b/lib/agent.js
index eadf84c..f54001d 100644
--- a/lib/agent.js
+++ b/lib/agent.js
@@ -5,6 +5,7 @@ const kOptions = Symbol('options');
const kTargets = Symbol('targets');
const defaultTtl = 5;
+const defaultMaxStale = 120;
const defaultMaxRetryWait = 30;
const createError = (code, msg) => Object.assign(new Error(msg), { code });
@@ -12,6 +13,9 @@ const createError = (code, msg) => Object.assign(new Error(msg), { code });
const errNodata = createError(dns.NODATA, 'no address is available');
const errMaxStale = createError(dns.SERVFAIL, 'cached data is older than maxStale seconds');
+const withDefault = (value, defaultValue) => (
+ value === undefined ? defaultValue : value);
+
/**
* Returns whether the error is a connection error which is worty of a re-try.
*/
@@ -73,9 +77,8 @@ class Target {
timeoutMs: options.resolveTimeoutMs,
};
- this.logger = options.logger;
- this.maxStale = options.maxStale;
- this.maxRetryWait = options.maxRetryWait || defaultMaxRetryWait;
+ this.maxStale = withDefault(options.maxStale, defaultMaxStale);
+ this.maxRetryWait = withDefault(options.maxRetryWait, defaultMaxRetryWait);
this.hostname = hostname;
this.getTime = now;
@@ -206,10 +209,7 @@ class Target {
this.err = undefined;
this.errSeq = 0;
- if (this.maxStale !== undefined) {
- this.maxStaleAt = this.getTime() + this.ttl + this.maxStale;
- }
-
+ this.maxStaleAt = this.getTime() + this.ttl + this.maxStale;
this.renewNextAfter = this.getTime() + this.ttl;
},
(err) => {
@@ -246,7 +246,6 @@ class Target {
class Agent {
/**
* @param {Object} [options]
- * @param {Object} [options.logger]
* @param {number} [options.maxStale]
* @param {number} [options.maxRetryWait]
* @param {number} [options.resolveTimeoutMs]
diff --git a/lib/agent.test.js b/lib/agent.test.js
index c7ec955..4849776 100644
--- a/lib/agent.test.js
+++ b/lib/agent.test.js
@@ -158,41 +158,6 @@ describe('Target', function () {
);
});
- it('should return stale data forever when maxStale is undefined', async function () {
- let time = 10;
- const target = new Target({});
-
- target.getTime = () => time;
- target.getRetryWait = () => 10;
- target.resolver = async () => [
- { name: 'foo.com', port: 80, ttl: 10 },
- ];
-
- // Perform the first successful renewal.
- await target.resolve();
-
- // Make further renewals fail.
- target.resolver = async () => {
- throw Object.assign(new Error('no data'), { code: dns.NODATA });
- };
-
- time = 1000;
- assert.strictEqual(String(await target.resolve()), 'foo.com:80');
- assert.strictEqual(String(await target.resolve()), 'foo.com:80');
-
- target.resolver = async () => [
- { name: 'bar.com', port: 80, ttl: 10 },
- ];
-
- // Renewal does not happen yet, because errors inherit the last
- // known TTL.
- time = 1005;
- assert.strictEqual(String(await target.resolve()), 'foo.com:80');
-
- time = 1010.1;
- assert.strictEqual(String(await target.resolve()), 'bar.com:80');
- });
-
it('should not return stale data when maxStale is 0', async function () {
let time = 10;
const target = new Target({ maxStale: 0 });
@@ -278,7 +243,7 @@ describe('Target', function () {
assert.strictEqual(didRenew, 1);
});
- it('should not wait for renewal to complete when stale data is disallowed', async function () {
+ it('should wait for renewal to complete when stale data is disallowed', async function () {
let time = 10;
const target = new Target({ maxStale: 0 });