@@ -14,6 +14,7 @@ import (
14
14
"os/signal"
15
15
"strings"
16
16
"sync"
17
+ "sync/atomic"
17
18
"time"
18
19
19
20
stan "github.com/nats-io/stan.go"
@@ -34,12 +35,6 @@ func main() {
34
35
35
36
hostname , _ := os .Hostname ()
36
37
37
- var durable string
38
- if config .NatsDurableQueueSubscription {
39
- durable = "faas"
40
- }
41
-
42
- var unsubscribe bool
43
38
var credentials * auth.BasicAuthCredentials
44
39
var err error
45
40
@@ -53,9 +48,9 @@ func main() {
53
48
54
49
client := makeClient (config .TLSInsecure )
55
50
56
- i := 0
51
+ counter := uint64 ( 0 )
57
52
messageHandler := func (msg * stan.Msg ) {
58
- i ++
53
+ i := atomic . AddUint64 ( & counter , 1 )
59
54
60
55
log .Printf ("[#%d] Received on [%s]: '%s'\n " , i , msg .Subject , msg )
61
56
@@ -65,14 +60,14 @@ func main() {
65
60
unmarshalErr := json .Unmarshal (msg .Data , & req )
66
61
67
62
if unmarshalErr != nil {
68
- log .Printf ("Unmarshal error: %s with data %s" , unmarshalErr , msg .Data )
63
+ log .Printf ("[#%d] Unmarshal error: %s with data %s" , i , unmarshalErr , msg .Data )
69
64
return
70
65
}
71
66
72
67
xCallID := req .Header .Get ("X-Call-Id" )
73
68
74
69
functionURL := makeFunctionURL (& req , & config , req .Path , req .QueryString )
75
- fmt .Printf ("Invoking: %s with %d bytes, via: %s\n " , req .Function , len (req .Body ), functionURL )
70
+ fmt .Printf ("[#%d] Invoking: %s with %d bytes, via: %s\n " , i , req .Function , len (req .Body ), functionURL )
76
71
77
72
if config .DebugPrintBody {
78
73
fmt .Println (string (req .Body ))
@@ -81,7 +76,7 @@ func main() {
81
76
start := time .Now ()
82
77
request , err := http .NewRequest (http .MethodPost , functionURL , bytes .NewReader (req .Body ))
83
78
if err != nil {
84
- log .Printf ("Unable to post message due to invalid URL, error: %s" , err .Error ())
79
+ log .Printf ("[#%d] Unable to post message due to invalid URL, error: %s" , i , err .Error ())
85
80
return
86
81
}
87
82
@@ -102,12 +97,12 @@ func main() {
102
97
103
98
duration := time .Since (start )
104
99
105
- log .Printf ("Invoked: %s [%d] in %fs" , req .Function , statusCode , duration .Seconds ())
100
+ log .Printf ("[#%d] Invoked: %s [%d] in %fs" , i , req .Function , statusCode , duration .Seconds ())
106
101
107
102
if err != nil {
108
103
status = http .StatusServiceUnavailable
109
104
110
- log .Printf ("Error invoking %s, error: %s" , req .Function , err )
105
+ log .Printf ("[#%d] Error invoking %s, error: %s" , i , req .Function , err )
111
106
112
107
timeTaken := time .Since (started ).Seconds ()
113
108
@@ -122,18 +117,18 @@ func main() {
122
117
timeTaken )
123
118
124
119
if resultErr != nil {
125
- log .Printf ("Posted callback to: %s - status %d, error: %s\n " , req .CallbackURL .String (), http .StatusServiceUnavailable , resultErr .Error ())
120
+ log .Printf ("[#%d] Posted callback to: %s - status %d, error: %s\n " , i , req .CallbackURL .String (), http .StatusServiceUnavailable , resultErr .Error ())
126
121
} else {
127
- log .Printf ("Posted result to %s - status: %d" , req .CallbackURL .String (), resultStatusCode )
122
+ log .Printf ("[#%d] Posted result to %s - status: %d" , i , req .CallbackURL .String (), resultStatusCode )
128
123
}
129
124
}
130
125
131
126
if config .GatewayInvoke == false {
132
127
statusCode , reportErr := postReport (& client , req .Function , status , timeTaken , config .GatewayAddressURL (), credentials )
133
128
if reportErr != nil {
134
- log .Printf ("Error posting report: %s\n " , reportErr )
129
+ log .Printf ("[#%d] Error posting report: %s\n " , i , reportErr )
135
130
} else {
136
- log .Printf ("Posting report to gateway for %s - status: %d\n " , req .Function , statusCode )
131
+ log .Printf ("[#%d] Posting report to gateway for %s - status: %d\n " , i , req .Function , statusCode )
137
132
}
138
133
return
139
134
}
@@ -148,20 +143,20 @@ func main() {
148
143
functionResult = resData
149
144
150
145
if err != nil {
151
- log .Printf ("Error reading body for: %s, error: %s" , req .Function , err )
146
+ log .Printf ("[#%d] Error reading body for: %s, error: %s" ,i , req .Function , err )
152
147
}
153
148
154
149
if config .WriteDebug {
155
150
fmt .Println (string (functionResult ))
156
151
} else {
157
- fmt .Printf ("% s returned %d bytes\n " , req .Function , len (functionResult ))
152
+ fmt .Printf ("[#%d] % s returned %d bytes\n " , i , req .Function , len (functionResult ))
158
153
}
159
154
}
160
155
161
156
timeTaken := time .Since (started ).Seconds ()
162
157
163
158
if req .CallbackURL != nil {
164
- log .Printf ("Callback to: %s\n " , req .CallbackURL .String ())
159
+ log .Printf ("[#%d] Callback to: %s\n " , i , req .CallbackURL .String ())
165
160
166
161
resultStatusCode , resultErr := postResult (& client ,
167
162
res ,
@@ -173,18 +168,18 @@ func main() {
173
168
timeTaken )
174
169
175
170
if resultErr != nil {
176
- log .Printf ("Error posting to callback-url: %s\n " , resultErr )
171
+ log .Printf ("[#%d] Error posting to callback-url: %s\n " , i , resultErr )
177
172
} else {
178
- log .Printf ("Posted result for %s to callback-url: %s, status: %d" , req .Function , req .CallbackURL .String (), resultStatusCode )
173
+ log .Printf ("[#%d] Posted result for %s to callback-url: %s, status: %d" , i , req .Function , req .CallbackURL .String (), resultStatusCode )
179
174
}
180
175
}
181
176
182
177
if config .GatewayInvoke == false {
183
178
statusCode , reportErr := postReport (& client , req .Function , res .StatusCode , timeTaken , config .GatewayAddressURL (), credentials )
184
179
if reportErr != nil {
185
- log .Printf ("Error posting report: %s\n " , reportErr .Error ())
180
+ log .Printf ("[#%d] Error posting report: %s\n " , i , reportErr .Error ())
186
181
} else {
187
- log .Printf ("Posting report for %s, status: %d\n " , req .Function , statusCode )
182
+ log .Printf ("[#%d] Posting report for %s, status: %d\n " , i , req .Function , statusCode )
188
183
}
189
184
}
190
185
@@ -204,10 +199,8 @@ func main() {
204
199
205
200
subject : config .NatsChannel ,
206
201
qgroup : config .NatsQueueGroup ,
207
- durable : durable ,
208
202
messageHandler : messageHandler ,
209
- startOption : stan .StartWithLastReceived (),
210
- maxInFlight : stan .MaxInflight (config .MaxInflight ),
203
+ maxInFlight : config .MaxInflight ,
211
204
ackWait : config .AckWait ,
212
205
}
213
206
@@ -218,29 +211,13 @@ func main() {
218
211
// Wait for a SIGINT (perhaps triggered by user with CTRL-C)
219
212
// Run cleanup when signal is received
220
213
signalChan := make (chan os.Signal , 1 )
221
- cleanupDone := make (chan bool )
222
214
signal .Notify (signalChan , os .Interrupt )
223
- go func () {
224
- for range signalChan {
225
- fmt .Printf ("\n Received an interrupt, unsubscribing and closing connection...\n \n " )
226
- // Do not unsubscribe a durable on exit, except if asked to.
227
- if durable == "" || unsubscribe {
228
- if err := natsQueue .unsubscribe (); err != nil {
229
- log .Panicf (
230
- "Cannot unsubscribe subject: %s from %s because of an error: %v" ,
231
- natsQueue .subject ,
232
- natsQueue .natsURL ,
233
- err ,
234
- )
235
- }
236
- }
237
- if err := natsQueue .closeConnection (); err != nil {
238
- log .Panicf ("Cannot close connection to %s because of an error: %v\n " , natsQueue .natsURL , err )
239
- }
240
- cleanupDone <- true
241
- }
242
- }()
243
- <- cleanupDone
215
+ <- signalChan
216
+ fmt .Printf ("\n Received an interrupt, unsubscribing and closing connection...\n \n " )
217
+ if err := natsQueue .closeConnection (); err != nil {
218
+ log .Panicf ("Cannot close connection to %s because of an error: %v\n " , natsQueue .natsURL , err )
219
+ }
220
+ close (signalChan )
244
221
}
245
222
246
223
// makeClient constructs a HTTP client with keep-alive turned
0 commit comments