-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtimeout-middleware.go
More file actions
96 lines (84 loc) · 2.06 KB
/
timeout-middleware.go
File metadata and controls
96 lines (84 loc) · 2.06 KB
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
package main
import (
"context"
"net/http"
"sync"
"time"
)
// timeoutResponseWriter wraps http.ResponseWriter to prevent writes after timeout
type timeoutResponseWriter struct {
http.ResponseWriter
mu sync.Mutex
timedOut bool
wroteHeader bool
}
func (tw *timeoutResponseWriter) WriteHeader(code int) {
tw.mu.Lock()
defer tw.mu.Unlock()
if tw.timedOut || tw.wroteHeader {
return
}
tw.wroteHeader = true
tw.ResponseWriter.WriteHeader(code)
}
func (tw *timeoutResponseWriter) Write(b []byte) (int, error) {
tw.mu.Lock()
defer tw.mu.Unlock()
if tw.timedOut {
return 0, http.ErrHandlerTimeout
}
if !tw.wroteHeader {
tw.wroteHeader = true
}
return tw.ResponseWriter.Write(b)
}
func (tw *timeoutResponseWriter) markTimedOut() {
tw.mu.Lock()
defer tw.mu.Unlock()
tw.timedOut = true
}
func timeoutMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
tw := &timeoutResponseWriter{ResponseWriter: w}
done := make(chan struct{})
panicChan := make(chan interface{}, 1)
go func() {
defer func() {
if err := recover(); err != nil {
panicChan <- err
}
}()
next.ServeHTTP(tw, r.WithContext(ctx))
close(done)
}()
select {
case p := <-panicChan:
// Panic occurred, re-panic in main goroutine
panic(p)
case <-done:
// Request completed successfully
return
case <-ctx.Done():
// Timeout reached
tw.markTimedOut()
if ctx.Err() == context.DeadlineExceeded {
// Only write retry page if handler hasn't written anything yet
tw.mu.Lock()
hasWritten := tw.wroteHeader
tw.mu.Unlock()
if !hasWritten {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
w.Header().Set("X-Robots-Tag", "noindex, nofollow")
w.WriteHeader(http.StatusOK)
retryTemplate(RetryPageParams{
HeadParams: HeadParams{},
}).Render(r.Context(), w)
}
}
}
}
}