-
-
Save thomasbabuj/1e6ab4aa6cb3f3f377caec4eb48da5b7 to your computer and use it in GitHub Desktop.
Basic CircuitBreaker Node
This file contains 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) { | |
this.request = request | |
this.state = "CLOSED" | |
this.failureThreshold = 3 | |
this.failureCount = 0 | |
this.successThreshold = 2 | |
this.successCount = 0 | |
this.timeout = 6000 | |
this.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment