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

Add possibility to scale middleproxy buffers

tags/v1.0.5^2
9seconds 6 лет назад
Родитель
Сommit
b5a8d8b804
5 измененных файлов: 52 добавлений и 9 удалений
  1. 1
    1
      cli/proxy.go
  2. 45
    0
      config/config.go
  3. 2
    2
      proxy/proxy.go
  4. 1
    1
      telegram/base.go
  5. 3
    5
      utils/init_tcp.go

+ 1
- 1
cli/proxy.go Просмотреть файл

50
 
50
 
51
 	zap.S().Debugw("Configuration", "config", config.Printable())
51
 	zap.S().Debugw("Configuration", "config", config.Printable())
52
 
52
 
53
-	if len(config.C.AdTag) > 0 {
53
+	if config.C.MiddleProxyMode() {
54
 		zap.S().Infow("Use middle proxy connection to Telegram")
54
 		zap.S().Infow("Use middle proxy connection to Telegram")
55
 
55
 
56
 		diff, err := ntp.Fetch()
56
 		diff, err := ntp.Fetch()

+ 45
- 0
config/config.go Просмотреть файл

6
 	"encoding/json"
6
 	"encoding/json"
7
 	"errors"
7
 	"errors"
8
 	"fmt"
8
 	"fmt"
9
+	"math"
9
 	"net"
10
 	"net"
10
 
11
 
11
 	"github.com/alecthomas/units"
12
 	"github.com/alecthomas/units"
104
 	AdTag  []byte `json:"adtag"`
105
 	AdTag  []byte `json:"adtag"`
105
 }
106
 }
106
 
107
 
108
+func (c *Config) ClientReadBuffer() int {
109
+	return c.ReadBuffer
110
+}
111
+
112
+func (c *Config) ClientWriteBuffer() int {
113
+	return c.WriteBuffer
114
+}
115
+
116
+func (c *Config) MiddleProxyMode() bool {
117
+	return len(c.AdTag) > 0
118
+}
119
+
120
+func (c *Config) ProxyReadBuffer() int {
121
+	value := c.ReadBuffer
122
+
123
+	if c.MiddleProxyMode() {
124
+		value = c.adjustProxyValue(value)
125
+	}
126
+
127
+	return value
128
+}
129
+
130
+func (c *Config) ProxyWriteBuffer() int {
131
+	value := c.WriteBuffer
132
+
133
+	if c.MiddleProxyMode() {
134
+		value = c.adjustProxyValue(value)
135
+	}
136
+
137
+	return value
138
+}
139
+
140
+func (c *Config) adjustProxyValue(value int) int {
141
+	if c.MultiplexPerConnection == 0 {
142
+		return value
143
+	}
144
+
145
+	fvalue := float64(value)
146
+	newValue := fvalue * 1.5 * math.Log2(float64(c.MultiplexPerConnection))
147
+	newValue = math.Max(fvalue, math.Ceil(newValue))
148
+
149
+	return int(newValue)
150
+}
151
+
107
 type Opt struct {
152
 type Opt struct {
108
 	Option OptionType
153
 	Option OptionType
109
 	Value  interface{}
154
 	Value  interface{}

+ 2
- 2
proxy/proxy.go Просмотреть файл

51
 	connID := conntypes.NewConnID()
51
 	connID := conntypes.NewConnID()
52
 	logger := p.Logger.With("connection_id", connID)
52
 	logger := p.Logger.With("connection_id", connID)
53
 
53
 
54
-	if err := utils.InitTCP(conn); err != nil {
54
+	if err := utils.InitTCP(conn, config.C.ClientReadBuffer(), config.C.ClientWriteBuffer()); err != nil {
55
 		logger.Errorw("Cannot initialize client TCP connection", "error", err)
55
 		logger.Errorw("Cannot initialize client TCP connection", "error", err)
56
 		return
56
 		return
57
 	}
57
 	}
90
 
90
 
91
 	err = nil
91
 	err = nil
92
 
92
 
93
-	if len(config.C.AdTag) > 0 {
93
+	if config.C.MiddleProxyMode() {
94
 		middleConnection(req)
94
 		middleConnection(req)
95
 	} else {
95
 	} else {
96
 		err = directConnection(req)
96
 		err = directConnection(req)

+ 1
- 1
telegram/base.go Просмотреть файл

37
 			continue
37
 			continue
38
 		}
38
 		}
39
 
39
 
40
-		if err := utils.InitTCP(conn); err != nil {
40
+		if err := utils.InitTCP(conn, config.C.ProxyReadBuffer(), config.C.ProxyWriteBuffer()); err != nil {
41
 			b.logger.Infow("Cannot initialize TCP socket", "address", addr, "error", err)
41
 			b.logger.Infow("Cannot initialize TCP socket", "address", addr, "error", err)
42
 			continue
42
 			continue
43
 		}
43
 		}

+ 3
- 5
utils/init_tcp.go Просмотреть файл

4
 	"fmt"
4
 	"fmt"
5
 	"net"
5
 	"net"
6
 	"time"
6
 	"time"
7
-
8
-	"github.com/9seconds/mtg/config"
9
 )
7
 )
10
 
8
 
11
 const tcpKeepAlivePingPeriod = 2 * time.Second
9
 const tcpKeepAlivePingPeriod = 2 * time.Second
12
 
10
 
13
-func InitTCP(conn net.Conn) error {
11
+func InitTCP(conn net.Conn, readBufferSize int, writeBufferSize int) error {
14
 	tcpConn := conn.(*net.TCPConn)
12
 	tcpConn := conn.(*net.TCPConn)
15
 
13
 
16
 	if err := tcpConn.SetNoDelay(true); err != nil {
14
 	if err := tcpConn.SetNoDelay(true); err != nil {
17
 		return fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
15
 		return fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
18
 	}
16
 	}
19
 
17
 
20
-	if err := tcpConn.SetReadBuffer(config.C.ReadBuffer); err != nil {
18
+	if err := tcpConn.SetReadBuffer(readBufferSize); err != nil {
21
 		return fmt.Errorf("cannot set read buffer size: %w", err)
19
 		return fmt.Errorf("cannot set read buffer size: %w", err)
22
 	}
20
 	}
23
 
21
 
24
-	if err := tcpConn.SetWriteBuffer(config.C.WriteBuffer); err != nil {
22
+	if err := tcpConn.SetWriteBuffer(writeBufferSize); err != nil {
25
 		return fmt.Errorf("cannot set write buffer size: %w", err)
23
 		return fmt.Errorf("cannot set write buffer size: %w", err)
26
 	}
24
 	}
27
 
25
 

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