Просмотр исходного кода

Merge pull request #116 from 9seconds/gcoldconnections

Forcefully cleanup old connections
tags/v1.0.1
Sergey Arkhipov 6 лет назад
Родитель
Сommit
8fdd4292b1
Аккаунт пользователя с таким Email не найден
2 измененных файлов: 14 добавлений и 6 удалений
  1. 4
    5
      hub/connection_list.go
  2. 10
    1
      hub/mux.go

+ 4
- 5
hub/connection_list.go Просмотреть файл

11
 	connections []*connection
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
 	if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
15
 	if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
20
 		if err := c.connections[0].Attach(conn); err == nil {
16
 		if err := c.connections[0].Attach(conn); err == nil {
21
 			return c.connections[0], nil
17
 			return c.connections[0], nil
41
 
37
 
42
 func (c *connectionList) gc() {
38
 func (c *connectionList) gc() {
43
 	prevLen := len(c.connections)
39
 	prevLen := len(c.connections)
40
+	if prevLen == 0 {
41
+		return
42
+	}
44
 
43
 
45
 	for i := len(c.connections) - 1; i >= 0; i-- {
44
 	for i := len(c.connections) - 1; i >= 0; i-- {
46
 		lastIndex := len(c.connections) - 1
45
 		lastIndex := len(c.connections) - 1

+ 10
- 1
hub/mux.go Просмотреть файл

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

Загрузка…
Отмена
Сохранить