Skip to content

Instantly share code, notes, and snippets.

@markmichon
Last active July 23, 2021 21:13
Show Gist options
  • Save markmichon/801f8783eef2e08f861bacf7dc94cc90 to your computer and use it in GitHub Desktop.
Save markmichon/801f8783eef2e08f861bacf7dc94cc90 to your computer and use it in GitHub Desktop.
Basic CircuitBreaker Node
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