-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathconsumer.go
155 lines (135 loc) · 2.87 KB
/
consumer.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
package queue
import (
"context"
"encoding/json"
"errors"
"sync"
"sync/atomic"
"time"
"github.com/golang-queue/queue/core"
)
var _ core.Worker = (*Consumer)(nil)
var errMaxCapacity = errors.New("max capacity reached")
// Consumer for simple queue using buffer channel
type Consumer struct {
taskQueue chan core.QueuedMessage
runFunc func(context.Context, core.QueuedMessage) error
stop chan struct{}
logger Logger
stopOnce sync.Once
stopFlag int32
}
func (s *Consumer) handle(job Job) error {
// create channel with buffer size 1 to avoid goroutine leak
done := make(chan error, 1)
panicChan := make(chan interface{}, 1)
startTime := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), job.Timeout)
defer func() {
cancel()
}()
// run the job
go func() {
// handle panic issue
defer func() {
if p := recover(); p != nil {
panicChan <- p
}
}()
// run custom process function
if job.Task != nil {
done <- job.Task(ctx)
} else {
done <- s.runFunc(ctx, job)
}
}()
select {
case p := <-panicChan:
panic(p)
case <-ctx.Done(): // timeout reached
return ctx.Err()
case <-s.stop: // shutdown service
// cancel job
cancel()
leftTime := job.Timeout - time.Since(startTime)
// wait job
select {
case <-time.After(leftTime):
return context.DeadlineExceeded
case err := <-done: // job finish
return err
case p := <-panicChan:
panic(p)
}
case err := <-done: // job finish
return err
}
}
// Run to execute new task
func (s *Consumer) Run(task core.QueuedMessage) error {
var data Job
_ = json.Unmarshal(task.Bytes(), &data)
if v, ok := task.(Job); ok {
if v.Task != nil {
data.Task = v.Task
}
}
if err := s.handle(data); err != nil {
return err
}
return nil
}
// Shutdown the worker
func (s *Consumer) Shutdown() error {
if !atomic.CompareAndSwapInt32(&s.stopFlag, 0, 1) {
return ErrQueueShutdown
}
s.stopOnce.Do(func() {
close(s.stop)
close(s.taskQueue)
})
return nil
}
// Queue send task to the buffer channel
func (s *Consumer) Queue(task core.QueuedMessage) error {
if atomic.LoadInt32(&s.stopFlag) == 1 {
return ErrQueueShutdown
}
select {
case s.taskQueue <- task:
return nil
default:
return errMaxCapacity
}
}
// Request a new task from channel
func (s *Consumer) Request() (core.QueuedMessage, error) {
clock := 0
loop:
for {
select {
case task, ok := <-s.taskQueue:
if !ok {
return nil, ErrQueueHasBeenClosed
}
return task, nil
case <-time.After(1 * time.Second):
if clock == 5 {
break loop
}
clock += 1
}
}
return nil, ErrNoTaskInQueue
}
// NewConsumer for create new consumer instance
func NewConsumer(opts ...Option) *Consumer {
o := NewOptions(opts...)
w := &Consumer{
taskQueue: make(chan core.QueuedMessage, o.queueSize),
stop: make(chan struct{}),
logger: o.logger,
runFunc: o.fn,
}
return w
}