-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
178 lines (151 loc) · 4.24 KB
/
example_test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package sqserv_test
import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/leelynne/sqserv/v2"
)
func Example_basic() {
// For SQS queue name 'message-queue'
queueHandler := func(w http.ResponseWriter, req *http.Request) {
// Handle as a you would an http request
// Ack the message by returning a success code
w.WriteHeader(http.StatusNoContent)
}
// Use any http.Handler compatible router/middleware
mux := http.NewServeMux()
mux.Handle("/message-queue", http.HandlerFunc(queueHandler))
conf := aws.Config{}
qsrv, err := sqserv.New(conf, mux)
if err != nil {
log.Fatal(err)
}
// non-blocking call to start polling for SQS messages
err = qsrv.ListenAndServe("message-queue")
if err == nil {
log.Fatal(err)
}
// Block until shutdown
ch := make(chan os.Signal)
// Blocks until a signal is received
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println("Shutting down queue listener.")
qsrv.Shutdown(2 * time.Second)
}
func Example_withConf() {
// For SQS queue name 'message-queue'
queueHandler := func(w http.ResponseWriter, req *http.Request) {
// Handle as a you would an http request
// Ack the message by returning a success code
w.WriteHeader(http.StatusNoContent)
}
// Use any http.Handler compatible router/middleware
mux := http.NewServeMux()
mux.Handle("/worker-virgina", http.HandlerFunc(queueHandler))
mux.Handle("/worker-oregon", http.HandlerFunc(queueHandler))
conf := aws.Config{}
qsrv, err := sqserv.New(conf, mux)
if err != nil {
log.Fatal(err)
}
// Listen to queues from two region
queues := []sqserv.QueueConf{
{
Name: "workers-virginia",
Region: "us-east-1",
},
{
Name: "workers-oregon",
Region: "us-west-2",
},
}
// non-blocking call to start polling for SQS messages
err = qsrv.ListenAndServeQueues(queues...)
if err == nil {
log.Fatal(err)
}
// Block until shutdown
ch := make(chan os.Signal)
// Blocks until a signal is received
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println("Shutting down queue listener.")
qsrv.Shutdown(2 * time.Second)
}
func Example_metrics() {
metricHandler := func(mtype sqserv.MetricType, val float64, inflight int) {
switch mtype {
case sqserv.MetricAck:
log.Printf("ACK: Inflight %d", inflight)
case sqserv.MetricNack:
log.Printf("NACK: Inflight %d", inflight)
case sqserv.MetricHeartBeat:
log.Printf("Heartbeat: Inflight %d", inflight)
case sqserv.MetricPollFailure:
log.Printf("Poll Failure: Inflight %d", inflight)
case sqserv.MetricReceive:
log.Printf("Received %f: Inflight %d", val, inflight)
case sqserv.MetricShutdown:
log.Printf("Shutdown: Inflight %d", inflight)
}
}
// For SQS queue name 'message-queue'
queueHandler := func(w http.ResponseWriter, req *http.Request) {
// Handle as a you would an http request
// Ack the message by returning a success code
w.WriteHeader(http.StatusNoContent)
}
// Use any http.Handler compatible router/middleware
mux := http.NewServeMux()
mux.Handle("/worker", http.HandlerFunc(queueHandler))
conf := aws.Config{}
qsrv, err := sqserv.New(conf, mux)
if err != nil {
log.Fatal(err)
}
// Listen to queues from two region
qconf := sqserv.QueueConf{
Name: "workers",
Metrics: metricHandler,
}
// non-blocking call to start polling for SQS messages
err = qsrv.ListenAndServeQueues(qconf)
if err == nil {
log.Fatal(err)
}
// Block until shutdown
ch := make(chan os.Signal)
// Blocks until a signal is received
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println("Shutting down queue listener.")
qsrv.Shutdown(2 * time.Second)
}
func ExampleNew() {
conf := aws.Config{}
qsrv, err := sqserv.New(conf, nil)
if err != nil {
log.Fatal(err)
}
qsrv.ListenAndServe("myqueue")
}
func ExampleSQSServer_Shutdown() {
conf := aws.Config{}
qsrv, err := sqserv.New(conf, nil)
if err != nil {
log.Fatal(err)
}
// non-blocking call to start polling for SQS messages
err = qsrv.ListenAndServe("message-queue")
if err == nil {
log.Fatal(err)
}
// Block until shutdown
ch := make(chan os.Signal)
// Blocks until a signal is received
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println("Shutting down queue listener.")
qsrv.Shutdown(2 * time.Second)
}