forked from mailgun/gubernator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.go
148 lines (133 loc) · 4.51 KB
/
interval.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2018-2022 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gubernator
import (
"errors"
"time"
"github.com/mailgun/holster/v4/clock"
"github.com/mailgun/holster/v4/syncutil"
)
// Interval is a one-shot ticker. Call `Next()` to trigger the start of an
// interval. Read the `C` channel for tick event.
type Interval struct {
C chan struct{}
in chan struct{}
wg syncutil.WaitGroup
}
// NewInterval creates a new ticker like object, however
// the `C` channel does not return the current time and
// `C` channel will only get a tick after `Next()` has
// been called.
func NewInterval(d clock.Duration) *Interval {
i := Interval{
C: make(chan struct{}, 1),
in: make(chan struct{}, 1),
}
i.run(d)
return &i
}
func (i *Interval) run(d clock.Duration) {
i.wg.Until(func(done chan struct{}) bool {
select {
case <-i.in:
time.Sleep(d)
i.C <- struct{}{}
return true
case <-done:
return false
}
})
}
func (i *Interval) Stop() {
i.wg.Stop()
}
// Next queues the next interval to run, If multiple calls to Next() are
// made before previous intervals have completed they are ignored.
func (i *Interval) Next() {
select {
case i.in <- struct{}{}:
default:
}
}
const (
GregorianMinutes int64 = iota
GregorianHours
GregorianDays
GregorianWeeks
GregorianMonths
GregorianYears
)
// GregorianDuration returns the entire duration of the Gregorian interval
func GregorianDuration(now clock.Time, d int64) (int64, error) {
switch d {
case GregorianMinutes:
return 60000, nil
case GregorianHours:
return 3.6e+6, nil
case GregorianDays:
return 8.64e+7, nil
case GregorianWeeks:
return 0, errors.New("`Duration = GregorianWeeks` not yet supported; consider making a PR!`")
case GregorianMonths:
y, m, _ := now.Date()
// Given the beginning of the month, subtract the end of the current month to get the duration
begin := clock.Date(y, m, 1, 0, 0, 0, 0, now.Location())
end := begin.AddDate(0, 1, 0).Add(-clock.Nanosecond)
return end.UnixNano() - begin.UnixNano()/1000000, nil
case GregorianYears:
y, _, _ := now.Date()
// Given the beginning of the year, subtract the end of the current year to get the duration
begin := clock.Date(y, clock.January, 1, 0, 0, 0, 0, now.Location())
end := begin.AddDate(1, 0, 0).Add(-clock.Nanosecond)
return end.UnixNano() - begin.UnixNano()/1000000, nil
}
return 0, errors.New("behavior DURATION_IS_GREGORIAN is set; but `Duration` is not a valid gregorian interval")
}
// GregorianExpiration returns an gregorian interval as defined by the
// 'DURATION_IS_GREGORIAN` Behavior. it returns the expiration time as the
// end of GREGORIAN interval in milliseconds from `now`.
//
// Example: If `now` is 2019-01-01 11:20:10 and `d` = GregorianMinutes then the return
// expire time would be 2019-01-01 11:20:59 in milliseconds since epoch
func GregorianExpiration(now clock.Time, d int64) (int64, error) {
switch d {
case GregorianMinutes:
return now.Truncate(clock.Minute).
Add(clock.Minute-clock.Nanosecond).
UnixNano() / 1000000, nil
case GregorianHours:
y, m, d := now.Date()
// See time.Truncate() documentation on why we can' reliably use time.Truncate(Hour) here.
return clock.Date(y, m, d, now.Hour(), 0, 0, 0, now.Location()).
Add(clock.Hour-clock.Nanosecond).
UnixNano() / 1000000, nil
case GregorianDays:
y, m, d := now.Date()
return clock.Date(y, m, d, 23, 59, 59, int(clock.Second-clock.Nanosecond), now.Location()).
UnixNano() / 1000000, nil
case GregorianWeeks:
return 0, errors.New("`Duration = GregorianWeeks` not yet supported; consider making a PR!`")
case GregorianMonths:
y, m, _ := now.Date()
return clock.Date(y, m, 1, 0, 0, 0, 0, now.Location()).
AddDate(0, 1, 0).Add(-clock.Nanosecond).
UnixNano() / 1000000, nil
case GregorianYears:
y, _, _ := now.Date()
return clock.Date(y, clock.January, 1, 0, 0, 0, 0, now.Location()).
AddDate(1, 0, 0).
Add(-clock.Nanosecond).
UnixNano() / 1000000, nil
}
return 0, errors.New("behavior DURATION_IS_GREGORIAN is set; but `Duration` is not a valid gregorian interval")
}