Skip to content

Commit

Permalink
add support of onError for circuit Breaker
Browse files Browse the repository at this point in the history
  • Loading branch information
tichon29 committed Nov 23, 2020
1 parent a85781f commit 7ae4bfd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/module/breaker/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Module, ModuleOptions } from '..';
import { Circuit } from '../../circuit';

type ErrorCallback = (err: any) => boolean;

export class BreakerError extends Error {
constructor(message: string) {
super(message);
Expand Down Expand Up @@ -30,6 +32,7 @@ export interface SlidingWindowBreakerOptions extends ModuleOptions {
slowCallRateThreshold?: number;
slowCallDurationThreshold?: number;
permittedNumberOfCallsInHalfOpenState?: number;
onError?: ErrorCallback;
}

export enum SlidingWindowRequestResult {
Expand All @@ -53,6 +56,7 @@ export abstract class SlidingWindowBreaker<T> extends Module implements BreakerN
private openTimeout = 0;
private nbCallsInHalfOpenedState: number;
private callsInHalfOpenedState: SlidingWindowRequestResult[];
public onError: ErrorCallback;

constructor (options?: SlidingWindowBreakerOptions) {
super(options);
Expand All @@ -73,6 +77,7 @@ export abstract class SlidingWindowBreaker<T> extends Module implements BreakerN
this.nbCallsInHalfOpenedState = 0;
this.callsInHalfOpenedState = [];
this.callsInClosedState = [];
this.onError = options?.onError || (() => true);
}

private reinitializeCounters (): void {
Expand Down Expand Up @@ -143,7 +148,9 @@ export abstract class SlidingWindowBreaker<T> extends Module implements BreakerN
return {requestResult: requestResp, response: res};
})
.catch((err: any) => {
return {requestResult: SlidingWindowRequestResult.FAILURE, response: err};
const shouldReportFailure = this.onError(err);
const returnedValue = shouldReportFailure ? SlidingWindowRequestResult.FAILURE : SlidingWindowRequestResult.SUCCESS;
return {requestResult: returnedValue, response: err};
});
}

Expand Down
29 changes: 29 additions & 0 deletions test/unit/module/breaker/sliding-count-breaker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,33 @@ describe('Sliding Count Breaker', () => {
await expect(circuit.fn(failureAsync).execute('dummy')).rejects.toEqual('dummy');
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.OPENED);
});
it('No switch to Open when failures but failure reported as success', async () => {
const slidingCountBreaker = new Mollitia.SlidingCountBreaker({
slidingWindowSize: 2,
minimumNumberOfCalls: 2,
failureRateThreshold: 60,
openStateDelay: 20,
onError: (err) => {
if (err === 'credentials-issue') {
return false;
} else {
return true;
}
}
});
const circuit = new Mollitia.Circuit({
options: {
modules: [
slidingCountBreaker
]
}
});
await circuit.fn(failureAsync).execute('credentials-issue');
await circuit.fn(failureAsync).execute('credentials-issue');
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.CLOSED);
await circuit.fn(failureAsync).execute('real-issue').catch(()=>{ });
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.CLOSED);
await circuit.fn(failureAsync).execute('real-issue').catch(()=>{ });
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.OPENED);
});
});

0 comments on commit 7ae4bfd

Please sign in to comment.