/
singleton.go
77 lines (64 loc) · 1.83 KB
/
singleton.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
package natsx
import (
"bytes"
"context"
"strconv"
"time"
"github.com/altairsix/pkg/tracer"
"github.com/nats-io/go-nats"
"github.com/opentracing/opentracing-go/log"
)
// Func defines a function for use by Singleton
type Func func(ctx context.Context) error
// Singleton ensures that only one instance of a long running function will be running at a time
func Singleton(nc *nats.Conn, subject string, interval time.Duration, fn Func) (<-chan struct{}, Func) {
// done signals once the singleton has completed
done := make(chan struct{})
// payload contains the start time for this singleton. the current election rule is the singleton that
// was started first wins
payload := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
return done, func(parent context.Context) error {
ctx, cancel := context.WithCancel(parent)
defer cancel()
segment, _ := tracer.NewSegment(parent, "natz.singleton",
log.String("subject", subject),
log.String("payoad", string(payload)),
)
sub, err := nc.Subscribe(subject, func(msg *nats.Msg) {
if v := msg.Data; v != nil && bytes.Compare(payload, v) > 0 {
segment.Info("natz.singleton.yield", log.String("other-payload", string(v)))
cancel()
}
})
if err != nil {
segment.Info("natz.singleton.err.subscribe_failed", log.Error(err))
return err
}
// Broadcast ping at interval to prevent more than one action from running
//
go func() {
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-t.C:
nc.Publish(subject, payload)
case <-ctx.Done():
segment.Info("natz.singleton.stop_ticker")
return
}
}
}()
// Unsubscribe once the action is completed
//
go func() {
defer close(done)
select {
case <-ctx.Done():
segment.Info("natz.singleton.cleanup")
sub.Unsubscribe()
}
}()
return fn(ctx)
}
}