Przeglądaj źródła

Add possibility to scale middleproxy buffers

tags/v1.0.5^2
9seconds 6 lat temu
rodzic
commit
b5a8d8b804
5 zmienionych plików z 52 dodań i 9 usunięć
  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 Wyświetl plik

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

+ 45
- 0
config/config.go Wyświetl plik

@@ -6,6 +6,7 @@ import (
6 6
 	"encoding/json"
7 7
 	"errors"
8 8
 	"fmt"
9
+	"math"
9 10
 	"net"
10 11
 
11 12
 	"github.com/alecthomas/units"
@@ -104,6 +105,50 @@ type Config struct {
104 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 152
 type Opt struct {
108 153
 	Option OptionType
109 154
 	Value  interface{}

+ 2
- 2
proxy/proxy.go Wyświetl plik

@@ -51,7 +51,7 @@ func (p *Proxy) accept(conn net.Conn) {
51 51
 	connID := conntypes.NewConnID()
52 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 55
 		logger.Errorw("Cannot initialize client TCP connection", "error", err)
56 56
 		return
57 57
 	}
@@ -90,7 +90,7 @@ func (p *Proxy) accept(conn net.Conn) {
90 90
 
91 91
 	err = nil
92 92
 
93
-	if len(config.C.AdTag) > 0 {
93
+	if config.C.MiddleProxyMode() {
94 94
 		middleConnection(req)
95 95
 	} else {
96 96
 		err = directConnection(req)

+ 1
- 1
telegram/base.go Wyświetl plik

@@ -37,7 +37,7 @@ func (b *baseTelegram) dial(dc conntypes.DC,
37 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 41
 			b.logger.Infow("Cannot initialize TCP socket", "address", addr, "error", err)
42 42
 			continue
43 43
 		}

+ 3
- 5
utils/init_tcp.go Wyświetl plik

@@ -4,24 +4,22 @@ import (
4 4
 	"fmt"
5 5
 	"net"
6 6
 	"time"
7
-
8
-	"github.com/9seconds/mtg/config"
9 7
 )
10 8
 
11 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 12
 	tcpConn := conn.(*net.TCPConn)
15 13
 
16 14
 	if err := tcpConn.SetNoDelay(true); err != nil {
17 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 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 23
 		return fmt.Errorf("cannot set write buffer size: %w", err)
26 24
 	}
27 25
 

Ładowanie…
Anuluj
Zapisz