/
endpoints.go
288 lines (241 loc) · 6.93 KB
/
endpoints.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package endpoints
import (
"fmt"
"net"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/spoke-d/thermionic/internal/cert"
"github.com/spoke-d/thermionic/internal/clock"
"github.com/spoke-d/thermionic/internal/retrier"
"github.com/spoke-d/thermionic/internal/tomb"
)
// DefaultPort defines the default port for the http server
const DefaultPort = "8080"
// Numeric code identifying a specific API endpoint type.
type kind int
// Numeric codes identifying the various endpoints.
const (
network kind = iota
pprof
)
func (k kind) Description() string {
switch k {
case network:
return "TCP socket"
case pprof:
return "pprof socket"
default:
return "unknown"
}
}
// Server is a point of use interface for a http.Server
type Server interface {
// Server traffic from the listener.
Serve(net.Listener) error
}
// Endpoints are in charge of bringing up and down the HTTP endpoints for
// serving the RESTful API.
//
// When it starts up, they start listen to the appropriate sockets and attach
// the relevant HTTP handlers to them. When it shuts down they close all
// sockets.
type Endpoints struct {
tomb *tomb.Tomb
mutex sync.RWMutex
listeners map[kind]net.Listener
servers map[kind]Server
cert *cert.Info
logger log.Logger
sleeper clock.Sleeper
}
// New creates Endpoints with sane defaults
func New(restServer Server, cert *cert.Info, options ...Option) *Endpoints {
opts := newOptions()
opts.restServer = restServer
opts.cert = cert
for _, option := range options {
option(opts)
}
return &Endpoints{
tomb: tomb.New(),
listeners: map[kind]net.Listener{
network: networkCreateListener(opts.networkAddress, cert, opts.logger),
pprof: pprofCreateListener(opts.debugAddress, opts.logger),
},
servers: map[kind]Server{
network: opts.restServer,
pprof: pprofCreateServer(),
},
cert: opts.cert,
logger: opts.logger,
sleeper: opts.sleeper,
}
}
// Up brings down all endpoints and stops serving HTTP requests.
func (e *Endpoints) Up() error {
e.mutex.Lock()
defer e.mutex.Unlock()
level.Info(e.logger).Log("msg", "REST API daemon")
if err := e.serveHTTP(network); err != nil {
return errors.WithStack(err)
}
if err := e.serveHTTP(pprof); err != nil {
return errors.WithStack(err)
}
return nil
}
// Down brings down all endpoints and stops serving HTTP requests.
func (e *Endpoints) Down() error {
e.mutex.Lock()
defer e.mutex.Unlock()
for _, v := range []kind{network, pprof} {
if e.listeners[v] != nil {
level.Info(e.logger).Log("msg", "Stopping API handler", "kind", v.Description())
err := e.closeListener(v)
if err != nil {
return errors.WithStack(err)
}
}
}
if e.tomb != nil {
e.tomb.Kill(nil)
e.tomb.Wait()
}
return nil
}
// NetworkAddress returns the network addresses of the network endpoint, or an
// empty string if there's no network endpoint
func (e *Endpoints) NetworkAddress() string {
e.mutex.RLock()
defer e.mutex.RUnlock()
listener := e.listeners[network]
if listener == nil {
return ""
}
return listener.Addr().String()
}
// PprofAddress returns the network addresss of the pprof endpoint, or an empty
// string if there's no pprof endpoint
func (e *Endpoints) PprofAddress() string {
e.mutex.RLock()
defer e.mutex.RUnlock()
listener := e.listeners[pprof]
if listener == nil {
return ""
}
return listener.Addr().String()
}
// NetworkPublicKey returns the public key of the TLS certificate used by the
// network endpoint.
func (e *Endpoints) NetworkPublicKey() []byte {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.cert.PublicKey()
}
// NetworkPrivateKey returns the private key of the TLS certificate used by the
// network endpoint.
func (e *Endpoints) NetworkPrivateKey() []byte {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.cert.PrivateKey()
}
// NetworkCert returns the full TLS certificate information for this endpoint.
func (e *Endpoints) NetworkCert() *cert.Info {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.cert
}
// NetworkUpdateCert updates the TLS keypair and CA used by the network
// endpoint.
//
// If the network endpoint is active, in-flight requests will continue using
// the old certificate, and only new requests will use the new one.
func (e *Endpoints) NetworkUpdateCert(cert *cert.Info) error {
e.mutex.Lock()
defer e.mutex.Unlock()
e.cert = cert
if listener, ok := e.listeners[network]; ok {
listener.(*networkListener).Config(cert)
}
return nil
}
// NetworkUpdateAddress updates the address for the network endpoint, shutting
// it down and restarting it.
func (e *Endpoints) NetworkUpdateAddress(address string) error {
return e.newListener(network, e.NetworkAddress(), address)
}
// PprofUpdateAddress updates the address for the pprof endpoint, shutting
// it down and restarting it.
func (e *Endpoints) PprofUpdateAddress(address string) error {
return e.newListener(pprof, e.PprofAddress(), address)
}
// Start an HTTP server for the endpoint associated with the given code.
func (e *Endpoints) serveHTTP(kind kind) error {
listener := e.listeners[kind]
if listener == nil {
return nil
}
message := fmt.Sprintf(" - binding %s", kind.Description())
level.Info(e.logger).Log("msg", message, "address", listener.Addr())
server := e.servers[kind]
if err := e.tomb.Go(func() error {
return server.Serve(listener)
}); err != nil {
return errors.WithStack(err)
}
return nil
}
// Stop the HTTP server of the endpoint associated with the given code. The
// associated socket will be shutdown too.
func (e *Endpoints) closeListener(kind kind) error {
listener := e.listeners[kind]
if listener == nil {
return nil
}
delete(e.listeners, kind)
level.Info(e.logger).Log("msg", " - closing socket", "address", listener.Addr())
return listener.Close()
}
func (e *Endpoints) newListener(k kind, oldAddress, address string) error {
if address != "" {
address = canonicalNetworkAddress(address)
}
if address == oldAddress {
return nil
}
level.Info(e.logger).Log("msg", " - updating address", "old-address", oldAddress, "address", address)
e.mutex.Lock()
defer e.mutex.Unlock()
// Close the previous socket
e.closeListener(k)
if address == "" {
return nil
}
// Attempt to setup the new listening socket
getListener := func(address string) (net.Listener, error) {
var listener net.Listener
retry := retrier.New(e.sleeper, 10, time.Millisecond*100)
if err := retry.Run(func() error {
var err error
listener, err = net.Listen("tcp", address)
return err
}); err != nil {
return nil, errors.WithStack(err)
}
return listener, nil
}
listener, err := getListener(address)
if err != nil {
// Attempt to revert to the previous address
if listener, err := getListener(oldAddress); err != nil {
e.listeners[k] = networkTLSListener(listener, e.cert, e.logger)
e.serveHTTP(k)
}
return errors.WithStack(err)
}
e.listeners[k] = networkTLSListener(listener, e.cert, e.logger)
return e.serveHTTP(k)
}