@@ -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,31 @@ func (th *telemetryHandle) Close(timeout int) {
330331 timeout = defaultTimeout
331332 }
332333
334+ maxWaitTimeInSeconds := timeout
335+ if maxWaitTimeInSeconds < maxCloseTimeoutInSeconds {
336+ maxWaitTimeInSeconds = maxCloseTimeoutInSeconds
337+ }
338+
333339 // wait for items to be sent otherwise timeout
334- <- th .client .Channel ().Close (time .Duration (timeout ) * time .Second )
340+ // similar to the example in the appinsights-go repo: https://github.com/microsoft/ApplicationInsights-Go#shutdown
341+ select {
342+ case <- th .client .Channel ().Close (time .Duration (timeout ) * time .Second ):
343+ // timeout specified for retries.
344+
345+ // If we got here, then all telemetry was submitted
346+ // successfully, and we can proceed to exiting.
347+ case <- time .After (time .Duration (maxWaitTimeInSeconds ) * time .Second ):
348+ // Thirty second absolute timeout. This covers any
349+ // previous telemetry submission that may not have
350+ // completed before Close was called.
351+
352+ // There are a number of reasons we could have
353+ // reached here. We gave it a go, but telemetry
354+ // submission failed somewhere. Perhaps old events
355+ // were still retrying, or perhaps we're throttled.
356+ // Either way, we don't want to wait around for it
357+ // to complete, so let's just exit.
358+ }
335359
336360 // Remove diganostic message listener
337361 if th .diagListener != nil {
0 commit comments