mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 19:14:01 +03:00
Forcefully cleanup old connections
This commit is contained in:
@@ -11,11 +11,7 @@ type connectionList struct {
|
|||||||
connections []*connection
|
connections []*connection
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectionList) Get(conn *ProxyConn) (*connection, error) {
|
func (c *connectionList) get(conn *ProxyConn) (*connection, error) {
|
||||||
if len(c.connections) > 0 {
|
|
||||||
c.gc()
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
|
if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
|
||||||
if err := c.connections[0].Attach(conn); err == nil {
|
if err := c.connections[0].Attach(conn); err == nil {
|
||||||
return c.connections[0], nil
|
return c.connections[0], nil
|
||||||
@@ -41,6 +37,9 @@ func (c *connectionList) Get(conn *ProxyConn) (*connection, error) {
|
|||||||
|
|
||||||
func (c *connectionList) gc() {
|
func (c *connectionList) gc() {
|
||||||
prevLen := len(c.connections)
|
prevLen := len(c.connections)
|
||||||
|
if prevLen == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for i := len(c.connections) - 1; i >= 0; i-- {
|
for i := len(c.connections) - 1; i >= 0; i-- {
|
||||||
lastIndex := len(c.connections) - 1
|
lastIndex := len(c.connections) - 1
|
||||||
|
|||||||
+10
-1
@@ -2,11 +2,14 @@ package hub
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"mtg/conntypes"
|
"mtg/conntypes"
|
||||||
"mtg/protocol"
|
"mtg/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const muxGCEvery = time.Minute
|
||||||
|
|
||||||
type muxNewRequest struct {
|
type muxNewRequest struct {
|
||||||
req *protocol.TelegramRequest
|
req *protocol.TelegramRequest
|
||||||
resp chan<- muxNewResponse
|
resp chan<- muxNewResponse
|
||||||
@@ -26,6 +29,9 @@ type mux struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *mux) run() {
|
func (m *mux) run() {
|
||||||
|
gcTicker := time.NewTicker(muxGCEvery)
|
||||||
|
defer gcTicker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-m.ctx.Done():
|
case <-m.ctx.Done():
|
||||||
@@ -34,9 +40,12 @@ func (m *mux) run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
case <-gcTicker.C:
|
||||||
|
m.connections.gc()
|
||||||
case req := <-m.channelNew:
|
case req := <-m.channelNew:
|
||||||
|
m.connections.gc()
|
||||||
proxyConn := newProxyConn(req.req, m.channelClosed)
|
proxyConn := newProxyConn(req.req, m.channelClosed)
|
||||||
conn, err := m.connections.Get(proxyConn)
|
conn, err := m.connections.get(proxyConn)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
m.clients[string(req.req.ConnID[:])] = conn
|
m.clients[string(req.req.ConnID[:])] = conn
|
||||||
|
|||||||
Reference in New Issue
Block a user