-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
Include endpointUrl in RpcService event listener data #5225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { | ||
BrokenCircuitError, | ||
CircuitState, | ||
EventEmitter as CockatielEventEmitter, | ||
ConsecutiveBreaker, | ||
ExponentialBackoff, | ||
circuitBreaker, | ||
|
@@ -11,13 +12,16 @@ import { | |
} from 'cockatiel'; | ||
import type { | ||
CircuitBreakerPolicy, | ||
Event as CockatielEvent, | ||
IPolicy, | ||
Policy, | ||
RetryPolicy, | ||
} from 'cockatiel'; | ||
|
||
export { CircuitState, BrokenCircuitError, handleAll, handleWhen }; | ||
|
||
export type { CockatielEvent }; | ||
|
||
/** | ||
* The options for `createServicePolicy`. | ||
*/ | ||
|
@@ -76,7 +80,7 @@ export type ServicePolicy = IPolicy & { | |
* never succeeds before the retry policy gives up and before the maximum | ||
* number of consecutive failures has been reached. | ||
*/ | ||
onDegraded: (fn: () => void) => void; | ||
onDegraded: CockatielEvent<void>; | ||
/** | ||
* A function which will be called by the retry policy each time the service | ||
* fails and the policy kicks off a timer to re-run the service. This is | ||
|
@@ -203,27 +207,21 @@ export function createServicePolicy({ | |
}); | ||
const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy); | ||
|
||
const onDegradedListeners: (() => void)[] = []; | ||
const onDegradedEventEmitter = new CockatielEventEmitter<void>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are reusing the Cockatiel built-in EventEmitter class instead of implementing the EventEmitter pattern ourselves. We do this because we want the signature of the |
||
retryPolicy.onGiveUp(() => { | ||
if (circuitBreakerPolicy.state === CircuitState.Closed) { | ||
for (const listener of onDegradedListeners) { | ||
listener(); | ||
} | ||
onDegradedEventEmitter.emit(); | ||
} | ||
}); | ||
retryPolicy.onSuccess(({ duration }) => { | ||
if ( | ||
circuitBreakerPolicy.state === CircuitState.Closed && | ||
duration > degradedThreshold | ||
) { | ||
for (const listener of onDegradedListeners) { | ||
listener(); | ||
} | ||
onDegradedEventEmitter.emit(); | ||
} | ||
}); | ||
const onDegraded = (listener: () => void) => { | ||
onDegradedListeners.push(listener); | ||
}; | ||
const onDegraded = onDegradedEventEmitter.addListener; | ||
|
||
// Every time the retry policy makes an attempt, it executes the circuit | ||
// breaker policy, which executes the service. | ||
|
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.
I've checked the coverage report and all of the lines I added are tested.