-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslot.go
93 lines (78 loc) · 1.83 KB
/
slot.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
package timerwheel
import "time"
type Slot interface {
GetMax() uint64
CurrentSlot() uint64
CheckHit(slot, slotProfile uint64) bool
Hit(profile uint64) bool
initSlot(slot uint64)
Tick() (toZore bool)
NextSlot() (slot uint64, toZore bool)
}
func NewSlot(max uint64) Slot {
return &slotImpl{
currentSlot: 0,
maxSlot: max,
}
}
type slotImpl struct {
currentSlot uint64
maxSlot uint64
}
func (impl *slotImpl) CheckHit(slot, slotProfile uint64) bool {
return 1<<slot&slotProfile != 0
}
func (impl *slotImpl) Hit(slotProfile uint64) bool {
return impl.CheckHit(impl.currentSlot, slotProfile)
}
func (impl *slotImpl) GetMax() uint64 {
return impl.maxSlot
}
func (impl *slotImpl) CurrentSlot() uint64 {
return impl.currentSlot
}
func (impl *slotImpl) initSlot(slot uint64) {
impl.currentSlot = slot
}
func (impl *slotImpl) Tick() (toZore bool) {
impl.currentSlot, toZore = impl.NextSlot()
return toZore
}
func (impl *slotImpl) NextSlot() (slot uint64, toZore bool) {
slot = impl.currentSlot + 1
if slot >= impl.maxSlot {
return 0, true
}
return slot, false
}
func NewWeekSlot() Slot {
return &weekSlotImpl{
slotImpl{maxSlot: 7, currentSlot: 0},
}
}
type weekSlotImpl struct {
slotImpl
}
func (impl *weekSlotImpl) initSlot(slot uint64) {
impl.currentSlot = (slot + 4) % impl.maxSlot
}
func NewMonthSlot(location *time.Location) Slot {
if location == nil {
location = defaultLocatioin
}
return &monthSlotImpl{
slotImpl: slotImpl{maxSlot: 31, currentSlot: 1},
location: location,
}
}
type monthSlotImpl struct {
slotImpl
location *time.Location
}
func (impl *monthSlotImpl) NextSlot() (uint64, bool) {
slot := uint64(time.Now().In(impl.location).Add(time.Hour * 24).Day())
return slot, slot == 1
}
func (impl *monthSlotImpl) initSlot(slot uint64) {
impl.currentSlot = uint64(time.Now().In(impl.location).Day())
}