timer.go 521 B

1234567891011121314151617181920212223242526272829303132
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type timer struct {
  7. StartTime time.Time
  8. Timer <-chan time.Time
  9. }
  10. func startTimer(dt time.Duration, info string) *timer {
  11. return &timer{time.Now(), time.After(dt)}
  12. }
  13. func (t *timer) Check() {
  14. select {
  15. case <-t.Timer:
  16. panic(t.TimeoutErr())
  17. default:
  18. // Go ahead
  19. }
  20. }
  21. func (t *timer) TimeoutErr() imgproxyError {
  22. return newError(503, fmt.Sprintf("Timeout after %v", t.Since()), "Timeout")
  23. }
  24. func (t *timer) Since() time.Duration {
  25. return time.Since(t.StartTime)
  26. }