You've already forked imgproxy
mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-12-03 23:19:17 +02:00
fasthttp; Optimized memory allocation
This commit is contained in:
28
timer.go
28
timer.go
@@ -1,32 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var timerSinceCtxKey = ctxKey("timerSince")
|
||||
|
||||
type timer struct {
|
||||
StartTime time.Time
|
||||
Timer <-chan time.Time
|
||||
}
|
||||
|
||||
func startTimer(dt time.Duration, info string) *timer {
|
||||
return &timer{time.Now(), time.After(dt)}
|
||||
func startTimer(d time.Duration) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(
|
||||
context.WithValue(context.Background(), timerSinceCtxKey, time.Now()),
|
||||
d,
|
||||
)
|
||||
}
|
||||
|
||||
func (t *timer) Check() {
|
||||
func getTimerSince(ctx context.Context) time.Duration {
|
||||
return time.Since(ctx.Value(timerSinceCtxKey).(time.Time))
|
||||
}
|
||||
|
||||
func checkTimeout(ctx context.Context) {
|
||||
select {
|
||||
case <-t.Timer:
|
||||
panic(t.TimeoutErr())
|
||||
case <-ctx.Done():
|
||||
panic(newError(503, fmt.Sprintf("Timeout after %v", getTimerSince(ctx)), "Timeout"))
|
||||
default:
|
||||
// Go ahead
|
||||
}
|
||||
}
|
||||
|
||||
func (t *timer) TimeoutErr() imgproxyError {
|
||||
return newError(503, fmt.Sprintf("Timeout after %v", t.Since()), "Timeout")
|
||||
}
|
||||
|
||||
func (t *timer) Since() time.Duration {
|
||||
return time.Since(t.StartTime)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user