Skip to content

Commit

Permalink
fix: time waiting for appinsights to close was unbounded
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Gregory <[email protected]>
  • Loading branch information
huntergregory committed Jan 9, 2025
1 parent 1296fd7 commit df8cc53
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion aitelemetry/telemetrywrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
azurePublicCloudStr = "AzurePublicCloud"
hostNameKey = "hostname"
defaultTimeout = 10
maxCloseTimeoutInSeconds = 30
defaultBatchIntervalInSecs = 15
defaultBatchSizeInBytes = 32768
defaultGetEnvRetryCount = 5
Expand Down Expand Up @@ -330,8 +331,32 @@ func (th *telemetryHandle) Close(timeout int) {
timeout = defaultTimeout
}

// max wait is the minimum of the timeout and maxCloseTimeoutInSeconds
maxWaitTimeInSeconds := timeout
if maxWaitTimeInSeconds < maxCloseTimeoutInSeconds {
maxWaitTimeInSeconds = maxCloseTimeoutInSeconds
}

// wait for items to be sent otherwise timeout
<-th.client.Channel().Close(time.Duration(timeout) * time.Second)
// similar to the example in the appinsights-go repo: https://github.com/microsoft/ApplicationInsights-Go#shutdown
select {
case <-th.client.Channel().Close(time.Duration(timeout) * time.Second):
// timeout specified for retries.

// If we got here, then all telemetry was submitted
// successfully, and we can proceed to exiting.
case <-time.After(time.Duration(maxWaitTimeInSeconds) * time.Second):
// Thirty second absolute timeout. This covers any
// previous telemetry submission that may not have
// completed before Close was called.

// There are a number of reasons we could have
// reached here. We gave it a go, but telemetry
// submission failed somewhere. Perhaps old events
// were still retrying, or perhaps we're throttled.
// Either way, we don't want to wait around for it
// to complete, so let's just exit.
}

// Remove diganostic message listener
if th.diagListener != nil {
Expand Down

0 comments on commit df8cc53

Please sign in to comment.