Skip to content

Commit

Permalink
fix undefined options
Browse files Browse the repository at this point in the history
  • Loading branch information
cadgerfeast committed Jan 7, 2021
1 parent e5a367a commit fcdbbbc
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 23 deletions.
18 changes: 9 additions & 9 deletions src/module/breaker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,20 @@ export abstract class SlidingWindowBreaker<T> extends Module {

constructor (options?: SlidingWindowBreakerOptions) {
super(options);
this.state = options?.state || BreakerState.CLOSED;
this.openStateDelay = options?.openStateDelay || 60 * 1000;
this.halfOpenStateMaxDelay = options?.halfOpenStateMaxDelay || 0;
this.state = (options?.state !== undefined) ? options.state : BreakerState.CLOSED;
this.openStateDelay = (options?.openStateDelay !== undefined) ? options.openStateDelay : 60 * 1000;
this.halfOpenStateMaxDelay = (options?.halfOpenStateMaxDelay !== undefined) ? options.halfOpenStateMaxDelay : 0;
if (this.state === BreakerState.OPENED) {
this.setHalfDelay();
} 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.permittedNumberOfCallsInHalfOpenState = options?.permittedNumberOfCallsInHalfOpenState ? options?.permittedNumberOfCallsInHalfOpenState : 2;
this.slidingWindowSize = (options?.slidingWindowSize !== undefined) ? options.slidingWindowSize : 10;
this.minimumNumberOfCalls = (options?.minimumNumberOfCalls !== undefined) ? options.minimumNumberOfCalls : 10;
this.failureRateThreshold = (options?.failureRateThreshold !== undefined) ? options.failureRateThreshold : 50;
this.slowCallDurationThreshold = (options?.slowCallDurationThreshold !== undefined) ? options.slowCallDurationThreshold : 60000;
this.slowCallRateThreshold = (options?.slowCallRateThreshold !== undefined) ? options?.slowCallRateThreshold : 100;
this.permittedNumberOfCallsInHalfOpenState = (options?.permittedNumberOfCallsInHalfOpenState !== undefined) ? options.permittedNumberOfCallsInHalfOpenState : 2;
this.nbCallsInHalfOpenedState = 0;
this.callsInHalfOpenedState = [];
this.callsInClosedState = [];
Expand Down
2 changes: 1 addition & 1 deletion src/module/breaker/sliding-count-breaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SlidingWindowBreaker, SlidingWindowBreakerOptions, SlidingWindowRequest
export class SlidingCountBreaker extends SlidingWindowBreaker<SlidingWindowRequestResult> {
constructor(options?: SlidingWindowBreakerOptions) {
super(options);
this.slidingWindowSize = options?.slidingWindowSize ? options?.slidingWindowSize : 10;
this.slidingWindowSize = (options?.slidingWindowSize !== undefined) ? options.slidingWindowSize : 10;
if (this.slidingWindowSize < this.minimumNumberOfCalls) {
this.slidingWindowSize = this.minimumNumberOfCalls;
}
Expand Down
2 changes: 1 addition & 1 deletion src/module/breaker/sliding-time-breaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class SlidingTimeBreaker extends SlidingWindowBreaker<SlidingTimeElem> {

constructor(options?: SlidingWindowBreakerOptions) {
super(options);
this.slidingWindowSize = options?.slidingWindowSize ? options?.slidingWindowSize : 60;
this.slidingWindowSize = (options?.slidingWindowSize !== undefined) ? options.slidingWindowSize : 60;
this.maxSize = 1000;
}

Expand Down
6 changes: 3 additions & 3 deletions src/module/bulkhead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export class Bulkhead extends Module {
// Constructor
constructor (options?: BulkheadOptions) {
super(options);
this.concurrentSize = options?.concurrentSize ? options?.concurrentSize : 10;
this.queueSize = options?.queueSize ? options?.queueSize : 10;
this.maxQueueWait = options?.maxQueueWait ? options?.maxQueueWait : 60000;
this.concurrentSize = (options?.concurrentSize !== undefined) ? options.concurrentSize : 10;
this.queueSize = (options?.queueSize !== undefined) ? options.queueSize : 10;
this.maxQueueWait = (options?.maxQueueWait !== undefined) ? options.maxQueueWait : 60000;
this.concurrentBuffer = [];
this.queueBuffer = [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/module/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export class Cache extends Module {
// Constructor
constructor (options?: CacheOptions) {
super(options);
this.ttl = options?.ttl ? options?.ttl : 6000; // 1 minute
this.getInformationFromCache = options?.getInformationFromCache ? options?.getInformationFromCache : false;
this.ttl = (options?.ttl !== undefined) ? options.ttl : 6000; // 1 minute
this.getInformationFromCache = (options?.getInformationFromCache !== undefined) ? options.getInformationFromCache : false;
this._cacheInterval = null;
this._cacheClearInterval = 0;
this.cacheClearInterval = options?.cacheClearInterval ? options?.cacheClearInterval : 900000; // 15 minutes
this.cacheClearInterval = (options?.cacheClearInterval !== undefined) ? options.cacheClearInterval : 900000; // 15 minutes
this.cache = new MapCache();
}
// Public Methods
Expand Down
4 changes: 2 additions & 2 deletions src/module/ratelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class Ratelimit extends Module {
// Constructor
constructor (options?: RatelimitOptions) {
super(options);
this.limitPeriod = options?.limitPeriod ? options?.limitPeriod : 0;
this.limitForPeriod = options?.limitForPeriod ? options?.limitForPeriod : Infinity;
this.limitPeriod = (options?.limitPeriod !== undefined) ? options.limitPeriod : 0;
this.limitForPeriod = (options?.limitForPeriod !== undefined) ? options.limitForPeriod : Infinity;
this.requestsTime = [];
}
// Public Methods
Expand Down
4 changes: 2 additions & 2 deletions src/module/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class Retry extends Module {
// Constructor
constructor (options?: RetryOptions) {
super(options);
this.attempts = options?.attempts ? options?.attempts : 2;
this.interval = options?.interval ? options?.interval : 0;
this.attempts = (options?.attempts !== undefined) ? options.attempts : 2;
this.interval = (options?.interval !== undefined) ? options.interval : 0;
this.onRejection = options?.onRejection || (() => true);
}
// Public Methods
Expand Down
2 changes: 1 addition & 1 deletion src/module/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Timeout extends Module {
// Constructor
constructor (options?: TimeoutOptions) {
super(options);
this.delay = options?.delay ? options?.delay : 60000;
this.delay = (options?.delay !== undefined) ? options.delay : 60000;
}
// Public Methods
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 1 addition & 1 deletion test/unit/module/breaker/sliding-time-breaker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Sliding Count Breaker', () => {
await expect(circuit.fn(successAsync).execute('dummy')).resolves.toEqual('dummy');
await expect(circuit.fn(failureAsync).execute('dummy')).rejects.toEqual('dummy');
expect(slidingTimeBreaker.state).toEqual(Mollitia.BreakerState.CLOSED);
await expect(circuit.fn(failureAsync).execute('dummy', 100)).rejects.toEqual('dummy');
await expect(circuit.fn(failureAsync).execute('dummy', 150)).rejects.toEqual('dummy');
expect(slidingTimeBreaker.state).toEqual(Mollitia.BreakerState.CLOSED);
await expect(circuit.fn(failureAsync).execute('dummy')).rejects.toEqual('dummy');
expect(slidingTimeBreaker.state).toEqual(Mollitia.BreakerState.OPENED);
Expand Down

0 comments on commit fcdbbbc

Please sign in to comment.