@@ -5,22 +5,16 @@ import (
5
5
"errors"
6
6
"flag"
7
7
"fmt"
8
+ "net/http"
9
+ "sync"
10
+
8
11
"github.com/chaseisabelle/backoff/expbo"
9
12
"github.com/chaseisabelle/flagz"
10
13
"github.com/chaseisabelle/sqsc"
11
14
"github.com/chaseisabelle/stop"
12
15
"github.com/g3n/engine/util/logger"
13
- "net/http"
14
16
)
15
17
16
- // listening for workers to shutdown
17
- var listener chan struct {}
18
-
19
- // func is called before main()
20
- func init () {
21
- listener = make (chan struct {})
22
- }
23
-
24
18
// main process
25
19
func main () {
26
20
// init the configs for everything
@@ -42,9 +36,7 @@ func main() {
42
36
boMax := flag .Int ("backoff-max" , 10 , "max sleep time for the exponential backoff" )
43
37
44
38
var flags flagz.Flagz
45
-
46
39
flag .Var (& flags , "requeue" , "the http status code timeout requeue a message for" )
47
-
48
40
flag .Parse ()
49
41
50
42
if * verbose {
@@ -77,7 +69,6 @@ func main() {
77
69
78
70
if * workers < 1 {
79
71
err := errors .New ("need at least 1 worker" )
80
-
81
72
die ("failed to init workers" , err )
82
73
}
83
74
@@ -106,13 +97,16 @@ func main() {
106
97
// listen for kill/term/stop signals from user/os
107
98
stop .Listen ()
108
99
100
+ var wg sync.WaitGroup
101
+
109
102
// create the workers
110
103
for tmp > 0 {
111
104
// spawn a goroutine for each worker
112
105
go func () {
113
- empties := uint64 (0 ) //<< keeps track of number of subsequent empty replies from queue
114
- bo , err := expbo .New (uint64 (1000 ), uint64 (* boMax ) * 1000 , 2 ) //<< exponential backoff
115
-
106
+ wg .Add (1 )
107
+ defer wg .Done ()
108
+ empties := uint64 (0 ) //<< keeps track of number of subsequent empty replies from queue
109
+ bo , err := expbo .New (uint64 (1000 ), uint64 (* boMax )* 1000 , 2 ) //<< exponential backoff
116
110
if err != nil {
117
111
die ("failed to init backoff" , err )
118
112
}
@@ -122,16 +116,13 @@ func main() {
122
116
// check if user/os has stopped the program and exit gracefully
123
117
if stop .Stopped () {
124
118
debug ("graceful exit" , "worker" )
125
-
126
119
break
127
120
}
128
121
129
122
// attempt to consume a message from the queue
130
123
bod , rh , err := sqs .Consume ()
131
-
132
124
if err != nil {
133
125
fail ("consumer failure" , err )
134
-
135
126
continue
136
127
}
137
128
@@ -142,7 +133,6 @@ func main() {
142
133
// back off if necessary
143
134
if * boAfter != 0 && empties >= uint64 (* boAfter ) {
144
135
debug ("sleeping" , bo )
145
-
146
136
bo .Backoff ()
147
137
}
148
138
@@ -160,72 +150,51 @@ func main() {
160
150
161
151
// create new http request
162
152
req , err := http .NewRequest (* method , * to , bytes .NewBuffer ([]byte (bod )))
163
-
164
153
if err != nil {
165
154
fail ("http request failure" , err )
166
-
167
155
continue
168
156
}
169
157
170
158
// execute http request
171
159
res , err := cli .Do (req )
172
-
173
160
if err != nil {
174
161
fail ("http query failure" , err )
175
-
176
162
continue
177
163
}
178
164
179
165
// check the http response
180
166
if res == nil {
181
167
fail ("http response failure" , errors .New ("received nil response" ))
182
-
183
168
continue
184
169
}
185
170
186
171
err = res .Body .Close ()
187
-
188
172
if err != nil {
189
173
fail ("failed to close http response body" , err )
190
174
}
191
175
192
176
sc := res .StatusCode
193
-
194
177
debug ("received http status code" , sc )
195
178
196
179
// if the http code is in the requeue codes the requeue the message
197
180
if has (sc , statuses ) {
198
181
info ("requeue due to http response code" , sc )
199
-
200
182
continue
201
183
}
202
184
203
185
// elsewise delete the message from the queue
204
186
_ , err = sqs .Delete (rh )
205
-
206
187
if err != nil {
207
188
fail ("sqs delete failure" , err )
208
189
}
209
190
}
210
-
211
- // if we have broken out of the forever loop then we need to exit gracefully
212
- listener <- struct {}{}
213
191
}()
214
192
215
193
tmp --
216
194
}
217
195
218
- tmp = 0
219
-
220
- // listen for workers to exit gracefully
221
- for range listener {
222
- tmp ++
223
-
224
- if tmp >= * workers {
225
- break
226
- }
227
- }
228
-
196
+ // wait for the workers to finish
197
+ wg .Wait ()
229
198
info ("graceful exit" , "bye bye" )
230
199
}
231
200
@@ -251,6 +220,5 @@ func has(num int, arr []int) bool {
251
220
return true
252
221
}
253
222
}
254
-
255
223
return false
256
224
}
0 commit comments