-
Notifications
You must be signed in to change notification settings - Fork 0
/
timecapsule.go
162 lines (132 loc) · 4.75 KB
/
timecapsule.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package timecapsule
import (
"time"
"github.com/nekomeowww/xo/exp/channelx"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
// TimeCapsuleLogger is the interface that wraps the basic Log method.
type TimeCapsuleLogger interface {
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
}
// TimeCapsuleDiggerOption is the option for TimeCapsuleDigger.
type TimeCapsuleDiggerOption struct {
RetryLimit int
RetryInterval time.Duration
Logger TimeCapsuleLogger
}
// DefaultTimeCapsuleDiggerOption returns the default option for TimeCapsuleDigger.
func DefaultTimeCapsuleDiggerOption() TimeCapsuleDiggerOption {
return TimeCapsuleDiggerOption{
RetryLimit: 100,
RetryInterval: 500 * time.Millisecond,
Logger: logrus.New(),
}
}
// mergeTimeCapsuleDiggerOption merges the options.
func mergeTimeCapsuleDiggerOption(original *TimeCapsuleDiggerOption, options ...TimeCapsuleDiggerOption) TimeCapsuleDiggerOption {
if len(options) == 0 {
return *original
}
option := options[0]
if option.RetryLimit > 0 {
original.RetryLimit = option.RetryLimit
}
if option.RetryInterval > 0 {
original.RetryInterval = option.RetryInterval
}
if option.Logger != nil {
original.Logger = option.Logger
}
return *original
}
// TimeCapsuleDigger will keep polling from TimeCapsuleDigger instance for new messages
// once TimeCapsuleDigger.Start() is called, and will stop once TimeCapsuleDigger.Stop()
// is called.
type TimeCapsuleDigger[P any] struct {
dataloader Dataloader[P]
option TimeCapsuleDiggerOption
handlerFunc func(digger *TimeCapsuleDigger[P], capsule *TimeCapsule[P])
// Digging ticker to notify the goroutine to dig a new capsule
diggingTicker *time.Ticker
// Puller
puller *channelx.Puller[*TimeCapsule[P]]
}
// Digger creates a new TimeCapsuleDigger instance which derives from the TimeCapsule instance
//
// Params:
//
// topicKey is the key of the topic of time capsules, in most cases it will be the set key such
// as a Redis sorted set key or a Kafka topic
//
// topicKey: string
//
// digInterval is the interval of digging a new capsule
//
// digInterval: time.Duration
func NewDigger[P any](dataloader Dataloader[P], digInterval time.Duration, options ...TimeCapsuleDiggerOption) *TimeCapsuleDigger[P] {
digger := &TimeCapsuleDigger[P]{
dataloader: dataloader,
option: DefaultTimeCapsuleDiggerOption(),
diggingTicker: time.NewTicker(digInterval),
}
mergeTimeCapsuleDiggerOption(&digger.option, options...)
digger.puller = channelx.NewPuller[*TimeCapsule[P]]().
WithTickerChannel(digger.diggingTicker.C, func(_ time.Time) *TimeCapsule[P] { return digger.dig() }).
WithHandler(digger.handle)
return digger
}
func (t *TimeCapsuleDigger[P]) SetHandler(handlerFunc func(digger *TimeCapsuleDigger[P], capsule *TimeCapsule[P])) {
t.handlerFunc = handlerFunc
}
// BuryFor bury a capsule for a specific time.
func (t *TimeCapsuleDigger[P]) BuryFor(ctx context.Context, payload P, forTimeRange time.Duration) error {
return t.dataloader.BuryFor(ctx, payload, forTimeRange)
}
// BuryUtil bury a capsule until a specific time.
func (t *TimeCapsuleDigger[P]) BuryUtil(ctx context.Context, payload P, utilUnixMilliTimestamp int64) error {
return t.dataloader.BuryUtil(ctx, payload, utilUnixMilliTimestamp)
}
func (t *TimeCapsuleDigger[P]) dig() *TimeCapsule[P] {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
dugCapsule, err := t.dataloader.Dig(ctx)
if err != nil {
t.option.Logger.Errorf("[TimeCapsule] failed to dig time capsule from dataloader %v: %v", t.dataloader.Type(), err)
return nil
}
return dugCapsule
}
func (t *TimeCapsuleDigger[P]) destroy(capsule *TimeCapsule[P]) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if err := t.dataloader.Destroy(ctx, capsule); err != nil {
t.option.Logger.Errorf("[TimeCapsule] failed to burn time capsule: %v", err)
} else {
t.option.Logger.Debugf("[TimeCapsule] burned a capsule from dataloader %v", t.dataloader.Type())
}
}
func (t *TimeCapsuleDigger[P]) handle(dugCapsule *TimeCapsule[P]) {
if dugCapsule == nil {
return
}
t.option.Logger.Debugf("[TimeCapsule] dug a new capsule from dataloader %v", t.dataloader.Type())
t.destroy(dugCapsule)
if t.handlerFunc != nil {
t.handlerFunc(t, dugCapsule)
}
}
// Start starts the digger, which will keep polling the time capsule for new messages once the interval ticks.
func (t *TimeCapsuleDigger[P]) Start() {
t.puller.StartPull(context.Background())
}
// Stop stops the digger.
func (t *TimeCapsuleDigger[P]) Stop() {
t.diggingTicker.Stop()
_ = t.puller.StopPull(context.Background())
}