-
Notifications
You must be signed in to change notification settings - Fork 48
/
Waiter.go
48 lines (38 loc) · 1017 Bytes
/
Waiter.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
39
40
41
42
43
44
45
46
47
48
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.10.
package commonruntime
import (
"time"
)
//============================================
type WAITER struct {
start time.Time
Sleep int // sec, default = 1
Timeout int // sec, default = 120
}
//============================================
func NewWaiter(sleep int, timeout int) *WAITER {
var waiter = new(WAITER)
waiter.start = time.Now()
waiter.Sleep = 1
waiter.Timeout = 120
if sleep > 1 {
waiter.Sleep = sleep
}
if timeout > waiter.Sleep {
waiter.Timeout = timeout
}
return waiter
}
func (waiter *WAITER)Wait() bool {
elapsed := time.Since(waiter.start)
if int(elapsed.Seconds()) < waiter.Timeout {
time.Sleep(time.Duration(waiter.Sleep) * time.Second)
return true // more waiting
}
return false // stop waiting
}