1515 *
1616 */
1717
18+ import { LogVerbosity } from './constants' ;
19+ import * as logging from './logging' ;
20+
21+ const TRACER_NAME = 'backoff' ;
22+
1823const INITIAL_BACKOFF_MS = 1000 ;
1924const BACKOFF_MULTIPLIER = 1.6 ;
2025const MAX_BACKOFF_MS = 120000 ;
@@ -84,7 +89,12 @@ export class BackoffTimeout {
8489 */
8590 private endTime : Date = new Date ( ) ;
8691
92+ private id : number ;
93+
94+ private static nextId = 0 ;
95+
8796 constructor ( private callback : ( ) => void , options ?: BackoffOptions ) {
97+ this . id = BackoffTimeout . getNextId ( ) ;
8898 if ( options ) {
8999 if ( options . initialDelay ) {
90100 this . initialDelay = options . initialDelay ;
@@ -99,18 +109,29 @@ export class BackoffTimeout {
99109 this . maxDelay = options . maxDelay ;
100110 }
101111 }
112+ this . trace ( 'constructed initialDelay=' + this . initialDelay + ' multiplier=' + this . multiplier + ' jitter=' + this . jitter + ' maxDelay=' + this . maxDelay ) ;
102113 this . nextDelay = this . initialDelay ;
103114 this . timerId = setTimeout ( ( ) => { } , 0 ) ;
104115 clearTimeout ( this . timerId ) ;
105116 }
106117
118+ private static getNextId ( ) {
119+ return this . nextId ++ ;
120+ }
121+
122+ private trace ( text : string ) {
123+ logging . trace ( LogVerbosity . DEBUG , TRACER_NAME , '{' + this . id + '} ' + text ) ;
124+ }
125+
107126 private runTimer ( delay : number ) {
127+ this . trace ( 'runTimer(delay=' + delay + ')' ) ;
108128 this . endTime = this . startTime ;
109129 this . endTime . setMilliseconds (
110130 this . endTime . getMilliseconds ( ) + delay
111131 ) ;
112132 clearTimeout ( this . timerId ) ;
113133 this . timerId = setTimeout ( ( ) => {
134+ this . trace ( 'timer fired' ) ;
114135 this . callback ( ) ;
115136 this . running = false ;
116137 } , delay ) ;
@@ -123,6 +144,7 @@ export class BackoffTimeout {
123144 * Call the callback after the current amount of delay time
124145 */
125146 runOnce ( ) {
147+ this . trace ( 'runOnce()' ) ;
126148 this . running = true ;
127149 this . startTime = new Date ( ) ;
128150 this . runTimer ( this . nextDelay ) ;
@@ -140,6 +162,7 @@ export class BackoffTimeout {
140162 * again.
141163 */
142164 stop ( ) {
165+ this . trace ( 'stop()' ) ;
143166 clearTimeout ( this . timerId ) ;
144167 this . running = false ;
145168 }
@@ -149,6 +172,7 @@ export class BackoffTimeout {
149172 * retroactively apply that reset to the current timer.
150173 */
151174 reset ( ) {
175+ this . trace ( 'reset() running=' + this . running ) ;
152176 this . nextDelay = this . initialDelay ;
153177 if ( this . running ) {
154178 const now = new Date ( ) ;
0 commit comments