import debug from 'debug' ; import { IRetryProps } from './types' ; const MAX_RETRIES = 10 ; const dlog = debug ( 'dubbo-server:retry~' ) ; export class Retry { private retryNum : number ; private readonly delay : number ; private readonly maxRetryCount : number ; private readonly retry : Function ; private readonly end : Function ; constructor ( props ) { this . maxRetryCount = props . maxRetry || MAX_RETRIES ; this . retryNum = this . maxRetryCount ; this . retry = props . retry ; this . end = props . end ; this . delay = props . delay || 1000 ; dlog ( `init props: %j` , { delay : this . delay } ) ; } static from ( props ) { return new Retry ( props ) ; } start ( ) { dlog ( `starting retry, current retry num:%d` , this . retryNum ) ; if ( this . retryNum === 0 ) { this . end ( ) ; return ; } setTimeout ( ( ) => { this . retry ( ) ; this . retryNum -- ; } , this . delay ) ; } reset ( ) { dlog ( 'reset' ) ; this . retryNum = this . maxRetryCount ; } }