-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatcher.go
51 lines (42 loc) · 1.42 KB
/
stopwatcher.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
49
50
51
package scold
import (
"time"
"github.com/jonboulle/clockwork"
)
// Stopwatcher abstracts away the concept of the stopwatch.
// At any time, one can look up the elapsed time. Additionally, one
// can be notified when the time is up.
type Stopwatcher interface {
Now() time.Time
Elapsed(since time.Time) time.Duration
TimeLimit(since time.Time) <-chan time.Time
}
// ConfigurableStopwatcher implements Stopwatcher and allows its user to
// fully customize it with no make function required.
type ConfigurableStopwatcher struct {
TL time.Duration
Clock clockwork.Clock
}
// Now will return time point since internal's clock epoch.
func (s *ConfigurableStopwatcher) Now() time.Time {
return s.Clock.Now()
}
// Elapsed will return the duration since the `since` time, or zero if `since`
// is in the future.
func (s *ConfigurableStopwatcher) Elapsed(since time.Time) time.Duration {
if s.Clock.Now().After(since) {
return s.Clock.Since(since)
}
return 0
}
// TimeLimit for a given `since` time point returns a channel that will return
// at `since` + TL time point the very same time point. I.e., it works just
// just like time.After, but you can specify the *time* after which you want to
// be notified.
func (s *ConfigurableStopwatcher) TimeLimit(since time.Time) <-chan time.Time {
if s.TL == 0 || since == (time.Time{}) {
ch := make(chan time.Time, 1)
return ch
}
return s.Clock.After(since.Add(s.TL).Sub(s.Clock.Now()))
}