Skip to content

Commit

Permalink
fix doc for retry
Browse files Browse the repository at this point in the history
  • Loading branch information
tichon29 committed Jan 5, 2021
1 parent b2ca314 commit 7c74083
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
1 change: 0 additions & 1 deletion docs/components/global/playground/pg-retry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default {
}
},
mounted () {
this.time = this.$refs.c1.time;
this.modules.push(this.$refs.r1.retry);
}
}
Expand Down
1 change: 0 additions & 1 deletion docs/components/module/RateLimit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export default {
limitForPeriod: this.limitForPeriod,
});
this.ratelimit.on('ratelimit', this.onRatelimit);
//this.ratelimit.on('execute', this.onExecute);
}
}
</script>
Expand Down
63 changes: 39 additions & 24 deletions docs/components/module/Retry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export default {
name: {
type: String,
default: 'Retry'
},
time: {
type: Number,
default: 0
}
},
data () {
Expand All @@ -38,20 +34,26 @@ export default {
retries: 2,
retryInterval: 0,
index: 0,
interval: null,
requests: []
checkStyleDelay: 100,
intervals: [],
requests: [],
isEnded: false
};
},
computed: {
attempts () {
return this.retries + 1;
},
time () {
return this.$parent.time;
}
},
methods: {
cleanup () {
this.requests = new Array(this.retries).fill(0, 0, this.retries);
for (const ref in this.$refs) {
this.$refs[ref][0].style.width = 0;
this.intervals = new Array(this.retries);
for (let i=0;i<=this.retries;i++) {
this.$refs[`progress-${i}`][0].style.width = 0;
}
},
update () {
Expand All @@ -61,34 +63,46 @@ export default {
onExecute () {
this.index = 0;
this.cleanup();
this.interval = setInterval(() => {
this.requests[this.index] += (100 * 100 / this.time);
this.$refs[`progress-${this.index}`][0].style.width = `${this.requests[this.index]}%`;
this.$refs[`progress-${this.index}`][0].style.backgroundColor = 'var(--mollitia-info-color)';
if (this.requests[this.index] >= 100) {
clearInterval(this.interval);
}
}, 100);
this.intervals[0] = setInterval(() => {
this.requests[0] += (100 * this.checkStyleDelay / this.time);
this.computeStyle(0);
}, this.checkStyleDelay);
},
onRetry () {
this.$refs[`progress-${this.index}`][0].style.backgroundColor = 'var(--mollitia-error-color)';
this.index++;
this.requests[this.index] = 0;
this.interval = setInterval(() => {
this.requests[this.index] += (100 * 100 / this.time);
this.$refs[`progress-${this.index}`][0].style.width = `${this.requests[this.index]}%`;
this.$refs[`progress-${this.index}`][0].style.backgroundColor = 'var(--mollitia-info-color)';
if (this.requests[this.index] >= 100) {
clearInterval(this.interval);
}
}, 100);
this.intervals[this.index] = setInterval(() => {
this.requests[this.index] += (100 * this.checkStyleDelay / this.time);
this.computeStyle(this.index);
}, this.checkStyleDelay);
},
onEnd (success) {
this.isEnded = true;
if (!success) {
this.$refs[`progress-${this.index}`][0].style.backgroundColor = 'var(--mollitia-error-color)';
}
},
computeStyle (index) {
if (index > 0) {
for (let i = 0; i < index; i++) {
this.$refs[`progress-${i}`][0].style.width = '100%';
clearInterval(this.intervals[i]);
}
}
if (this.requests[index] >= 100) {
this.$refs[`progress-${index}`][0].style.width = '100%';
clearInterval(this.intervals[index]);
if (this.isEnded) {
this.$refs[`progress-${index}`][0].style.backgroundColor = 'var(--mollitia-info-color)';
}
} else {
this.$refs[`progress-${index}`][0].style.width = `${this.requests[index]}%`;
this.$refs[`progress-${index}`][0].style.backgroundColor = 'var(--mollitia-info-color)';
}
}
},
created () {
this.requests = new Array(this.attempts).fill(0, 0, this.attempts);
this.retry = new this.$mollitia.Retry({
Expand All @@ -100,6 +114,7 @@ export default {
},
destroyed () {
clearInterval(this.interval);
clearInterval(this.intervalRetry);
this.retry.off('execute', this.onExecute);
this.retry.off('retry', this.onRetry);
}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/map-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CacheItem<T> {

export class MapCache {
// Private Attributes
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private map: Map<any, any>;
// Constructor
constructor () {
Expand Down
4 changes: 4 additions & 0 deletions src/module/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module, ModuleOptions } from '.';
import { Circuit, CircuitFunction } from '../circuit';
import { delay } from '../helpers/time';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type RetryCallback = (err: any) => boolean|number;

/**
Expand Down Expand Up @@ -49,12 +50,14 @@ export class Retry extends Module {
this.onRejection = options?.onRejection || (() => true);
}
// Public Methods
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async execute<T> (circuit: Circuit, promise: CircuitFunction, ...params: any[]): Promise<T> {
const _exec = this._promiseRetry<T>(circuit, this.attempts + 1, promise, ...params);
this.emit('execute', circuit, _exec);
return _exec;
}
// Private Methods
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async _promiseRetry<T> (circuit: Circuit, attempts: number, promise: CircuitFunction, ...params: any[]): Promise<T> {
if (attempts - 1 === 0) {
this.emit('retry', circuit, this.attempts);
Expand All @@ -65,6 +68,7 @@ export class Retry extends Module {
this.emit('retry', circuit, this.attempts - attempts + 1);
this.logger?.debug(`${circuit.name}/${this.name} - Retry: (${this.attempts - attempts + 1}/${this.attempts})`);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return promise(...params).catch(async (err: any) => {
const shouldRetry = this.onRejection(err);
const interval = (typeof shouldRetry === 'number') ? shouldRetry : this.interval;
Expand Down
2 changes: 2 additions & 0 deletions src/module/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export class Timeout extends Module {
this.delay = options?.delay ? options?.delay : 60000;
}
// Public Methods
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async execute<T> (circuit: Circuit, promise: CircuitFunction, ...params: any[]): Promise<T> {
const _exec = this._promiseTimeout<T>(circuit, this.delay, promise, ...params);
this.emit('execute', circuit, _exec);
return _exec;
}
// Private Methods
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async _promiseTimeout<T> (circuit: Circuit, time: number, promise: CircuitFunction, ...params: any[]): Promise<T> {
let timeout: number;
if (time !== 0 && time !== Infinity) {
Expand Down

0 comments on commit 7c74083

Please sign in to comment.