mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:24:03 +03:00
Add possibility to scale middleproxy buffers
This commit is contained in:
+1
-1
@@ -50,7 +50,7 @@ func Proxy() error { // nolint: funlen
|
|||||||
|
|
||||||
zap.S().Debugw("Configuration", "config", config.Printable())
|
zap.S().Debugw("Configuration", "config", config.Printable())
|
||||||
|
|
||||||
if len(config.C.AdTag) > 0 {
|
if config.C.MiddleProxyMode() {
|
||||||
zap.S().Infow("Use middle proxy connection to Telegram")
|
zap.S().Infow("Use middle proxy connection to Telegram")
|
||||||
|
|
||||||
diff, err := ntp.Fetch()
|
diff, err := ntp.Fetch()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/alecthomas/units"
|
"github.com/alecthomas/units"
|
||||||
@@ -104,6 +105,50 @@ type Config struct {
|
|||||||
AdTag []byte `json:"adtag"`
|
AdTag []byte `json:"adtag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) ClientReadBuffer() int {
|
||||||
|
return c.ReadBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) ClientWriteBuffer() int {
|
||||||
|
return c.WriteBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) MiddleProxyMode() bool {
|
||||||
|
return len(c.AdTag) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) ProxyReadBuffer() int {
|
||||||
|
value := c.ReadBuffer
|
||||||
|
|
||||||
|
if c.MiddleProxyMode() {
|
||||||
|
value = c.adjustProxyValue(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) ProxyWriteBuffer() int {
|
||||||
|
value := c.WriteBuffer
|
||||||
|
|
||||||
|
if c.MiddleProxyMode() {
|
||||||
|
value = c.adjustProxyValue(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) adjustProxyValue(value int) int {
|
||||||
|
if c.MultiplexPerConnection == 0 {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
fvalue := float64(value)
|
||||||
|
newValue := fvalue * 1.5 * math.Log2(float64(c.MultiplexPerConnection))
|
||||||
|
newValue = math.Max(fvalue, math.Ceil(newValue))
|
||||||
|
|
||||||
|
return int(newValue)
|
||||||
|
}
|
||||||
|
|
||||||
type Opt struct {
|
type Opt struct {
|
||||||
Option OptionType
|
Option OptionType
|
||||||
Value interface{}
|
Value interface{}
|
||||||
|
|||||||
+2
-2
@@ -51,7 +51,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
connID := conntypes.NewConnID()
|
connID := conntypes.NewConnID()
|
||||||
logger := p.Logger.With("connection_id", connID)
|
logger := p.Logger.With("connection_id", connID)
|
||||||
|
|
||||||
if err := utils.InitTCP(conn); err != nil {
|
if err := utils.InitTCP(conn, config.C.ClientReadBuffer(), config.C.ClientWriteBuffer()); err != nil {
|
||||||
logger.Errorw("Cannot initialize client TCP connection", "error", err)
|
logger.Errorw("Cannot initialize client TCP connection", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
|
|
||||||
err = nil
|
err = nil
|
||||||
|
|
||||||
if len(config.C.AdTag) > 0 {
|
if config.C.MiddleProxyMode() {
|
||||||
middleConnection(req)
|
middleConnection(req)
|
||||||
} else {
|
} else {
|
||||||
err = directConnection(req)
|
err = directConnection(req)
|
||||||
|
|||||||
+1
-1
@@ -37,7 +37,7 @@ func (b *baseTelegram) dial(dc conntypes.DC,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.InitTCP(conn); err != nil {
|
if err := utils.InitTCP(conn, config.C.ProxyReadBuffer(), config.C.ProxyWriteBuffer()); err != nil {
|
||||||
b.logger.Infow("Cannot initialize TCP socket", "address", addr, "error", err)
|
b.logger.Infow("Cannot initialize TCP socket", "address", addr, "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-5
@@ -4,24 +4,22 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const tcpKeepAlivePingPeriod = 2 * time.Second
|
const tcpKeepAlivePingPeriod = 2 * time.Second
|
||||||
|
|
||||||
func InitTCP(conn net.Conn) error {
|
func InitTCP(conn net.Conn, readBufferSize int, writeBufferSize int) error {
|
||||||
tcpConn := conn.(*net.TCPConn)
|
tcpConn := conn.(*net.TCPConn)
|
||||||
|
|
||||||
if err := tcpConn.SetNoDelay(true); err != nil {
|
if err := tcpConn.SetNoDelay(true); err != nil {
|
||||||
return fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
|
return fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tcpConn.SetReadBuffer(config.C.ReadBuffer); err != nil {
|
if err := tcpConn.SetReadBuffer(readBufferSize); err != nil {
|
||||||
return fmt.Errorf("cannot set read buffer size: %w", err)
|
return fmt.Errorf("cannot set read buffer size: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tcpConn.SetWriteBuffer(config.C.WriteBuffer); err != nil {
|
if err := tcpConn.SetWriteBuffer(writeBufferSize); err != nil {
|
||||||
return fmt.Errorf("cannot set write buffer size: %w", err)
|
return fmt.Errorf("cannot set write buffer size: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user