forked from dryark/ios_remote_provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_backoff.go
38 lines (33 loc) · 834 Bytes
/
proc_backoff.go
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
package main
import "time"
type Backoff struct {
fails int
start time.Time
elapsedSeconds float64
}
func ( self *Backoff ) markStart() {
self.start = time.Now()
}
func ( self *Backoff ) markEnd() ( float64 ) {
elapsed := time.Since( self.start )
seconds := elapsed.Seconds()
self.elapsedSeconds = seconds
return seconds
}
func ( self *Backoff ) wait() {
sleeps := []int{ 0, 0, 2, 5, 10 }
numSleeps := len( sleeps )
if self.elapsedSeconds < 20 {
self.fails = self.fails + 1
index := self.fails
if index >= numSleeps {
index = numSleeps - 1
}
sleepLen := sleeps[ index ]
if sleepLen != 0 {
time.Sleep( time.Second * time.Duration( sleepLen ) )
}
} else {
self.fails = 0
}
}