2017-10-04 21:44:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-05 17:17:36 +02:00
|
|
|
"context"
|
2017-10-04 21:44:58 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
var timerSinceCtxKey = ctxKey("timerSince")
|
|
|
|
|
2019-09-16 11:53:45 +02:00
|
|
|
func setTimerSince(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, timerSinceCtxKey, time.Now())
|
2018-10-05 17:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTimerSince(ctx context.Context) time.Duration {
|
|
|
|
return time.Since(ctx.Value(timerSinceCtxKey).(time.Time))
|
2017-10-04 21:44:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
func checkTimeout(ctx context.Context) {
|
2017-10-04 21:44:58 +02:00
|
|
|
select {
|
2018-10-05 17:17:36 +02:00
|
|
|
case <-ctx.Done():
|
2018-10-25 15:24:34 +02:00
|
|
|
d := getTimerSince(ctx)
|
|
|
|
|
|
|
|
if newRelicEnabled {
|
|
|
|
sendTimeoutToNewRelic(ctx, d)
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:04:47 +02:00
|
|
|
if prometheusEnabled {
|
|
|
|
incrementPrometheusErrorsTotal("timeout")
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:24:34 +02:00
|
|
|
panic(newError(503, fmt.Sprintf("Timeout after %v", d), "Timeout"))
|
2017-10-04 21:44:58 +02:00
|
|
|
default:
|
|
|
|
// Go ahead
|
|
|
|
}
|
|
|
|
}
|