From acb02baa2e005d98ccfc6c6bda03b2e1c50a715b Mon Sep 17 00:00:00 2001 From: 9seconds Date: Tue, 10 Dec 2019 14:13:26 +0300 Subject: [PATCH] Implement connection TTLs for multiplexing --- hub/connection.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hub/connection.go b/hub/connection.go index 60ee738..8769514 100644 --- a/hub/connection.go +++ b/hub/connection.go @@ -4,6 +4,7 @@ import ( "fmt" "math/rand" "sync" + "time" "go.uber.org/zap" @@ -13,6 +14,8 @@ import ( "github.com/9seconds/mtg/protocol" ) +const connectionTTL = time.Hour + type connection struct { conn conntypes.PacketReadWriteCloser proxyConns map[string]*ProxyConn @@ -31,6 +34,9 @@ type connection struct { func (c *connection) run() { defer c.Close() + ttl := time.NewTimer(connectionTTL) + defer ttl.Stop() + for { select { case <-c.channelDone: @@ -39,6 +45,9 @@ func (c *connection) run() { } return + case <-ttl.C: + c.logger.Debugw("Closing connection by TTL") + c.Close() case resp := <-c.channelRead: if channel, ok := c.proxyConns[string(resp.ConnID[:])]; ok { if resp.Type == rpc.ProxyResponseTypeCloseExt {