@@ -27,6 +27,7 @@ const (
2727 azurePublicCloudStr = "AzurePublicCloud"
2828 hostNameKey = "hostname"
2929 defaultTimeout = 10
30+ maxCloseTimeoutInSeconds = 30
3031 defaultBatchIntervalInSecs = 15
3132 defaultBatchSizeInBytes = 32768
3233 defaultGetEnvRetryCount = 5
@@ -330,8 +331,32 @@ func (th *telemetryHandle) Close(timeout int) {
330331 timeout = defaultTimeout
331332 }
332333
334+ // max wait is the minimum of the timeout and maxCloseTimeoutInSeconds
335+ maxWaitTimeInSeconds := timeout
336+ if maxWaitTimeInSeconds < maxCloseTimeoutInSeconds {
337+ maxWaitTimeInSeconds = maxCloseTimeoutInSeconds
338+ }
339+
333340 // 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+ }
335360
336361 // Remove diganostic message listener
337362 if th .diagListener != nil {
0 commit comments