Repository navigation
Expand file tree
/
Copy pathtimeout.go
More file actions
151 lines (130 loc) · 3.9 KB
/
Copy pathtimeout.go
File metadata and controls
151 lines (130 loc) · 3.9 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package timeout
import (
"context"
"fmt"
"maps"
"net/http"
"runtime/debug"
"time"
"github.com/gin-gonic/gin"
)
var bufPool *BufferPool
const (
defaultTimeout = 5 * time.Second
)
// panicChan transmits both the panic value and the stack trace.
type panicInfo struct {
Value any
Stack []byte
}
// New wraps a handler and aborts the process of the handler if the timeout is reached
func New(opts ...Option) gin.HandlerFunc {
t := &Timeout{
timeout: defaultTimeout,
response: defaultResponse,
}
// Apply each option to the Timeout instance
for _, opt := range opts {
if opt == nil {
panic("timeout Option must not be nil")
}
// Call the option to configure the Timeout instance
opt(t)
}
// Initialize the buffer pool for response writers.
bufPool = &BufferPool{}
return func(c *gin.Context) {
// Swap the response writer with a buffered writer.
w := c.Writer
buffer := bufPool.Get()
tw := NewWriter(w, buffer)
c.Writer = tw
buffer.Reset()
// Set a deadline on the request context so handlers can detect
// the timeout via c.Request.Context().Done() and exit promptly.
ctx, cancel := context.WithTimeout(c.Request.Context(), t.timeout)
defer cancel()
c.Request = c.Request.WithContext(ctx)
// Channel to signal handler completion.
finish := make(chan struct{}, 1)
panicChan := make(chan panicInfo, 1)
// Run the handler in a separate goroutine to enforce timeout and catch panics.
go func() {
defer func() {
if p := recover(); p != nil {
// Capture both the panic value and the stack trace.
panicChan <- panicInfo{
Value: p,
Stack: debug.Stack(),
}
}
}()
c.Next()
finish <- struct{}{}
}()
// Use time.NewTimer for the select trigger — it has lower latency than
// ctx.Done() which runs AfterFunc in a separate goroutine.
timer := time.NewTimer(t.timeout)
defer timer.Stop()
select {
case pi := <-panicChan:
// Goroutine is done (deferred recover ran), safe to touch c.
tw.FreeBuffer()
bufPool.Put(buffer)
c.Writer = w
// If in debug mode, write error and stack trace to response for easier debugging.
if gin.IsDebugging() {
// Add the panic error to Gin's error list and write 500 status and stack trace to response.
// Check the error return value of c.Error to satisfy errcheck linter.
_ = c.Error(fmt.Errorf("%v", pi.Value))
c.Writer.WriteHeader(http.StatusInternalServerError)
// Use fmt.Fprintf instead of Write([]byte(fmt.Sprintf(...))) to satisfy staticcheck.
_, _ = fmt.Fprintf(c.Writer, "panic caught: %v\n", pi.Value)
_, _ = c.Writer.Write([]byte("Panic stack trace:\n"))
_, _ = c.Writer.Write(pi.Stack)
return
}
// In non-debug mode, re-throw the original panic value to be handled by the upper middleware.
panic(pi.Value)
case <-finish:
// Goroutine is done, safe to touch c. Flush buffer to response.
tw.mu.Lock()
defer tw.mu.Unlock()
dst := tw.ResponseWriter.Header()
maps.Copy(dst, tw.Header())
// Write the status code if it was set, otherwise use 200
if tw.code != 0 {
tw.ResponseWriter.WriteHeader(tw.code)
}
// Only write content if there's any
if buffer.Len() > 0 {
if _, err := tw.ResponseWriter.Write(buffer.Bytes()); err != nil {
panic(err)
}
}
tw.FreeBuffer()
bufPool.Put(buffer)
case <-timer.C:
tw.mu.Lock()
tw.timeout = true
tw.FreeBuffer()
bufPool.Put(buffer)
tw.mu.Unlock()
timeoutCtx := c.Copy()
timeoutCtx.Writer = w
if !w.Written() {
t.response(timeoutCtx)
}
// Wait for the goroutine to finish to avoid data race on c.index.
// The context deadline has already fired, so well-behaved handlers
// that check c.Request.Context().Done() will exit promptly.
// The tw.timeout flag causes all handler writes to be discarded.
select {
case <-finish:
case <-panicChan:
}
// Goroutine is done. Safe to modify c.index now.
c.Abort()
}
}
}