@@ -27,6 +27,7 @@ const (
27
27
azurePublicCloudStr = "AzurePublicCloud"
28
28
hostNameKey = "hostname"
29
29
defaultTimeout = 10
30
+ maxCloseTimeoutInSeconds = 30
30
31
defaultBatchIntervalInSecs = 15
31
32
defaultBatchSizeInBytes = 32768
32
33
defaultGetEnvRetryCount = 5
@@ -330,8 +331,32 @@ func (th *telemetryHandle) Close(timeout int) {
330
331
timeout = defaultTimeout
331
332
}
332
333
334
+ // max wait is the minimum of the timeout and maxCloseTimeoutInSeconds
335
+ maxWaitTimeInSeconds := timeout
336
+ if maxWaitTimeInSeconds < maxCloseTimeoutInSeconds {
337
+ maxWaitTimeInSeconds = maxCloseTimeoutInSeconds
338
+ }
339
+
333
340
// wait for items to be sent otherwise timeout
334
- <- th .client .Channel ().Close (time .Duration (timeout ) * time .Second )
341
+ // similar to the example in the appinsights-go repo: https://github.com/microsoft/ApplicationInsights-Go#shutdown
342
+ select {
343
+ case <- th .client .Channel ().Close (time .Duration (timeout ) * time .Second ):
344
+ // timeout specified for retries.
345
+
346
+ // If we got here, then all telemetry was submitted
347
+ // successfully, and we can proceed to exiting.
348
+ case <- time .After (time .Duration (maxWaitTimeInSeconds ) * time .Second ):
349
+ // Thirty second absolute timeout. This covers any
350
+ // previous telemetry submission that may not have
351
+ // completed before Close was called.
352
+
353
+ // There are a number of reasons we could have
354
+ // reached here. We gave it a go, but telemetry
355
+ // submission failed somewhere. Perhaps old events
356
+ // were still retrying, or perhaps we're throttled.
357
+ // Either way, we don't want to wait around for it
358
+ // to complete, so let's just exit.
359
+ }
335
360
336
361
// Remove diganostic message listener
337
362
if th .diagListener != nil {
0 commit comments