-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.go
77 lines (62 loc) · 1.36 KB
/
send.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 main
import (
"bufio"
"context"
"errors"
"io"
"os"
)
func isSendMode() bool {
fi, err := os.Stdin.Stat()
if err != nil {
return false
}
return (fi.Mode() & os.ModeCharDevice) == 0
}
type inFunc func() (string, error)
func stdinNextFunc() inFunc {
scanner := bufio.NewScanner(os.Stdin)
return func() (string, error) {
for scanner.Scan() {
return scanner.Text(), nil
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", io.EOF
}
}
func dispatch(ctx context.Context, sqsClient sqsClient, next inFunc) error {
for {
if err := dispatchCommon(ctx, sqsClient, next); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, io.EOF) {
return nil
}
return err
}
}
}
func dispatchWithLimit(ctx context.Context, sqsClient sqsClient, next inFunc, limit int) error {
for limit > 0 {
if err := dispatchCommon(ctx, sqsClient, next); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, io.EOF) {
return nil
}
return err
}
limit--
}
return nil
}
func dispatchCommon(ctx context.Context, sqsClient sqsClient, next inFunc) error {
select {
case <-ctx.Done():
return nil
default:
body, err := next()
if err != nil {
return err
}
return sqsClient.SendMessage(ctx, &body)
}
}