1
0
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:
DarthSim
2018-10-05 21:17:36 +06:00
parent 6e8933198f
commit 34a61d287f
95 changed files with 23591 additions and 179 deletions

View File

@@ -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)
}