Skip to content

Commit 272caee

Browse files
committed
fix: time waiting for appinsights to close was unbounded
Signed-off-by: Hunter Gregory <[email protected]>
1 parent 4dac095 commit 272caee

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

aitelemetry/telemetrywrapper.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)