forked from monzo/typhon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
124 lines (114 loc) · 3.45 KB
/
server.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
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
package typhon
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strconv"
"sync"
"github.com/monzo/slog"
)
type Server struct {
l net.Listener
srv *http.Server
shuttingDown chan struct{}
shutdownOnce sync.Once
shutdownFuncs []func(context.Context)
shutdownFuncsM sync.Mutex
}
// Listener returns the network listener that this server is active on.
func (s *Server) Listener() net.Listener {
return s.l
}
// Done returns a channel that will be closed when the server begins to shutdown. The server may still be draining its
// connections at the time the channel is closed.
func (s *Server) Done() <-chan struct{} {
return s.shuttingDown
}
// Stop shuts down the server, returning when there are no more connections still open. Graceful shutdown will be
// attempted until the passed context expires, at which time all connections will be forcibly terminated.
func (s *Server) Stop(ctx context.Context) {
s.shutdownFuncsM.Lock()
defer s.shutdownFuncsM.Unlock()
s.shutdownOnce.Do(func() {
close(s.shuttingDown)
// Shut down the HTTP server in parallel to calling any custom shutdown functions
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if err := s.srv.Shutdown(ctx); err != nil {
slog.Debug(ctx, "Graceful shutdown failed; forcibly closing connections 👢")
if err := s.srv.Close(); err != nil {
slog.Critical(ctx, "Forceful shutdown failed, exiting 😱: %v", err)
panic(err) // Something is super hosed here
}
}
}()
for _, f := range s.shutdownFuncs {
f := f // capture range variable
wg.Add(1)
go func() {
defer wg.Done()
f(ctx)
}()
}
wg.Wait()
})
}
// addShutdownFunc registers a function that will be called when the server is stopped. The function is expected to try
// to shutdown gracefully until the context expires, at which time it should terminate its work forcefully.
func (s *Server) addShutdownFunc(f func(context.Context)) {
s.shutdownFuncsM.Lock()
defer s.shutdownFuncsM.Unlock()
s.shutdownFuncs = append(s.shutdownFuncs, f)
}
// Serve starts a HTTP server, binding the passed Service to the passed listener.
func Serve(svc Service, l net.Listener) (*Server, error) {
s := &Server{
l: l,
shuttingDown: make(chan struct{})}
svc = svc.Filter(func(req Request, svc Service) Response {
req.server = s
return svc(req)
})
s.srv = &http.Server{
Handler: HttpHandler(svc),
MaxHeaderBytes: http.DefaultMaxHeaderBytes}
go func() {
err := s.srv.Serve(l)
if err != nil && err != http.ErrServerClosed {
slog.Error(nil, "HTTP server error: %v", err)
// Stopping with an already-closed context means we go immediately to "forceful" mode
ctx, cancel := context.WithCancel(context.Background())
cancel()
s.Stop(ctx)
}
}()
return s, nil
}
func Listen(svc Service, addr string) (*Server, error) {
// Determine on which address to listen, choosing in order one of:
// 1. The passed addr
// 2. PORT variable (listening on all interfaces)
// 3. Random, available port, on the loopback interface only
if addr == "" {
if _addr := os.Getenv("LISTEN_ADDR"); _addr != "" {
addr = _addr
} else if port, err := strconv.Atoi(os.Getenv("PORT")); err == nil && port >= 0 {
addr = fmt.Sprintf(":%d", port)
} else {
addr = ":0"
}
}
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
l, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
return nil, err
}
return Serve(svc, l)
}