Procházet zdrojové kódy

Merge pull request #116 from 9seconds/gcoldconnections

Forcefully cleanup old connections
tags/v1.0.1
Sergey Arkhipov před 6 roky
rodič
revize
8fdd4292b1
Žádný účet není propojen s e-mailovou adresou tvůrce revize
2 změnil soubory, kde provedl 14 přidání a 6 odebrání
  1. 4
    5
      hub/connection_list.go
  2. 10
    1
      hub/mux.go

+ 4
- 5
hub/connection_list.go Zobrazit soubor

@@ -11,11 +11,7 @@ type connectionList struct {
11 11
 	connections []*connection
12 12
 }
13 13
 
14
-func (c *connectionList) Get(conn *ProxyConn) (*connection, error) {
15
-	if len(c.connections) > 0 {
16
-		c.gc()
17
-	}
18
-
14
+func (c *connectionList) get(conn *ProxyConn) (*connection, error) {
19 15
 	if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
20 16
 		if err := c.connections[0].Attach(conn); err == nil {
21 17
 			return c.connections[0], nil
@@ -41,6 +37,9 @@ func (c *connectionList) Get(conn *ProxyConn) (*connection, error) {
41 37
 
42 38
 func (c *connectionList) gc() {
43 39
 	prevLen := len(c.connections)
40
+	if prevLen == 0 {
41
+		return
42
+	}
44 43
 
45 44
 	for i := len(c.connections) - 1; i >= 0; i-- {
46 45
 		lastIndex := len(c.connections) - 1

+ 10
- 1
hub/mux.go Zobrazit soubor

@@ -2,11 +2,14 @@ package hub
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"time"
5 6
 
6 7
 	"mtg/conntypes"
7 8
 	"mtg/protocol"
8 9
 )
9 10
 
11
+const muxGCEvery = time.Minute
12
+
10 13
 type muxNewRequest struct {
11 14
 	req  *protocol.TelegramRequest
12 15
 	resp chan<- muxNewResponse
@@ -26,6 +29,9 @@ type mux struct {
26 29
 }
27 30
 
28 31
 func (m *mux) run() {
32
+	gcTicker := time.NewTicker(muxGCEvery)
33
+	defer gcTicker.Stop()
34
+
29 35
 	for {
30 36
 		select {
31 37
 		case <-m.ctx.Done():
@@ -34,9 +40,12 @@ func (m *mux) run() {
34 40
 			}
35 41
 
36 42
 			return
43
+		case <-gcTicker.C:
44
+			m.connections.gc()
37 45
 		case req := <-m.channelNew:
46
+			m.connections.gc()
38 47
 			proxyConn := newProxyConn(req.req, m.channelClosed)
39
-			conn, err := m.connections.Get(proxyConn)
48
+			conn, err := m.connections.get(proxyConn)
40 49
 
41 50
 			if err == nil {
42 51
 				m.clients[string(req.req.ConnID[:])] = conn

Načítá se…
Zrušit
Uložit