/
delay.go
35 lines (31 loc) · 828 Bytes
/
delay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package mware
import (
"net/http"
"strconv"
"time"
"zgo.at/zhttp"
"zgo.at/zlog"
)
// Delay adds a delay before every request.
//
// The default delay is taken from the paramters (which may be 0), and can also
// be overriden by setting a "debug-delay" cookie, which is in seconds.
//
// This is intended for debugging frontend timing issues.
func Delay(d time.Duration) zhttp.Middleware {
return func(next zhttp.HandlerFunc) zhttp.HandlerFunc {
return zhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
delay := d
c, _ := r.Cookie("debug-delay")
if c != nil {
n, _ := strconv.ParseInt(c.Value, 10, 32)
delay = time.Duration(n) * time.Second
}
if delay > 0 {
zlog.Module("debug-delay").Printf("%s delay", delay)
time.Sleep(delay)
}
return next(w, r)
})
}
}