-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
47 lines (39 loc) · 750 Bytes
/
worker.go
File metadata and controls
47 lines (39 loc) · 750 Bytes
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
package sqsprocessor
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
)
type workItemMetadata struct {
ReceiptHandle string
Deadline time.Time
}
type workItem struct {
workItemMetadata
cleanup processorCleanupFunc
msg types.Message
}
type workItemResult struct {
ProcessResult
workItemMetadata
}
type worker struct {
work <-chan workItem
f ProcessFunc
}
func (w *worker) start(ctx context.Context) {
for {
select {
case msg := <-w.work:
fctx, cancel := context.WithDeadline(ctx, msg.Deadline)
res := w.f(fctx, msg.msg)
msg.cleanup(fctx, workItemResult{
ProcessResult: res,
workItemMetadata: msg.workItemMetadata,
})
cancel()
case <-ctx.Done():
return
}
}
}