/
timeout.go
41 lines (34 loc) · 841 Bytes
/
timeout.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
package timeout
import (
"net/http"
"time"
"github.com/AlexanderChen1989/xrest"
"golang.org/x/net/context"
)
// timeout plug
// if duration <= 0, no timeout
type timeout struct {
next xrest.Handler
Duration time.Duration
}
// New create a new timeout plug
func New(d time.Duration) xrest.Plugger {
return newTimeout(d)
}
func newTimeout(d time.Duration) *timeout {
return &timeout{Duration: d}
}
// Plug implements xrest.Plugger interface
func (to *timeout) Plug(h xrest.Handler) xrest.Handler {
to.next = h
return to
}
// ServeHTTP implements xrest.Handler interface
func (to *timeout) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if to.Duration > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, to.Duration)
defer cancel()
}
to.next.ServeHTTP(ctx, w, r)
}