don't panic on a backend disconnect

This commit is contained in:
Cadey Ratio 2017-09-30 10:33:19 -07:00
parent 61af71fdd3
commit d2c0a530ab
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
6 changed files with 72 additions and 41 deletions

View File

@ -42,6 +42,7 @@ func (b *Backend) List(ctx context.Context, sel *proto.BackendSelector) (*proto.
Domain: bk.Domain, Domain: bk.Domain,
Phi: bk.Phi, Phi: bk.Phi,
Host: bk.Host, Host: bk.Host,
Usable: bk.Usable,
}) })
} }

View File

@ -8,4 +8,5 @@ type Backend struct {
Domain string Domain string
Phi float32 Phi float32
Host string Host string
Usable bool
} }

View File

@ -24,9 +24,15 @@ type Connection struct {
controlStream *smux.Stream controlStream *smux.Stream
user string user string
domain string domain string
cancel context.CancelFunc cf context.CancelFunc
detector *failure.Detector detector *failure.Detector
Auth *Auth Auth *Auth
usable bool
}
func (c *Connection) cancel() {
c.cf()
c.usable = false
} }
// F logs key->value pairs as an ln.Fer // F logs key->value pairs as an ln.Fer
@ -63,6 +69,10 @@ func (c *Connection) Ping() error {
// OpenStream creates a new stream (connection) to the backend server. // OpenStream creates a new stream (connection) to the backend server.
func (c *Connection) OpenStream() (net.Conn, error) { func (c *Connection) OpenStream() (net.Conn, error) {
if !c.usable {
return nil, ErrNoSuchBackend
}
err := c.conn.SetDeadline(time.Now().Add(time.Second)) err := c.conn.SetDeadline(time.Now().Add(time.Second))
if err != nil { if err != nil {
ln.Error(err, c.F()) ln.Error(err, c.F())

View File

@ -102,6 +102,7 @@ func (s *Server) getBackendsForMatcher(bm backendMatcher) []Backend {
Domain: c.domain, Domain: c.domain,
Phi: float32(c.detector.Phi(time.Now())), Phi: float32(c.detector.Phi(time.Now())),
Host: c.conn.RemoteAddr().String(), Host: c.conn.RemoteAddr().String(),
Usable: c.usable,
}) })
} }
@ -334,7 +335,7 @@ func (s *Server) HandleConn(c net.Conn, isKCP bool) {
session: session, session: session,
user: tokenUser, user: tokenUser,
domain: auth.Domain, domain: auth.Domain,
cancel: cancel, cf: cancel,
detector: failure.New(15, 1), detector: failure.New(15, 1),
Auth: auth, Auth: auth,
} }
@ -362,6 +363,7 @@ func (s *Server) HandleConn(c net.Conn, isKCP bool) {
conns = append(conns, connection) conns = append(conns, connection)
s.domains.Set(auth.Domain, conns) s.domains.Set(auth.Domain, conns)
connection.usable = true
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop() defer ticker.Stop()
@ -371,7 +373,7 @@ func (s *Server) HandleConn(c net.Conn, isKCP bool) {
case <-ticker.C: case <-ticker.C:
err := connection.Ping() err := connection.Ping()
if err != nil { if err != nil {
cancel() connection.cancel()
} }
case <-ctx.Done(): case <-ctx.Done():
s.RemoveConn(connection) s.RemoveConn(connection)
@ -440,7 +442,14 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
} }
} }
if len(conns) == 0 { var goodConns []*Connection
for _, conn := range conns {
if conn.usable {
goodConns = append(goodConns, conn)
}
}
if len(goodConns) == 0 {
ln.Error(ErrNoSuchBackend, ln.F{ ln.Error(ErrNoSuchBackend, ln.F{
"action": "no_backend_connected", "action": "no_backend_connected",
"remote": req.RemoteAddr, "remote": req.RemoteAddr,
@ -451,7 +460,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, ErrNoSuchBackend return nil, ErrNoSuchBackend
} }
c := conns[rand.Intn(len(conns))] c := goodConns[rand.Intn(len(goodConns))]
resp, err := c.RoundTrip(req) resp, err := c.RoundTrip(req)
if err != nil { if err != nil {

View File

@ -219,6 +219,7 @@ type Backend struct {
Domain string `protobuf:"bytes,4,opt,name=domain" json:"domain,omitempty"` Domain string `protobuf:"bytes,4,opt,name=domain" json:"domain,omitempty"`
Phi float32 `protobuf:"fixed32,5,opt,name=phi" json:"phi,omitempty"` Phi float32 `protobuf:"fixed32,5,opt,name=phi" json:"phi,omitempty"`
Host string `protobuf:"bytes,6,opt,name=host" json:"host,omitempty"` Host string `protobuf:"bytes,6,opt,name=host" json:"host,omitempty"`
Usable bool `protobuf:"varint,7,opt,name=usable" json:"usable,omitempty"`
} }
func (m *Backend) Reset() { *m = Backend{} } func (m *Backend) Reset() { *m = Backend{} }
@ -268,6 +269,13 @@ func (m *Backend) GetHost() string {
return "" return ""
} }
func (m *Backend) GetUsable() bool {
if m != nil {
return m.Usable
}
return false
}
type BackendList struct { type BackendList struct {
Bs *BackendSelector `protobuf:"bytes,1,opt,name=bs" json:"bs,omitempty"` Bs *BackendSelector `protobuf:"bytes,1,opt,name=bs" json:"bs,omitempty"`
Backends []*Backend `protobuf:"bytes,2,rep,name=backends" json:"backends,omitempty"` Backends []*Backend `protobuf:"bytes,2,rep,name=backends" json:"backends,omitempty"`
@ -814,40 +822,41 @@ var _Backends_serviceDesc = grpc.ServiceDesc{
func init() { proto.RegisterFile("route.proto", fileDescriptor0) } func init() { proto.RegisterFile("route.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{ var fileDescriptor0 = []byte{
// 553 bytes of a gzipped FileDescriptorProto // 565 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x4f, 0x6f, 0xd3, 0x4e, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x5d, 0x8b, 0xd3, 0x40,
0x10, 0xf5, 0x9f, 0xd8, 0x4d, 0x26, 0x3f, 0x35, 0xfd, 0x8d, 0xa2, 0xca, 0x4a, 0x39, 0x44, 0x4b, 0x14, 0xcd, 0x47, 0x93, 0xb6, 0xb7, 0xb2, 0x5d, 0x2f, 0x65, 0x09, 0x5d, 0x1f, 0xca, 0xb8, 0xae,
0x29, 0x16, 0x88, 0x0a, 0xa5, 0x48, 0x5c, 0xe0, 0x00, 0x0a, 0x8a, 0x22, 0x50, 0x85, 0x36, 0xdc, 0x41, 0x71, 0x91, 0xae, 0xe0, 0x8b, 0x3e, 0x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0xf6, 0xd3, 0x2e, 0x8e, 0x44, 0x1b, 0xde, 0xde, 0x6c, 0x5e, 0x02, 0x8c, 0x88, 0xff, 0xa2, 0xf4, 0xff,
0x87, 0x54, 0x0d, 0xb0, 0xde, 0x2f, 0xb8, 0xc2, 0xc1, 0x2b, 0x68, 0xbd, 0x4f, 0xb3, 0x0c, 0xcf, 0xcc, 0xe1, 0x02, 0x5a, 0xf6, 0xe9, 0x14, 0xbe, 0x82, 0x06, 0x27, 0xec, 0x1f, 0xa9, 0xea, 0x63,
0xea, 0xe8, 0x74, 0x5c, 0xff, 0xd2, 0x3c, 0xe4, 0xff, 0xe3, 0xe6, 0x77, 0x00, 0x00, 0x00, 0xff, 0xbd, 0x5f, 0x71, 0x85, 0x83, 0x97, 0xd0, 0xf8, 0x98, 0x17, 0x05, 0x9e, 0xd6, 0xd1, 0xc9, 0xa8,
0xff, 0x11, 0x4c, 0x9d, 0x1a, 0x00, 0x05, 0x00, 0x00, 0xfe, 0xa5, 0x2c, 0xe4, 0xff, 0xe6, 0xfa, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x6e, 0x00, 0xdd,
0xf1, 0x18, 0x05, 0x00, 0x00,
} }

View File

@ -121,6 +121,7 @@ message Backend {
string domain = 4; string domain = 4;
float phi = 5; float phi = 5;
string host = 6; string host = 6;
bool usable = 7;
} }
message BackendList { message BackendList {