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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package timers
import (
"time"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
)
// Monotonic uses the system's monotonic clock to emit timeouts.
type Monotonic struct {
zero time.Time
timeouts map[time.Duration]<-chan time.Time
}
// MakeMonotonicClock creates a new monotonic clock with a given zero point.
func MakeMonotonicClock(zero time.Time) WallClock {
return &Monotonic{
zero: zero,
}
}
// Zero returns a new Clock reset to the current time.
func (m *Monotonic) Zero() Clock {
z := time.Now()
logging.Base().Debugf("Clock zeroed to %v", z)
return MakeMonotonicClock(z)
}
// TimeoutAt returns a channel that will signal when the duration has elapsed.
func (m *Monotonic) TimeoutAt(delta time.Duration) <-chan time.Time {
if m.timeouts == nil {
m.timeouts = make(map[time.Duration]<-chan time.Time)
}
timeoutCh, ok := m.timeouts[delta]
if ok {
return timeoutCh
}
target := m.zero.Add(delta)
left := target.Sub(time.Now())
if left < 0 {
timeout := make(chan time.Time)
close(timeout)
timeoutCh = timeout
} else {
timeoutCh = time.After(left)
}
m.timeouts[delta] = timeoutCh
return timeoutCh
}
// Encode implements Clock.Encode.
func (m *Monotonic) Encode() []byte {
return protocol.EncodeReflect(m.zero)
}
// Decode implements Clock.Decode.
func (m *Monotonic) Decode(data []byte) (Clock, error) {
var zero time.Time
err := protocol.DecodeReflect(data, &zero)
if err == nil {
logging.Base().Debugf("Clock decoded with zero at %v", zero)
} else {
logging.Base().Errorf("Clock decoded with zero at %v (err: %v)", zero, err)
}
return MakeMonotonicClock(zero), err
}
func (m *Monotonic) String() string {
return time.Time(m.zero).String()
}
// Since returns the time that has passed between the time the clock was last zeroed out and now
func (m *Monotonic) Since() time.Duration {
return time.Since(m.zero)
}
// DeadlineMonitorAt returns a DeadlineMonitor that expires after the provided delta time from zero has passed.
// The method must be called after Zero; otherwise, the context's behavior is undefined.
func (m *Monotonic) DeadlineMonitorAt(at time.Duration) DeadlineMonitor {
return MakeMonotonicDeadlineMonitor(m, at)
}
|