mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 00:04:02 +03:00
Implement connection TTLs for multiplexing
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
@@ -13,6 +14,8 @@ import (
|
|||||||
"github.com/9seconds/mtg/protocol"
|
"github.com/9seconds/mtg/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const connectionTTL = time.Hour
|
||||||
|
|
||||||
type connection struct {
|
type connection struct {
|
||||||
conn conntypes.PacketReadWriteCloser
|
conn conntypes.PacketReadWriteCloser
|
||||||
proxyConns map[string]*ProxyConn
|
proxyConns map[string]*ProxyConn
|
||||||
@@ -31,6 +34,9 @@ type connection struct {
|
|||||||
func (c *connection) run() {
|
func (c *connection) run() {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
ttl := time.NewTimer(connectionTTL)
|
||||||
|
defer ttl.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-c.channelDone:
|
case <-c.channelDone:
|
||||||
@@ -39,6 +45,9 @@ func (c *connection) run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
case <-ttl.C:
|
||||||
|
c.logger.Debugw("Closing connection by TTL")
|
||||||
|
c.Close()
|
||||||
case resp := <-c.channelRead:
|
case resp := <-c.channelRead:
|
||||||
if channel, ok := c.proxyConns[string(resp.ConnID[:])]; ok {
|
if channel, ok := c.proxyConns[string(resp.ConnID[:])]; ok {
|
||||||
if resp.Type == rpc.ProxyResponseTypeCloseExt {
|
if resp.Type == rpc.ProxyResponseTypeCloseExt {
|
||||||
|
|||||||
Reference in New Issue
Block a user