Skip to content

Commit dd8c428

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

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

aitelemetry/telemetrywrapper.go

Lines changed: 26 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,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

Comments
 (0)