MCPcopy Index your code
hub / github.com/coder/coder / safeDuration

Function safeDuration

cli/cliui/agent.go:383–394  ·  view source on GitHub ↗

safeDuration returns a-b. If a or b is nil, it returns 0. This is because we often dereference a time pointer, which can cause a panic. These dereferences are used to calculate durations, which are not critical, and therefor should not break things when it fails. A panic has been observed in a test.

(sw *stageWriter, a, b *time.Time)

Source from the content-addressed store, hash-verified

381// when it fails.
382// A panic has been observed in a test.
383func safeDuration(sw *stageWriter, a, b *time.Time) time.Duration {
384 if a == nil || b == nil {
385 if sw != nil {
386 // Ideally the message includes which fields are <nil>, but you can
387 // use the surrounding log lines to figure that out. And passing more
388 // params makes this unwieldy.
389 sw.Log(time.Now(), codersdk.LogLevelWarn, "Warning: Failed to calculate duration from a time being <nil>.")
390 }
391 return 0
392 }
393 return a.Sub(*b)
394}
395
396// GetProgressiveInterval returns an interval that increases over time.
397// The interval starts at baseInterval and increases to

Callers 2

handleConnectedMethod · 0.85
waitForReconnectionMethod · 0.85

Calls 1

LogMethod · 0.80

Tested by

no test coverage detected