@@ -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,35 @@ 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+ timer := time .NewTimer (time .Duration (maxWaitTimeInSeconds ) * time .Second )
343+ defer timer .Stop ()
344+ select {
345+ case <- th .client .Channel ().Close (time .Duration (timeout ) * time .Second ):
346+ // timeout specified for retries.
347+
348+ // If we got here, then all telemetry was submitted
349+ // successfully, and we can proceed to exiting.
350+
351+ case <- timer .C :
352+ // absolute timeout. This covers any
353+ // previous telemetry submission that may not have
354+ // completed before Close was called.
355+
356+ // There are a number of reasons we could have
357+ // reached here. We gave it a go, but telemetry
358+ // submission failed somewhere. Perhaps old events
359+ // were still retrying, or perhaps we're throttled.
360+ // Either way, we don't want to wait around for it
361+ // to complete, so let's just exit.
362+ }
335363
336364 // Remove diganostic message listener
337365 if th .diagListener != nil {
0 commit comments