-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.go
145 lines (125 loc) · 3.14 KB
/
subscription.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
package dbconnector
import (
"context"
"errors"
"strconv"
"sync/atomic"
"time"
"github.com/go-jwdk/jobworker"
)
const (
subStateActive = int32(0)
subStateClosing = int32(1)
subStateClosed = int32(2)
subMetadataKeyPollingInterval = "PollingInterval"
subMetadataKeyVisibilityTimeout = "VisibilityTimeout"
subMetadataKeyMaxNumberOfJobs = "MaxNumberOfJobs"
defaultPollingInterval = 3 * time.Second
defaultMaxNumberOfJobs = int64(1)
)
func newSubscription(attributes *QueueAttributes,
conn jobworker.Connector,
grabber grabber) *subscription {
return &subscription{
pollingInterval: defaultPollingInterval,
queueAttributes: attributes,
conn: conn,
grabber: grabber,
visibilityTimeout: attributes.VisibilityTimeout,
maxNumberOfJobs: defaultMaxNumberOfJobs,
queue: make(chan *jobworker.Job),
}
}
type subscription struct {
queueAttributes *QueueAttributes
conn jobworker.Connector
pollingInterval time.Duration
visibilityTimeout int64
maxNumberOfJobs int64
grabber grabber
queue chan *jobworker.Job
state int32
}
func (s *subscription) SetMetadata(meta map[string]string) {
if v := meta[subMetadataKeyPollingInterval]; v != "" {
i, err := strconv.ParseInt(v, 10, 64)
if err == nil {
s.pollingInterval = time.Duration(i) * time.Second
}
}
if v := meta[subMetadataKeyVisibilityTimeout]; v != "" {
i, err := strconv.ParseInt(v, 10, 64)
if err == nil {
s.visibilityTimeout = i
}
}
if v := meta[subMetadataKeyMaxNumberOfJobs]; v != "" {
i, err := strconv.ParseInt(v, 10, 64)
if err == nil {
s.maxNumberOfJobs = i
}
}
}
func (s *subscription) Active() bool {
return atomic.LoadInt32(&s.state) == subStateActive
}
func (s *subscription) Queue() chan *jobworker.Job {
return s.queue
}
var ErrCompletedSubscription = errors.New("subscription is unsubscribed")
func (s *subscription) UnSubscribe() error {
if !atomic.CompareAndSwapInt32(&s.state, subStateActive, subStateClosing) {
return ErrCompletedSubscription
}
return nil
}
func (s *subscription) Start() {
ch := make(chan *jobworker.Job)
go s.writeChan(ch)
s.readChan(ch)
}
func (s *subscription) writeChan(ch chan *jobworker.Job) {
for {
if atomic.LoadInt32(&s.state) != subStateActive {
close(ch)
return
}
ctx := context.Background()
grabbed, err := s.grabber.GrabJobs(ctx, &GrabJobsInput{
QueueName: s.queueAttributes.Name,
MaxNumberOfJobs: s.maxNumberOfJobs,
VisibilityTimeout: s.visibilityTimeout,
})
if err != nil {
time.Sleep(s.pollingInterval)
continue
}
if len(grabbed.Jobs) == 0 {
time.Sleep(s.pollingInterval)
continue
}
for _, job := range grabbed.Jobs {
ch <- job
}
}
}
func (s *subscription) readChan(ch chan *jobworker.Job) {
for {
job, ok := <-ch
if !ok {
state := atomic.LoadInt32(&s.state)
if state == subStateActive || state == subStateClosing {
s.closeQueue()
}
return
}
s.queue <- job
}
}
func (s *subscription) closeQueue() {
atomic.StoreInt32(&s.state, subStateClosed)
close(s.queue)
}
type grabber interface {
GrabJobs(ctx context.Context, input *GrabJobsInput) (*GrabJobsOutput, error)
}