mirror of
https://github.com/itzg/mc-router.git
synced 2024-06-24 10:14:42 +02:00
Wait for connections to finish when stopping (#107)
This commit is contained in:
parent
d15bc9e986
commit
f8a65abb61
|
@ -152,7 +152,10 @@ func main() {
|
||||||
|
|
||||||
// wait for process-stop signal
|
// wait for process-stop signal
|
||||||
<-c
|
<-c
|
||||||
logrus.Info("Stopping")
|
logrus.Info("Stopping. Waiting for connections to complete...")
|
||||||
|
signal.Stop(c)
|
||||||
|
connector.WaitForConnections()
|
||||||
|
logrus.Info("Stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseMappings(vals []string) map[string]string {
|
func parseMappings(vals []string) map[string]string {
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-kit/kit/metrics"
|
"github.com/go-kit/kit/metrics"
|
||||||
|
@ -21,10 +23,6 @@ const (
|
||||||
|
|
||||||
var noDeadline time.Time
|
var noDeadline time.Time
|
||||||
|
|
||||||
type Connector interface {
|
|
||||||
StartAcceptingConnections(ctx context.Context, listenAddress string, connRateLimit int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ConnectorMetrics struct {
|
type ConnectorMetrics struct {
|
||||||
Errors metrics.Counter
|
Errors metrics.Counter
|
||||||
BytesTransmitted metrics.Counter
|
BytesTransmitted metrics.Counter
|
||||||
|
@ -32,21 +30,25 @@ type ConnectorMetrics struct {
|
||||||
ActiveConnections metrics.Gauge
|
ActiveConnections metrics.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool) Connector {
|
func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool) *Connector {
|
||||||
|
|
||||||
return &connectorImpl{
|
return &Connector{
|
||||||
metrics: metrics,
|
metrics: metrics,
|
||||||
sendProxyProto: sendProxyProto,
|
sendProxyProto: sendProxyProto,
|
||||||
|
connectionsCond: sync.NewCond(&sync.Mutex{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type connectorImpl struct {
|
type Connector struct {
|
||||||
state mcproto.State
|
state mcproto.State
|
||||||
metrics *ConnectorMetrics
|
metrics *ConnectorMetrics
|
||||||
sendProxyProto bool
|
sendProxyProto bool
|
||||||
|
|
||||||
|
activeConnections int32
|
||||||
|
connectionsCond *sync.Cond
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectorImpl) StartAcceptingConnections(ctx context.Context, listenAddress string, connRateLimit int) error {
|
func (c *Connector) StartAcceptingConnections(ctx context.Context, listenAddress string, connRateLimit int) error {
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", listenAddress)
|
ln, err := net.Listen("tcp", listenAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -60,7 +62,22 @@ func (c *connectorImpl) StartAcceptingConnections(ctx context.Context, listenAdd
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectorImpl) acceptConnections(ctx context.Context, ln net.Listener, connRateLimit int) {
|
func (c *Connector) WaitForConnections() {
|
||||||
|
c.connectionsCond.L.Lock()
|
||||||
|
defer c.connectionsCond.L.Unlock()
|
||||||
|
|
||||||
|
for {
|
||||||
|
count := atomic.LoadInt32(&c.activeConnections)
|
||||||
|
if count > 0 {
|
||||||
|
logrus.Infof("Waiting on %d connection(s)", count)
|
||||||
|
c.connectionsCond.Wait()
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connector) acceptConnections(ctx context.Context, ln net.Listener, connRateLimit int) {
|
||||||
//noinspection GoUnhandledErrorResult
|
//noinspection GoUnhandledErrorResult
|
||||||
defer ln.Close()
|
defer ln.Close()
|
||||||
|
|
||||||
|
@ -82,7 +99,7 @@ func (c *connectorImpl) acceptConnections(ctx context.Context, ln net.Listener,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectorImpl) HandleConnection(ctx context.Context, frontendConn net.Conn) {
|
func (c *Connector) HandleConnection(ctx context.Context, frontendConn net.Conn) {
|
||||||
c.metrics.Connections.With("side", "frontend").Add(1)
|
c.metrics.Connections.With("side", "frontend").Add(1)
|
||||||
//noinspection GoUnhandledErrorResult
|
//noinspection GoUnhandledErrorResult
|
||||||
defer frontendConn.Close()
|
defer frontendConn.Close()
|
||||||
|
@ -164,7 +181,7 @@ func (c *connectorImpl) HandleConnection(ctx context.Context, frontendConn net.C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectorImpl) findAndConnectBackend(ctx context.Context, frontendConn net.Conn,
|
func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.Conn,
|
||||||
clientAddr net.Addr, preReadContent io.Reader, serverAddress string) {
|
clientAddr net.Addr, preReadContent io.Reader, serverAddress string) {
|
||||||
|
|
||||||
backendHostPort, resolvedHost, waker := Routes.FindBackendForServerAddress(ctx, serverAddress)
|
backendHostPort, resolvedHost, waker := Routes.FindBackendForServerAddress(ctx, serverAddress)
|
||||||
|
@ -202,8 +219,14 @@ func (c *connectorImpl) findAndConnectBackend(ctx context.Context, frontendConn
|
||||||
}
|
}
|
||||||
|
|
||||||
c.metrics.Connections.With("side", "backend", "host", resolvedHost).Add(1)
|
c.metrics.Connections.With("side", "backend", "host", resolvedHost).Add(1)
|
||||||
c.metrics.ActiveConnections.Add(1)
|
|
||||||
defer c.metrics.ActiveConnections.Add(-1)
|
c.metrics.ActiveConnections.Set(float64(
|
||||||
|
atomic.AddInt32(&c.activeConnections, 1)))
|
||||||
|
defer func() {
|
||||||
|
c.metrics.ActiveConnections.Set(float64(
|
||||||
|
atomic.AddInt32(&c.activeConnections, -1)))
|
||||||
|
c.connectionsCond.Signal()
|
||||||
|
}()
|
||||||
|
|
||||||
// PROXY protocol implementation
|
// PROXY protocol implementation
|
||||||
if c.sendProxyProto {
|
if c.sendProxyProto {
|
||||||
|
@ -257,7 +280,7 @@ func (c *connectorImpl) findAndConnectBackend(ctx context.Context, frontendConn
|
||||||
c.pumpConnections(ctx, frontendConn, backendConn)
|
c.pumpConnections(ctx, frontendConn, backendConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectorImpl) pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
|
func (c *Connector) pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
|
||||||
//noinspection GoUnhandledErrorResult
|
//noinspection GoUnhandledErrorResult
|
||||||
defer backendConn.Close()
|
defer backendConn.Close()
|
||||||
|
|
||||||
|
@ -283,7 +306,7 @@ func (c *connectorImpl) pumpConnections(ctx context.Context, frontendConn, backe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectorImpl) pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string, clientAddr net.Addr) {
|
func (c *Connector) pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string, clientAddr net.Addr) {
|
||||||
amount, err := io.Copy(outgoing, incoming)
|
amount, err := io.Copy(outgoing, incoming)
|
||||||
logrus.
|
logrus.
|
||||||
WithField("client", clientAddr).
|
WithField("client", clientAddr).
|
||||||
|
|
Loading…
Reference in New Issue
Block a user