-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Domain Fetcher Circuit Breaker & Circuit Breaker Manager #1756
Open
jordevo
wants to merge
13
commits into
master
Choose a base branch
from
feat-domain-fetcher-circuit-breaker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
043a32c
feat(packages/sui-domain): wrap axios http verbs calls with Circuit B…
jordevo b64a94b
feat(packages/sui-domain): adjust circuit breaker code
jordevo 69f8599
feat(packages/sui-domain): changes on fetcher
jordevo 9bc7b1f
feat(packages/sui-domain): implement circuit breaker and circuit brea…
jordevo 09b2ce7
feat(packages/sui-domain): export default circuit breaker manager
jordevo c340f44
Merge branch 'master' into feat-domain-fetcher-circuit-breaker
jordevo 51e7bbc
feat(packages/sui-domain): return requester.call call
jordevo 0c54b7a
Merge branch 'master' into feat-domain-fetcher-circuit-breaker
jordevo 84b55c4
Merge branch 'master' into feat-domain-fetcher-circuit-breaker
jordevo ad09b5e
Merge branch 'master' into feat-domain-fetcher-circuit-breaker
jordevo 05e55e1
refactor(packages/sui-domain): use right getter
jordevo 416357e
refactor(Root): refactor axios fetcher to make it work with circuit b…
jordevo 89e7198
refactor(packages/sui-domain): simplify syntax
jordevo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const CircuitBreakerStates = { | ||
OPENED: 'OPENED', | ||
CLOSED: 'CLOSED', | ||
HALF: 'HALF' | ||
} | ||
|
||
export class CircuitBreaker { | ||
request = null | ||
state = CircuitBreakerStates.CLOSED | ||
failureCount = 0 | ||
failureThreshold = 5 // number of failures to determine when to open the circuit | ||
resetAfter = 50000 | ||
timeout = 5000 // declare request failure if the function takes more than 5 seconds | ||
|
||
constructor({options}) { | ||
this.state = CircuitBreakerStates.CLOSED // allowing requests to go through by default | ||
this.failureCount = 0 | ||
// allow request to go through after the circuit has been opened for resetAfter seconds | ||
// open the circuit again if failure is observed, close the circuit otherwise | ||
this.resetAfter = Date.now() | ||
if (options) { | ||
this.failureThreshold = options.failureThreshold | ||
this.timeout = options.timeout | ||
} else { | ||
this.failureThreshold = 5 | ||
this.timeout = 5000 // in ms | ||
} | ||
} | ||
|
||
async fire(requester, url, options) { | ||
this.request = requester.call(requester, url, options) | ||
|
||
if (this.state === CircuitBreakerStates.OPENED) { | ||
if (this.resetAfter <= Date.now()) { | ||
this.state = CircuitBreakerStates.HALF | ||
} else { | ||
throw new Error('Circuit is in open state right now. Please try again later.') | ||
} | ||
} | ||
try { | ||
const response = await this.request | ||
if (response.status === 200) return this.success(response) | ||
return this.failure(response) | ||
} catch (err) { | ||
return this.failure(err.message) | ||
} | ||
} | ||
|
||
success(response) { | ||
this.failureCount = 0 | ||
if (this.state === CircuitBreakerStates.HALF) { | ||
this.state = CircuitBreakerStates.CLOSED | ||
} | ||
return response | ||
} | ||
|
||
failure(response) { | ||
this.failureCount += 1 | ||
if (this.state === CircuitBreakerStates.HALF || this.failureCount >= this.failureThreshold) { | ||
this.state = CircuitBreakerStates.OPENED | ||
this.resetAfter = Date.now() + this.timeout | ||
} | ||
return response | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/sui-domain/src/circuitBreaker/CircuitBreakerManager.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {CircuitBreaker} from './CircuitBreaker.js' | ||
|
||
export default class CircuitBreakerManager { | ||
activeRequests = {} | ||
|
||
constructor({options}) { | ||
this._options = options | ||
} | ||
|
||
async fire(requester, url, requestOptions) { | ||
const key = `${requestOptions.method}#${url}` | ||
if (!this.activeRequests[key]) this.activeRequests[key] = new CircuitBreaker({options: this._options}) | ||
|
||
return this.activeRequests[key].fire(requester, url, requestOptions) | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these default settings can be overridden by passing an
options
object to theCircuitBreakerManager
constructor