-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.go
97 lines (78 loc) · 1.8 KB
/
middleware.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package healthz
import (
"bytes"
"net/http"
"sync"
"time"
)
// CacheMiddleware allows the health checks to be cached for a specified
// duration. This means that subsequent calls will return the cached response
// for all checks within a timespan of the given time.Duration.
// When an error occurs writing the cached data to the response, this will
// panic.
func CacheMiddleware(d time.Duration) MiddlewareFunc {
return func(next http.Handler) http.Handler {
h := &cacheHandler{
duration: d,
lastTick: time.Now(),
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if h.shouldCapture() {
h.captureResponse(next, r)
}
h.writeCachedResponse(w)
})
}
}
type cacheHandler struct {
sync.RWMutex
duration time.Duration
lastTick time.Time
writer *cacheWriter
}
func (h *cacheHandler) captureResponse(next http.Handler, r *http.Request) {
h.Lock()
defer h.Unlock()
h.writer = &cacheWriter{
Buffer: bytes.NewBuffer([]byte{}),
header: http.Header{},
}
h.lastTick = time.Now()
next.ServeHTTP(h.writer, r)
}
func (h *cacheHandler) writeCachedResponse(w http.ResponseWriter) {
h.RLock()
defer h.RUnlock()
for hk, hvs := range h.writer.header {
for _, hv := range hvs {
w.Header().Add(hk, hv)
}
}
if h.writer.statusCodeSet {
w.WriteHeader(h.writer.statusCode)
}
if _, err := w.Write(h.writer.Bytes()); err != nil {
panic(err)
}
}
func (h *cacheHandler) shouldCapture() bool {
h.RLock()
defer h.RUnlock()
if h.writer == nil {
return true
}
return time.Since(h.lastTick) >= h.duration
}
type cacheWriter struct {
*bytes.Buffer
header http.Header
statusCodeSet bool
statusCode int
}
func (h *cacheWriter) Header() http.Header {
return h.header
}
func (h *cacheWriter) WriteHeader(i int) {
h.statusCodeSet = true
h.statusCode = i
}