/
httpserver.go
148 lines (125 loc) · 3.76 KB
/
httpserver.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package net
import (
"context"
"errors"
"net"
"net/http"
"sync"
"go.uber.org/atomic"
)
var (
errServerStopped = errors.New("the server has been stopped")
errAlreadyListening = errors.New("the server is already listening")
)
// HTTPServer wraps an http.Server to listen asynchronously and allow stopping
// it.
type HTTPServer struct {
*http.Server
lock sync.RWMutex
listener net.Listener
done chan error
stopped atomic.Bool
}
// NewHTTPServer wraps the given http.Server into an HTTPServer.
func NewHTTPServer(s *http.Server) *HTTPServer {
return &HTTPServer{
Server: s,
done: make(chan error, 1),
}
}
// Listener returns the listener for this server or nil if the server isn't
// yet listening.
func (h *HTTPServer) Listener() net.Listener {
h.lock.RLock()
listener := h.listener
h.lock.RUnlock()
return listener
}
// ListenAndServe starts the given HTTP server up in the background and
// returns immediately. The server listens on the configured Addr or ":http"
// if unconfigured.
//
// An error is returned if the server failed to start up, if the server was
// already listening, or if the server was stopped with Stop().
func (h *HTTPServer) ListenAndServe() error {
if h.stopped.Load() {
return errServerStopped
}
h.lock.Lock()
defer h.lock.Unlock()
addr := h.Server.Addr
if addr == "" {
addr = ":http"
}
if h.listener != nil {
return errAlreadyListening
}
var err error
h.listener, err = net.Listen("tcp", addr)
if err != nil {
return err
}
go h.serve(h.listener)
return nil
}
func (h *HTTPServer) serve(listener net.Listener) {
// Serve always returns a non-nil error. For us, it's an error only if
// we didn't call Stop().
err := h.Server.Serve(listener)
if !h.stopped.Load() {
h.done <- err
} else {
h.done <- nil
}
}
// Shutdown stops the server. An error is returned if the server stopped
// unexpectedly.
//
// Once a server is stopped, it cannot be started again with ListenAndServe.
func (h *HTTPServer) Shutdown(ctx context.Context) error {
if h.stopped.Swap(true) {
return nil
}
wasRunning, closeErr := h.shutdownServer(ctx)
if !wasRunning {
return nil
}
serveErr := <-h.done // wait until Serve() stops
if closeErr != nil {
return closeErr
}
return serveErr
}
func (h *HTTPServer) shutdownServer(ctx context.Context) (wasRunning bool, _ error) {
h.lock.Lock()
defer h.lock.Unlock()
if h.listener == nil {
return false, nil
}
err := h.Server.Shutdown(ctx)
// It's possible that the serve goroutine hasn't yet started, so the server
// might not know about the listener. We ignore errors since we may b
// closing the same listener twice.
h.listener.Close()
h.listener = nil
return true, err
}