forked from yonglehou/dn-phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetryTimer.cs
79 lines (71 loc) · 1.88 KB
/
RetryTimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Timers;
namespace Phoenix
{
//// Creates a timer that accepts a `timerCalc` function to perform
//// calculated timeout retries, such as exponential backoff.
////
//// ## Examples
////
//// let reconnectTimer = new Timer(() => this.connect(), function(tries){
//// return [1000, 5000, 10000][tries - 1] || 10000
//// })
//// reconnectTimer.setTimeout() // fires after 1000
//// reconnectTimer.setTimeout() // fires after 5000
//// reconnectTimer.reset()
//// reconnectTimer.setTimeout() // fires after 1000
////
public class RetryTimer
{
private Action _callback;
private Func<int, int> _timerCalc;
private Timer _timer;
private int _numTries;
// constructor(callback, timerCalc){
// this.callback = callback
// this.timerCalc = timerCalc
// this.timer = null
// this.tries = 0
// }
public RetryTimer(Action callback, Func<int, int> timerCalc)
{
_callback = callback;
_timerCalc = timerCalc;
_timer = new Timer()
{
AutoReset = false
};
_timer.Elapsed += OnElapsed;
_numTries = 0;
}
// reset(){
// this.tries = 0
// clearTimeout(this.timer)
// }
public void Reset()
{
_numTries = 0;
_timer.Stop();
}
// // Cancels any previous setTimeout and schedules callback
// setTimeout(){
// clearTimeout(this.timer)
// this.timer = setTimeout(() => {
// this.tries = this.tries + 1
// this.callback()
// }, this.timerCalc(this.tries + 1))
// }
public void SetTimeout()
{
_timer.Stop();
var wait = _timerCalc(_numTries + 1);
_timer.Interval = wait;
_timer.Start();
}
private void OnElapsed(object sender, ElapsedEventArgs e)
{
_numTries += 1;
_callback();
}
}
}