The following examples are for Building a Circuit Breaker in Node.js
Last active
April 18, 2021 03:10
-
-
Save markmichon/82f8176a47780a0e168918ded25e7bf9 to your computer and use it in GitHub Desktop.
Circuit Breaker examples
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
class CircuitBreaker { | |
constructor(request, options = {}) { | |
const defaults = { | |
failureThreshold: 3, | |
successThreshold: 2, | |
timeout: 6000 | |
} | |
Object.assign(this, defaults, options, { | |
request, | |
state: "CLOSED", | |
failureCount: 0, | |
successCount: 0, | |
nextAttempt: Date.now() | |
}) | |
} | |
async fire() { | |
if (this.state === "OPEN") { | |
if (this.nextAttempt <= Date.now()) { | |
this.state = "HALF" | |
} else { | |
throw new Error("Breaker is OPEN") | |
} | |
} | |
try { | |
const response = await this.request() | |
return this.success(response) | |
} catch (err) { | |
return this.fail(err) | |
} | |
} | |
success(response) { | |
if (this.state === "HALF") { | |
this.successCount++ | |
if (this.successCount > this.successThreshold) { | |
this.successCount = 0 | |
this.state = "CLOSED" | |
} | |
} | |
this.failureCount = 0 | |
this.status("Success") | |
return response | |
} | |
fail(err) { | |
this.failureCount++ | |
if (this.failureCount >= this.failureThreshold) { | |
this.state = "OPEN" | |
this.nextAttempt = Date.now() + this.timeout | |
} | |
this.status("Failure") | |
return err | |
} | |
status(action) { | |
console.table({ | |
Action: action, | |
Timestamp: Date.now(), | |
Successes: this.successCount, | |
Failures: this.failureCount, | |
"Next State": this.state | |
}) | |
} | |
} | |
module.exports = CircuitBreaker |
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
class CircuitBreaker { | |
constructor(request, options = {}) { | |
const defaults = { | |
failureThreshold: 3, | |
successThreshold: 2, | |
timeout: 6000 | |
} | |
Object.assign(this, defaults, options, { | |
request, | |
state: "CLOSED", | |
failureCount: 0, | |
successCount: 0, | |
nextAttempt: Date.now() | |
}) | |
} | |
async fire() { | |
if (this.state === "OPEN") { | |
if (this.nextAttempt <= Date.now()) { | |
this.state = "HALF" | |
} else { | |
throw new Error("Breaker is OPEN") | |
} | |
} | |
try { | |
const response = await this.request() | |
return this.success(response) | |
} catch (err) { | |
return this.fail(err) | |
} | |
} | |
success(response) { | |
if (this.state === "HALF") { | |
this.successCount++ | |
if (this.successCount > this.successThreshold) { | |
this.close() | |
} | |
} | |
this.failureCount = 0 | |
this.status("Success") | |
return response | |
} | |
fail(err) { | |
this.failureCount++ | |
if (this.failureCount >= this.failureThreshold) { | |
this.open() | |
} | |
this.status("Failure") | |
return err | |
} | |
open() { | |
this.state = "OPEN" | |
this.nextAttempt = Date.now() + this.timeout | |
} | |
close() { | |
this.successCount = 0 | |
this.failureCount = 0 | |
this.state = "CLOSED" | |
} | |
half() { | |
this.state = "HALF" | |
} | |
status(action) { | |
console.table({ | |
Action: action, | |
Timestamp: Date.now(), | |
Successes: this.successCount, | |
Failures: this.failureCount, | |
"Next State": this.state | |
}) | |
} | |
} | |
module.exports = CircuitBreaker |
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
class CircuitBreaker { | |
constructor(request, options = {}) { | |
const defaults = { | |
failureThreshold: 3, | |
successThreshold: 2, | |
timeout: 6000, | |
fallback: null | |
} | |
Object.assign(this, defaults, options, { | |
request, | |
state: "CLOSED", | |
failureCount: 0, | |
successCount: 0, | |
nextAttempt: Date.now() | |
}) | |
} | |
async fire() { | |
if (this.state === "OPEN") { | |
if (this.nextAttempt <= Date.now()) { | |
this.state = "HALF" | |
} else { | |
if (this.fallback) { | |
return this.tryFallback() | |
} | |
throw new Error("Breaker is OPEN") | |
} | |
} | |
try { | |
const response = await this.request() | |
return this.success(response) | |
} catch (err) { | |
return this.fail(err) | |
} | |
} | |
success(response) { | |
if (this.state === "HALF") { | |
this.successCount++ | |
if (this.successCount > this.successThreshold) { | |
this.successCount = 0 | |
this.state = "CLOSED" | |
} | |
} | |
this.failureCount = 0 | |
this.status("Success") | |
return response | |
} | |
fail(err) { | |
this.failureCount++ | |
if (this.failureCount >= this.failureThreshold) { | |
this.open() | |
} | |
this.status("Failure") | |
if (this.fallback) return this.tryFallback() | |
return err | |
} | |
open() { | |
this.state = "OPEN" | |
this.nextAttempt = Date.now() + this.timeout | |
} | |
close() { | |
this.successCount = 0 | |
this.failureCount = 0 | |
this.state = "CLOSED" | |
} | |
half() { | |
this.state = "HALF" | |
} | |
async tryFallback() { | |
console.log("Attempting fallback request") | |
try { | |
const response = await this.fallback() | |
return response | |
} catch (err) { | |
return err | |
} | |
} | |
status(action) { | |
console.table({ | |
Action: action, | |
Timestamp: Date.now(), | |
Successes: this.successCount, | |
Failures: this.failureCount, | |
"Next State": this.state | |
}) | |
} | |
} | |
module.exports = CircuitBreaker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment