mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 12:04:03 +03:00
Get rid of buffersize everywhere
This commit is contained in:
@@ -61,6 +61,8 @@ const (
|
||||
DefaultConcurrency = 4096
|
||||
|
||||
// DefaultBufferSize is a default size of a copy buffer.
|
||||
//
|
||||
// Deprecated: this setting no longer makes any effect.
|
||||
DefaultBufferSize = 16 * 1024 // 16 kib
|
||||
|
||||
// DefaultDomainFrontingPort is a default port (HTTPS) to connect to in
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
net.Conn
|
||||
}
|
||||
|
||||
func (c conn) Read(p []byte) (int, error) {
|
||||
if err := c.SetReadDeadline(time.Now().Add(getTimeout())); err != nil {
|
||||
return 0, fmt.Errorf("cannot set read deadline: %w", err)
|
||||
}
|
||||
|
||||
return c.Conn.Read(p) // nolint: wrapcheck
|
||||
}
|
||||
@@ -1,12 +1,7 @@
|
||||
package relay
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
ConnectionTimeToLiveMin = 2 * time.Minute
|
||||
ConnectionTimeToLiveMax = 10 * time.Minute
|
||||
TimeoutMin = 20 * time.Second
|
||||
TimeoutMax = time.Minute
|
||||
bufferSize = 32 * 1024
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
|
||||
@@ -9,21 +9,16 @@ type eastWest struct {
|
||||
|
||||
var eastWestPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &eastWest{}
|
||||
return &eastWest{
|
||||
east: make([]byte, bufferSize),
|
||||
west: make([]byte, bufferSize),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func acquireEastWest(bufferSize int) *eastWest {
|
||||
func acquireEastWest() *eastWest {
|
||||
wanted := eastWestPool.Get().(*eastWest) // nolint: forcetypeassert
|
||||
|
||||
if len(wanted.east) != bufferSize {
|
||||
wanted.east = make([]byte, bufferSize)
|
||||
}
|
||||
|
||||
if len(wanted.west) != bufferSize {
|
||||
wanted.west = make([]byte, bufferSize)
|
||||
}
|
||||
|
||||
return wanted
|
||||
}
|
||||
|
||||
|
||||
@@ -3,16 +3,14 @@ package relay
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func Relay(ctx context.Context, log Logger, bufferSize int,
|
||||
telegramConn net.Conn, clientConn io.ReadWriteCloser) {
|
||||
func Relay(ctx context.Context, log Logger, telegramConn, clientConn io.ReadWriteCloser) {
|
||||
defer telegramConn.Close()
|
||||
defer clientConn.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, getConnectionTimeToLive())
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
@@ -21,13 +19,9 @@ func Relay(ctx context.Context, log Logger, bufferSize int,
|
||||
clientConn.Close()
|
||||
}()
|
||||
|
||||
buffers := acquireEastWest(bufferSize)
|
||||
buffers := acquireEastWest()
|
||||
defer releaseEastWest(buffers)
|
||||
|
||||
telegramConn = conn{
|
||||
Conn: telegramConn,
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(2) // nolint: gomnd
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ func (suite *RelayTestSuite) TearDownTest() {
|
||||
}
|
||||
|
||||
func (suite *RelayTestSuite) TestExit() {
|
||||
suite.telegramConnMock.On("SetReadDeadline", mock.Anything).Return(nil)
|
||||
suite.telegramConnMock.On("Close").Return(nil)
|
||||
suite.telegramConnMock.On("Read", mock.Anything).Return(10, io.EOF).Once()
|
||||
suite.telegramConnMock.On("Write", mock.Anything).Return(10, io.EOF).Maybe()
|
||||
@@ -46,8 +45,7 @@ func (suite *RelayTestSuite) TestExit() {
|
||||
suite.clientConnMock.On("Write", mock.Anything).Return(10, io.EOF).Maybe()
|
||||
suite.clientConnMock.On("Close").Return(nil)
|
||||
|
||||
relay.Relay(suite.ctx, suite.loggerMock, 1024,
|
||||
suite.telegramConnMock, suite.clientConnMock)
|
||||
relay.Relay(suite.ctx, suite.loggerMock, suite.telegramConnMock, suite.clientConnMock)
|
||||
}
|
||||
|
||||
func TestRelay(t *testing.T) {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getConnectionTimeToLive() time.Duration {
|
||||
return getTime(ConnectionTimeToLiveMin, ConnectionTimeToLiveMax)
|
||||
}
|
||||
|
||||
func getTimeout() time.Duration {
|
||||
return getTime(TimeoutMin, TimeoutMax)
|
||||
}
|
||||
|
||||
func getTime(minDuration, maxDuration time.Duration) time.Duration {
|
||||
minDurationInSeconds := int(minDuration.Seconds())
|
||||
maxDurationInSeconds := int(maxDuration.Seconds())
|
||||
number := minDurationInSeconds + rand.Intn(maxDurationInSeconds-minDurationInSeconds)
|
||||
|
||||
return time.Duration(number) * time.Second
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type TimeoutsTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (suite *TimeoutsTestSuite) TestGetConnectionTimeToLive() {
|
||||
for i := 0; i < 100; i++ {
|
||||
value := getConnectionTimeToLive()
|
||||
message := fmt.Sprintf("generated value is %v", value)
|
||||
|
||||
suite.GreaterOrEqual(value, ConnectionTimeToLiveMin, message)
|
||||
suite.LessOrEqual(value, ConnectionTimeToLiveMax, message)
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *TimeoutsTestSuite) TestGetTimeout() {
|
||||
for i := 0; i < 100; i++ {
|
||||
value := getTimeout()
|
||||
message := fmt.Sprintf("generated value is %v", value)
|
||||
|
||||
suite.GreaterOrEqual(value, TimeoutMin, message)
|
||||
suite.LessOrEqual(value, TimeoutMax, message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeouts(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &TimeoutsTestSuite{})
|
||||
}
|
||||
@@ -25,7 +25,6 @@ type Proxy struct {
|
||||
|
||||
allowFallbackOnUnknownDC bool
|
||||
tolerateTimeSkewness time.Duration
|
||||
bufferSize int
|
||||
domainFrontingPort int
|
||||
workerPool *ants.PoolWithFunc
|
||||
telegram *telegram.Telegram
|
||||
@@ -84,7 +83,6 @@ func (p *Proxy) ServeConn(conn net.Conn) {
|
||||
relay.Relay(
|
||||
ctx,
|
||||
ctx.logger.Named("relay"),
|
||||
p.bufferSize,
|
||||
ctx.telegramConn,
|
||||
ctx.clientConn,
|
||||
)
|
||||
@@ -267,7 +265,6 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
|
||||
relay.Relay(
|
||||
ctx,
|
||||
ctx.logger.Named("domain-fronting"),
|
||||
p.bufferSize,
|
||||
frontConn,
|
||||
conn,
|
||||
)
|
||||
@@ -296,7 +293,6 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) {
|
||||
logger: opts.getLogger("proxy"),
|
||||
domainFrontingPort: opts.getDomainFrontingPort(),
|
||||
tolerateTimeSkewness: opts.getTolerateTimeSkewness(),
|
||||
bufferSize: opts.getBufferSize(),
|
||||
allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
|
||||
telegram: tg,
|
||||
}
|
||||
|
||||
@@ -131,14 +131,6 @@ func (p ProxyOpts) valid() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p ProxyOpts) getBufferSize() int {
|
||||
if p.BufferSize < 1 {
|
||||
return DefaultBufferSize
|
||||
}
|
||||
|
||||
return int(p.BufferSize)
|
||||
}
|
||||
|
||||
func (p ProxyOpts) getConcurrency() int {
|
||||
if p.Concurrency == 0 {
|
||||
return DefaultConcurrency
|
||||
|
||||
@@ -30,11 +30,6 @@ func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
|
||||
suite.Error(err)
|
||||
}
|
||||
|
||||
func (suite *DefaultDialerTestSuite) TestNegativeBufferSize() {
|
||||
_, err := network.NewDefaultDialer(0, -1)
|
||||
suite.Error(err)
|
||||
}
|
||||
|
||||
func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
|
||||
_, err := suite.d.DialContext(context.Background(),
|
||||
"udp",
|
||||
|
||||
Reference in New Issue
Block a user