-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_alarm_manager.go
61 lines (50 loc) · 1.34 KB
/
mem_alarm_manager.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
package stableinterfaces
import (
"context"
"github.com/danthegoodman1/stableinterfaces/syncx"
"github.com/tidwall/btree"
)
type (
// MemAlarmManager is a memory-only implementation of AlarmManager.
// Not thread-safe, made for testing only!
MemAlarmManager struct {
alarms syncx.Map[uint32, *btree.Map[string, StoredAlarm]]
}
)
func NewMemAlarmManager() MemAlarmManager {
return MemAlarmManager{
alarms: syncx.NewMap[uint32, *btree.Map[string, StoredAlarm]](),
}
}
func (m *MemAlarmManager) GetNextAlarms(_ context.Context, shard uint32) ([]StoredAlarm, error) {
// Verify it exists
tree, exists := m.alarms.Load(shard)
if !exists {
return nil, nil
}
var alarms []StoredAlarm
// Get sorted alarms
tree.Scan(func(_ string, alarm StoredAlarm) bool {
alarms = append(alarms, alarm)
return true
})
return alarms, nil
}
func (m *MemAlarmManager) SetAlarm(_ context.Context, shard uint32, alarm StoredAlarm) error {
tree, exists := m.alarms.Load(shard)
if !exists {
tree = &btree.Map[string, StoredAlarm]{}
m.alarms.Store(shard, tree)
}
tree.Set(alarm.ID, alarm)
return nil
}
func (m *MemAlarmManager) MarkAlarmDone(_ context.Context, shard uint32, alarmID string, _ AlarmDoneReason) error {
// We're just going to delete it
tree, exists := m.alarms.Load(shard)
if !exists {
return nil
}
tree.Delete(alarmID)
return nil
}