@@ -55,10 +55,6 @@ const (
55
55
56
56
inactiveInstanceReconnectDelay = 1 * time .Hour
57
57
58
- // connectionTime is the maximum time after which agent closes its connection to ACS
59
- connectionTime = 15 * time .Minute
60
- connectionJitter = 30 * time .Minute
61
-
62
58
connectionBackoffMin = 250 * time .Millisecond
63
59
connectionBackoffMax = 2 * time .Minute
64
60
connectionBackoffJitter = 0.2
@@ -97,6 +93,7 @@ type session struct {
97
93
ctx context.Context
98
94
cancel context.CancelFunc
99
95
backoff retry.Backoff
96
+ metricsFactory metrics.EntryFactory
100
97
clientFactory wsclient.ClientFactory
101
98
sendCredentials bool
102
99
latestSeqNumTaskManifest * int64
@@ -127,6 +124,7 @@ func NewSession(
127
124
doctor * doctor.Doctor ,
128
125
clientFactory wsclient.ClientFactory ,
129
126
addUpdateRequestHandlers func (wsclient.ClientServer ),
127
+ metricsFactory metrics.EntryFactory ,
130
128
) Session {
131
129
backoff := retry .NewExponentialBackoff (connectionBackoffMin , connectionBackoffMax ,
132
130
connectionBackoffJitter , connectionBackoffMultiplier )
@@ -149,13 +147,14 @@ func NewSession(
149
147
backoff : backoff ,
150
148
latestSeqNumTaskManifest : latestSeqNumTaskManifest ,
151
149
doctor : doctor ,
150
+ metricsFactory : metricsFactory ,
152
151
clientFactory : clientFactory ,
153
152
addUpdateRequestHandlers : addUpdateRequestHandlers ,
154
153
sendCredentials : true ,
155
154
_heartbeatTimeout : heartbeatTimeout ,
156
155
_heartbeatJitter : heartbeatJitter ,
157
- connectionTime : connectionTime ,
158
- connectionJitter : connectionJitter ,
156
+ connectionTime : wsclient . DisconnectTimeout ,
157
+ connectionJitter : wsclient . DisconnectJitterMax ,
159
158
_inactiveInstanceReconnectDelay : inactiveInstanceReconnectDelay ,
160
159
}
161
160
}
@@ -233,7 +232,8 @@ func (acsSession *session) startSessionOnce() error {
233
232
url ,
234
233
acsSession .credentialsProvider ,
235
234
wsRWTimeout ,
236
- minAgentCfg )
235
+ minAgentCfg ,
236
+ acsSession .metricsFactory )
237
237
defer client .Close ()
238
238
239
239
return acsSession .startACSSession (client )
@@ -257,21 +257,19 @@ func (acsSession *session) startACSSession(client wsclient.ClientServer) error {
257
257
258
258
taskStopper := NewTaskStopper (acsSession .taskEngine )
259
259
260
- metricsFactory := metrics .NewNopEntryFactory ()
261
-
262
260
responseSender := func (response interface {}) error {
263
261
return client .MakeRequest (response )
264
262
}
265
263
responders := []wsclient.RequestResponder {
266
264
acssession .NewPayloadResponder (payloadMsgHandler , responseSender ),
267
- acssession .NewRefreshCredentialsResponder (acsSession .credentialsManager , credsMetadataSetter , metricsFactory ,
265
+ acssession .NewRefreshCredentialsResponder (acsSession .credentialsManager , credsMetadataSetter , acsSession . metricsFactory ,
268
266
responseSender ),
269
267
acssession .NewAttachTaskENIResponder (eniHandler , responseSender ),
270
268
acssession .NewAttachInstanceENIResponder (eniHandler , responseSender ),
271
269
acssession .NewHeartbeatResponder (acsSession .doctor , responseSender ),
272
270
acssession .NewTaskManifestResponder (taskComparer , sequenceNumberAccessor , manifestMessageIDAccessor ,
273
- metricsFactory , responseSender ),
274
- acssession .NewTaskStopVerificationACKResponder (taskStopper , manifestMessageIDAccessor , metricsFactory ),
271
+ acsSession . metricsFactory , responseSender ),
272
+ acssession .NewTaskStopVerificationACKResponder (taskStopper , manifestMessageIDAccessor , acsSession . metricsFactory ),
275
273
}
276
274
for _ , r := range responders {
277
275
client .AddRequestHandler (r .HandlerFunc ())
@@ -281,17 +279,17 @@ func (acsSession *session) startACSSession(client wsclient.ClientServer) error {
281
279
acsSession .addUpdateRequestHandlers (client )
282
280
}
283
281
284
- err := client .Connect ()
282
+ disconnectTimer , err := client .Connect (metrics .ACSDisconnectTimeoutMetricName ,
283
+ acsSession .connectionTime ,
284
+ acsSession .connectionJitter )
285
285
if err != nil {
286
286
seelog .Errorf ("Error connecting to ACS: %v" , err )
287
287
return err
288
288
}
289
289
290
+ defer disconnectTimer .Stop ()
291
+
290
292
seelog .Info ("Connected to ACS endpoint" )
291
- // Start a connection timer; agent close its ACS websocket connection
292
- // after this timer expires
293
- connectionTimer := newConnectionTimer (client , acsSession .connectionTime , acsSession .connectionJitter )
294
- defer connectionTimer .Stop ()
295
293
296
294
// Start a heartbeat timer for closing the connection
297
295
heartbeatTimer := newHeartbeatTimer (client , acsSession .heartbeatTimeout (), acsSession .heartbeatJitter ())
@@ -316,6 +314,20 @@ func (acsSession *session) startACSSession(client wsclient.ClientServer) error {
316
314
return client .Serve (acsSession .ctx )
317
315
}
318
316
317
+ // newHeartbeatTimer creates a new time object, with a callback to
318
+ // disconnect from ACS on inactivity
319
+ func newHeartbeatTimer (client wsclient.ClientServer , timeout time.Duration , jitter time.Duration ) ttime.Timer {
320
+ timer := time .AfterFunc (retry .AddJitter (timeout , jitter ), func () {
321
+ seelog .Warn ("ACS Connection hasn't had any activity for too long; closing connection" )
322
+ if err := client .Close (); err != nil {
323
+ seelog .Warnf ("Error disconnecting: %v" , err )
324
+ }
325
+ seelog .Info ("Disconnected from ACS" )
326
+ })
327
+
328
+ return timer
329
+ }
330
+
319
331
func (acsSession * session ) computeReconnectDelay (isInactiveInstance bool ) time.Duration {
320
332
if isInactiveInstance {
321
333
return acsSession ._inactiveInstanceReconnectDelay
@@ -366,37 +378,6 @@ func (acsSession *session) acsURL(endpoint string) string {
366
378
return acsURL + "?" + query .Encode ()
367
379
}
368
380
369
- // newHeartbeatTimer creates a new time object, with a callback to
370
- // disconnect from ACS on inactivity
371
- func newHeartbeatTimer (client wsclient.ClientServer , timeout time.Duration , jitter time.Duration ) ttime.Timer {
372
- timer := time .AfterFunc (retry .AddJitter (timeout , jitter ), func () {
373
- seelog .Warn ("ACS Connection hasn't had any activity for too long; closing connection" )
374
- if err := client .Close (); err != nil {
375
- seelog .Warnf ("Error disconnecting: %v" , err )
376
- }
377
- seelog .Info ("Disconnected from ACS" )
378
- })
379
-
380
- return timer
381
- }
382
-
383
- // newConnectionTimer creates a new timer, after which agent closes
384
- // its websocket connection
385
- func newConnectionTimer (client wsclient.ClientServer , connectionTime time.Duration ,
386
- connectionJitter time.Duration ) ttime.Timer {
387
- expiresAt := retry .AddJitter (connectionTime , connectionJitter )
388
- timer := time .AfterFunc (expiresAt , func () {
389
- seelog .Infof ("Closing ACS websocket connection after %v minutes" , expiresAt .Minutes ())
390
- // WriteCloseMessage() writes a close message using websocket control messages
391
- // Ref: https://pkg.go.dev/github.com/gorilla/websocket#hdr-Control_Messages
392
- err := client .WriteCloseMessage ()
393
- if err != nil {
394
- seelog .Warnf ("Error writing close message: %v" , err )
395
- }
396
- })
397
- return timer
398
- }
399
-
400
381
// anyMessageHandler handles any server message. Any server message means the
401
382
// connection is active and thus the heartbeat disconnect should not occur
402
383
func anyMessageHandler (timer ttime.Timer , client wsclient.ClientServer ) func (interface {}) {
0 commit comments