-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.go
197 lines (181 loc) · 5.5 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
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
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"context"
"errors"
"io"
"net"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// Option is used to set optional behaviors on the server. Required behaviors are set
// in NewServer. Omitting options will not adversely affect the service
type Option func(s *Server)
// SetUseProxy will enable ASCII based proxy support defined by
// http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
func SetUseProxy(v bool) Option {
return func(s *Server) {
s.proxy = v
}
}
// NewServer returns a new server.
// loggerProvider - the logging backend to use
// listener - net.Listener
// sp SecretProvider - enables server to translate net.conn.remaddr into associated config for that device
func NewServer(l loggerProvider, sp SecretProvider, opts ...Option) *Server {
s := &Server{loggerProvider: l, SecretProvider: sp}
for _, opt := range opts {
opt(s)
}
return s
}
// Server ...
type Server struct {
loggerProvider
waitGroup
SecretProvider
// enables ha-proxy ascii proxy header support
proxy bool
}
// DeadlineListener is a net.Listener that supports Deadlines
type DeadlineListener interface {
net.Listener
SetDeadline(t time.Time) error
}
// Serve is a blocking method that serves clients
func (s *Server) Serve(ctx context.Context, listener DeadlineListener) error {
defer func() {
s.Infof(ctx, "Stopping server listener for %v...", listener.Addr().String())
err := listener.Close()
if err != nil {
s.Errorf(ctx, "%s", err)
}
s.Infof(ctx, "waiting for [%v] connections to close prior to shutdown", s.active)
s.Wait()
}()
for {
select {
case <-ctx.Done():
return nil
default:
serveReceived.Inc()
// the 10 second deadline implies there is a limit to how long downstream handlers
// may take to respond to a client. Clients may also give up much sooner than this
// deadline. Be mindful of this when adjusting.
if err := listener.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
s.Errorf(ctx, "cannot set listener deadline; %s", err)
}
conn, err := listener.Accept()
if err != nil {
var opE *net.OpError
if errors.As(err, &opE) {
if !opE.Temporary() {
serveAcceptedError.Inc()
return nil
}
if opE.Temporary() {
// triggered by SetDeadline
continue
}
// something else? fall through
}
s.Errorf(ctx, "server error in serving request: %s", err)
serveAcceptedError.Inc()
continue
}
s.Add(1)
go s.serve(ctx, conn)
}
}
}
func (s *Server) serve(ctx context.Context, conn net.Conn) {
defer s.Done()
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
ms := v * 1000 // make milliseconds
connectionDuration.Observe(ms)
}))
defer timer.ObserveDuration()
// start a timer to measure loader duration
loaderStart := time.Now()
secret, handler, err := s.Get(ctx, conn.RemoteAddr())
if err != nil || secret == nil || handler == nil {
s.Errorf(ctx, "ignoring request: %v", err)
conn.Close()
timer.ObserveDuration()
return
}
ctx = context.WithValue(ctx, ContextLoaderDuration, time.Since(loaderStart).Milliseconds())
serveAccepted.Inc()
s.handle(ctx, newCrypter(secret, conn, s.proxy), handler)
serveAccepted.Dec()
}
// handle will process connections on a net.Conn. This is meant to be executed in a goroutine
func (s *Server) handle(ctx context.Context, c *crypter, h Handler) {
// defer closing the connection on return.
defer c.Close()
// scoped to the entire undelrying net.Conn. this is needed for single-connect
sessionProvider := newSessionProvider()
defer sessionProvider.close()
for {
select {
case <-ctx.Done():
s.Debugf(ctx, "context cancellation received, closing connection to %v", c.RemoteAddr())
return
default:
if err := c.SetReadDeadline(time.Now().Add(15 * time.Second)); err != nil {
s.Errorf(ctx, "unable to set read deadline on connection %v", c.RemoteAddr())
}
packet, err := c.read()
if err != nil {
if err != io.EOF {
s.Errorf(ctx, "closing connection, unable to read, %v", err)
}
return
}
// store basic connection parameters into ctx
ctxWithAddr := context.WithValue(ctx, ContextConnRemoteAddr, strip(c.RemoteAddr().String()))
ctxWithAddr = context.WithValue(ctxWithAddr, ContextConnLocalAddr, c.LocalAddr().String())
// create our request
req := Request{
Header: *packet.Header,
Body: packet.Body,
Context: ctxWithAddr,
}
// create the response
resp := &response{ctx: req.Context, crypter: c, loggerProvider: s.loggerProvider, header: req.Header}
state, err := sessionProvider.get(req.Header)
if err != nil {
s.Errorf(ctx, "unable to obtain a session; connection will close; %v", err)
return
}
// default to our provided handler for new flows
if state == nil {
state = h
sessionProvider.set(req.Header, nil)
}
handlers.Inc()
state.Handle(resp, req)
handlers.Dec()
if resp.next == nil {
s.Debugf(ctx, "[%v] sessionID is complete", req.Header.SessionID)
sessionProvider.delete(req.Header.SessionID)
continue
}
sessionProvider.update(resp.header, resp.next)
}
}
}
// strip removes port and [] from an IP address
// on a best effort basis. In case of any error, the
// original input is returned
func strip(ip string) string {
host, _, err := net.SplitHostPort(ip)
if err != nil {
return ip
}
return host
}