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

Merge pull request #127 from 9seconds/close-connections

Implement connection TTLs for multiplexing
tags/v1.0.2^2
Sergey Arkhipov 6 лет назад
Родитель
Сommit
5cc07a54a2
Аккаунт пользователя с таким Email не найден
1 измененных файлов: 9 добавлений и 0 удалений
  1. 9
    0
      hub/connection.go

+ 9
- 0
hub/connection.go Просмотреть файл

4
 	"fmt"
4
 	"fmt"
5
 	"math/rand"
5
 	"math/rand"
6
 	"sync"
6
 	"sync"
7
+	"time"
7
 
8
 
8
 	"go.uber.org/zap"
9
 	"go.uber.org/zap"
9
 
10
 
13
 	"github.com/9seconds/mtg/protocol"
14
 	"github.com/9seconds/mtg/protocol"
14
 )
15
 )
15
 
16
 
17
+const connectionTTL = time.Hour
18
+
16
 type connection struct {
19
 type connection struct {
17
 	conn            conntypes.PacketReadWriteCloser
20
 	conn            conntypes.PacketReadWriteCloser
18
 	proxyConns      map[string]*ProxyConn
21
 	proxyConns      map[string]*ProxyConn
31
 func (c *connection) run() {
34
 func (c *connection) run() {
32
 	defer c.Close()
35
 	defer c.Close()
33
 
36
 
37
+	ttl := time.NewTimer(connectionTTL)
38
+	defer ttl.Stop()
39
+
34
 	for {
40
 	for {
35
 		select {
41
 		select {
36
 		case <-c.channelDone:
42
 		case <-c.channelDone:
39
 			}
45
 			}
40
 
46
 
41
 			return
47
 			return
48
+		case <-ttl.C:
49
+			c.logger.Debugw("Closing connection by TTL")
50
+			c.Close()
42
 		case resp := <-c.channelRead:
51
 		case resp := <-c.channelRead:
43
 			if channel, ok := c.proxyConns[string(resp.ConnID[:])]; ok {
52
 			if channel, ok := c.proxyConns[string(resp.ConnID[:])]; ok {
44
 				if resp.Type == rpc.ProxyResponseTypeCloseExt {
53
 				if resp.Type == rpc.ProxyResponseTypeCloseExt {

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