Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cadgerfeast committed Dec 15, 2020
1 parent 30e485d commit 3eec0dc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/content/api/module/breaker/sliding/count.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ For example, if the count window size is 10, the circular array has always 10 me
| Name | Description | Params |
|:-----------|:-------------------------------------|:--------------- ---------------|
| `execute` | Called when the module is executed. | `Mollitia.Circuit` **circuit** |
| `state-changed` | Called when the breaker state changes. | `Mollitia.BreakerState` **state** |
1 change: 1 addition & 0 deletions docs/content/api/module/breaker/sliding/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ For example, if the count window size is 10000, the circular array stores the re
| Name | Description | Params |
|:-----------|:-------------------------------------|:--------------- ---------------|
| `execute` | Called when the module is executed. | `Mollitia.Circuit` **circuit** |
| `state-changed` | Called when the breaker state changes. | `Mollitia.BreakerState` **state** |
2 changes: 1 addition & 1 deletion src/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class Circuit extends EventEmitter {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async execute<T> (...params: any[]): Promise<T> {
let _exec;
let _exec: Promise<T>;
if (this.modules.length) {
if (this.modules.length > 1) {
const args = [];
Expand Down
16 changes: 8 additions & 8 deletions src/module/breaker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ export abstract class SlidingWindowBreaker<T> extends Module {
} else if (this.state === BreakerState.HALF_OPENED) {
this.setOpenDelay();
}
this.slidingWindowSize = options?.slidingWindowSize ? options?.slidingWindowSize : 10;
this.minimumNumberOfCalls = options?.minimumNumberOfCalls ? options?.minimumNumberOfCalls : 10;
this.failureRateThreshold = (options?.failureRateThreshold ? options?.failureRateThreshold : 50);
this.slowCallDurationThreshold = options?.slowCallDurationThreshold ? options?.slowCallDurationThreshold : 60000;
this.slowCallRateThreshold = (options?.slowCallRateThreshold ? options?.slowCallRateThreshold : 100);
this.slidingWindowSize = options?.slidingWindowSize ? options?.slidingWindowSize : 10;
this.minimumNumberOfCalls = options?.minimumNumberOfCalls ? options?.minimumNumberOfCalls : 10;
this.failureRateThreshold = (options?.failureRateThreshold ? options?.failureRateThreshold : 50);
this.slowCallDurationThreshold = options?.slowCallDurationThreshold ? options?.slowCallDurationThreshold : 60000;
this.slowCallRateThreshold = (options?.slowCallRateThreshold ? options?.slowCallRateThreshold : 100);
this.permittedNumberOfCallsInHalfOpenState = options?.permittedNumberOfCallsInHalfOpenState ? options?.permittedNumberOfCallsInHalfOpenState : 2;
this.nbCallsInHalfOpenedState = 0;
this.callsInHalfOpenedState = [];
Expand Down Expand Up @@ -290,7 +290,7 @@ export abstract class SlidingWindowBreaker<T> extends Module {
this.state = BreakerState.OPENED;
this.setHalfDelay();
this.onOpened();
this.emit('stateChanged');
this.emit('state-changed', this.state);
}
}
public halfOpen (): void {
Expand All @@ -299,15 +299,15 @@ export abstract class SlidingWindowBreaker<T> extends Module {
this.state = BreakerState.HALF_OPENED;
this.setOpenDelay();
this.onHalfOpened();
this.emit('stateChanged');
this.emit('state-changed', this.state);
}
}
public close (): void {
if (this.state !== BreakerState.CLOSED) {
this.clearHalfOpenTimeout();
this.state = BreakerState.CLOSED;
this.onClosed();
this.emit('stateChanged');
this.emit('state-changed', this.state);
}
}
private setHalfDelay (): void {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/module/breaker/sliding-count-breaker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ describe('Sliding Count Breaker', () => {
]
}
});
await circuit.fn(failureAsync).execute('credentials-issue').catch(()=>{ });
await circuit.fn(failureAsync).execute('credentials-issue').catch(()=>{ });
await circuit.fn(failureAsync).execute('credentials-issue').catch(()=>{ return; });
await circuit.fn(failureAsync).execute('credentials-issue').catch(()=>{ return; });
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.CLOSED);
await circuit.fn(failureAsync).execute('real-issue').catch(()=>{ });
await circuit.fn(failureAsync).execute('real-issue').catch(()=>{ return; });
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.CLOSED);
await circuit.fn(failureAsync).execute('real-issue').catch(()=>{ });
await circuit.fn(failureAsync).execute('real-issue').catch(()=>{ return; });
expect(slidingCountBreaker.state).toEqual(Mollitia.BreakerState.OPENED);
});
});

0 comments on commit 3eec0dc

Please sign in to comment.