1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/timer.go

41 lines
808 B
Go
Raw Normal View History

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")
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 ctx.Err() != context.DeadlineExceeded {
panic(newError(499, fmt.Sprintf("Request was cancelled after %v", d), "Cancelled"))
}
2018-10-25 15:24:34 +02:00
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
}
}