unleash-client
Advanced tools
Comparing version 5.1.0 to 5.2.0
@@ -1,1 +0,1 @@ | ||
{ "name": "unleash-client-node", "version": "5.1.0", "sdkVersion": "unleash-client-node:5.1.0" } | ||
{ "name": "unleash-client-node", "version": "5.2.0", "sdkVersion": "unleash-client-node:5.2.0" } |
@@ -53,2 +53,3 @@ /// <reference types="node" /> | ||
private metricsJitter; | ||
private failures; | ||
private disabled; | ||
@@ -64,2 +65,4 @@ private url; | ||
private getAppliedJitter; | ||
getFailures(): number; | ||
getInterval(): number; | ||
private startTimer; | ||
@@ -69,3 +72,6 @@ start(): void; | ||
registerInstance(): Promise<boolean>; | ||
configurationError(url: string, statusCode: number): void; | ||
backoff(url: string, statusCode: number): void; | ||
sendMetrics(): Promise<void>; | ||
reduceBackoff(): void; | ||
assertBucket(name: string): void; | ||
@@ -72,0 +78,0 @@ count(name: string, enabled: boolean): void; |
@@ -12,2 +12,3 @@ "use strict"; | ||
super(); | ||
this.failures = 0; | ||
this.disabled = disableMetrics; | ||
@@ -31,4 +32,17 @@ this.metricsInterval = metricsInterval; | ||
} | ||
getFailures() { | ||
return this.failures; | ||
} | ||
getInterval() { | ||
if (this.metricsInterval === 0) { | ||
return 0; | ||
} | ||
else { | ||
return this.metricsInterval + | ||
(this.failures * this.metricsInterval) + | ||
this.getAppliedJitter(); | ||
} | ||
} | ||
startTimer() { | ||
if (this.disabled) { | ||
if (this.disabled || this.getInterval() === 0) { | ||
return; | ||
@@ -38,3 +52,3 @@ } | ||
this.sendMetrics(); | ||
}, this.metricsInterval + this.getAppliedJitter()); | ||
}, this.getInterval()); | ||
if (process.env.NODE_ENV !== 'test' && typeof this.timer.unref === 'function') { | ||
@@ -45,3 +59,3 @@ this.timer.unref(); | ||
start() { | ||
if (typeof this.metricsInterval === 'number' && this.metricsInterval > 0) { | ||
if (this.metricsInterval > 0) { | ||
this.startTimer(); | ||
@@ -88,2 +102,13 @@ this.registerInstance(); | ||
} | ||
configurationError(url, statusCode) { | ||
this.emit(events_2.UnleashEvents.Warn, `${url} returning ${statusCode}, stopping metrics`); | ||
this.metricsInterval = 0; | ||
this.stop(); | ||
} | ||
backoff(url, statusCode) { | ||
this.failures = Math.min(10, this.failures + 1); | ||
// eslint-disable-next-line max-len | ||
this.emit(events_2.UnleashEvents.Warn, `${url} returning ${statusCode}. Backing off to ${this.failures} times normal interval`); | ||
this.startTimer(); | ||
} | ||
async sendMetrics() { | ||
@@ -111,13 +136,18 @@ if (this.disabled) { | ||
}); | ||
this.startTimer(); | ||
if (res.status === 404) { | ||
this.emit(events_2.UnleashEvents.Warn, `${url} returning 404, stopping metrics`); | ||
this.stop(); | ||
} | ||
if (!res.ok) { | ||
if (res.status === 404 || res.status === 403 || res.status == 401) { | ||
this.configurationError(url, res.status); | ||
} | ||
else if (res.status === 429 || | ||
res.status === 500 || | ||
res.status === 502 || | ||
res.status === 503 || | ||
res.status === 504) { | ||
this.backoff(url, res.status); | ||
} | ||
this.restoreBucket(payload.bucket); | ||
this.emit(events_2.UnleashEvents.Warn, `${url} returning ${res.status}`, await res.text()); | ||
} | ||
else { | ||
this.emit(events_2.UnleashEvents.Sent, payload); | ||
this.reduceBackoff(); | ||
} | ||
@@ -131,2 +161,6 @@ } | ||
} | ||
reduceBackoff() { | ||
this.failures = Math.max(0, this.failures - 1); | ||
this.startTimer(); | ||
} | ||
assertBucket(name) { | ||
@@ -202,7 +236,7 @@ if (this.disabled) { | ||
const { toggles } = bucket; | ||
Object.keys(toggles).forEach(toggleName => { | ||
Object.keys(toggles).forEach((toggleName) => { | ||
const toggle = toggles[toggleName]; | ||
this.increaseCounter(toggleName, true, toggle.yes); | ||
this.increaseCounter(toggleName, false, toggle.no); | ||
Object.keys(toggle.variants).forEach(variant => { | ||
Object.keys(toggle.variants).forEach((variant) => { | ||
this.increaseVariantCounter(toggleName, variant, toggle.variants[variant]); | ||
@@ -209,0 +243,0 @@ }); |
@@ -41,2 +41,3 @@ /// <reference types="node" /> | ||
private headers?; | ||
private failures; | ||
private customHeadersFunction?; | ||
@@ -67,2 +68,9 @@ private timeout?; | ||
private convertToMap; | ||
getFailures(): number; | ||
nextFetch(): number; | ||
private backoff; | ||
private countSuccess; | ||
private configurationError; | ||
private recoverableError; | ||
private handleErrorCases; | ||
fetch(): Promise<void>; | ||
@@ -69,0 +77,0 @@ mergeTagsToStringArray(tags: Array<TagFilter>): Array<string>; |
@@ -11,2 +11,3 @@ "use strict"; | ||
super(); | ||
this.failures = 0; | ||
this.stopped = false; | ||
@@ -133,2 +134,66 @@ this.ready = false; | ||
} | ||
getFailures() { | ||
return this.failures; | ||
} | ||
nextFetch() { | ||
return this.refreshInterval + this.failures * this.refreshInterval; | ||
} | ||
backoff() { | ||
this.failures = Math.min(this.failures + 1, 10); | ||
return this.nextFetch(); | ||
} | ||
countSuccess() { | ||
this.failures = Math.max(this.failures - 1, 0); | ||
return this.nextFetch(); | ||
} | ||
// Emits correct error message based on what failed, | ||
// and returns 0 as the next fetch interval (stop polling) | ||
configurationError(url, statusCode) { | ||
this.failures += 1; | ||
if (statusCode === 404) { | ||
this.emit(events_2.UnleashEvents.Error, new Error( | ||
// eslint-disable-next-line max-len | ||
`${url} responded NOT_FOUND (404) which means your API url most likely needs correction. Stopping refresh of toggles`)); | ||
} | ||
else if (statusCode === 401 || statusCode === 403) { | ||
this.emit(events_2.UnleashEvents.Error, new Error( | ||
// eslint-disable-next-line max-len | ||
`${url} responded ${statusCode} which means your API key is not allowed to connect. Stopping refresh of toggles`)); | ||
} | ||
return 0; | ||
} | ||
// We got a status code we know what to do with, so will log correct message | ||
// and return the new interval. | ||
recoverableError(url, statusCode) { | ||
let nextFetch = this.backoff(); | ||
if (statusCode === 429) { | ||
this.emit(events_2.UnleashEvents.Warn, | ||
// eslint-disable-next-line max-len | ||
`${url} responded TOO_MANY_CONNECTIONS (429). Backing off`); | ||
} | ||
else if (statusCode === 500 || | ||
statusCode === 502 || | ||
statusCode === 503 || | ||
statusCode === 504) { | ||
this.emit(events_2.UnleashEvents.Warn, `${url} responded ${statusCode}. Backing off`); | ||
} | ||
return nextFetch; | ||
} | ||
handleErrorCases(url, statusCode) { | ||
if (statusCode === 401 || statusCode === 403 || statusCode === 404) { | ||
return this.configurationError(url, statusCode); | ||
} | ||
else if (statusCode === 429 || | ||
statusCode === 500 || | ||
statusCode === 502 || | ||
statusCode === 503 || | ||
statusCode === 504) { | ||
return this.recoverableError(url, statusCode); | ||
} | ||
else { | ||
const error = new Error(`Response was not statusCode 2XX, but was ${statusCode}`); | ||
this.emit(events_2.UnleashEvents.Error, error); | ||
return this.refreshInterval; | ||
} | ||
} | ||
async fetch() { | ||
@@ -162,7 +227,4 @@ if (this.stopped || !(this.refreshInterval > 0)) { | ||
} | ||
else if (!res.ok) { | ||
const error = new Error(`Response was not statusCode 2XX, but was ${res.status}`); | ||
this.emit(events_2.UnleashEvents.Error, error); | ||
} | ||
else { | ||
else if (res.ok) { | ||
nextFetch = this.countSuccess(); | ||
try { | ||
@@ -182,2 +244,5 @@ const data = await res.json(); | ||
} | ||
else { | ||
nextFetch = this.handleErrorCases(url, res.status); | ||
} | ||
} | ||
@@ -184,0 +249,0 @@ catch (err) { |
{ | ||
"name": "unleash-client", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "Unleash Client for Node", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
224404
2860