-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserve.go
90 lines (75 loc) · 1.76 KB
/
serve.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
// Package xhttp implements http helpers.
package xhttp
import (
"context"
"errors"
"log"
"net"
"net/http"
"sync"
"sync/atomic"
"time"
"oss.terrastruct.com/util-go/xcontext"
)
func NewServer(log *log.Logger, h http.Handler) *http.Server {
return &http.Server{
MaxHeaderBytes: 1 << 18, // 262,144B
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Hour,
ErrorLog: log,
Handler: http.MaxBytesHandler(h, 1<<20), // 1,048,576B
}
}
type safeServer struct {
*http.Server
running int32
mu sync.Mutex
}
func newSafeServer(s *http.Server) *safeServer {
return &safeServer{
Server: s,
}
}
func (s *safeServer) ListenAndServe(l net.Listener) error {
s.mu.Lock()
defer s.mu.Unlock()
if !atomic.CompareAndSwapInt32(&s.running, 0, 1) {
return errors.New("server is already running")
}
defer atomic.StoreInt32(&s.running, 0)
return s.Serve(l)
}
func (s *safeServer) Shutdown(ctx context.Context) error {
s.mu.Lock()
defer s.mu.Unlock()
if atomic.LoadInt32(&s.running) == 0 {
return nil
}
return s.Server.Shutdown(ctx)
}
func Serve(ctx context.Context, shutdownTimeout time.Duration, s *http.Server, l net.Listener) error {
s.BaseContext = func(net.Listener) context.Context {
return ctx
}
ss := newSafeServer(s)
serverClosed := make(chan struct{})
var serverError error
go func() {
serverError = ss.ListenAndServe(l)
close(serverClosed)
}()
select {
case <-serverClosed:
return serverError
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(xcontext.WithoutCancel(ctx), shutdownTimeout)
defer cancel()
err := ss.Shutdown(shutdownCtx)
<-serverClosed // Wait for server to exit
if err != nil {
return err
}
return serverError
}
}