diff --git a/mtglib/internal/relay/pool_settings_mips.go b/mtglib/internal/relay/pool_settings_mips.go new file mode 100644 index 000000000..21d34a942 --- /dev/null +++ b/mtglib/internal/relay/pool_settings_mips.go @@ -0,0 +1,13 @@ +//go:build mips || mipsle + +package relay + +import "github.com/9seconds/mtg/v2/mtglib/internal/tls" + +const ( + // MIPS is quite short in resources, and usually it means that it will run + // on Microtiks, OpenWRT-based routers or similar hardware. I think it worth + // to sacrifice a number of read syscalls (read, CPU load) to shrink + // limited RAM resources. + bufPoolSize = tls.MaxRecordPayloadSize / 2 +) diff --git a/mtglib/internal/relay/pool_settings_other.go b/mtglib/internal/relay/pool_settings_other.go new file mode 100644 index 000000000..a5f1c1452 --- /dev/null +++ b/mtglib/internal/relay/pool_settings_other.go @@ -0,0 +1,9 @@ +//go:build !mips && !mipsle + +package relay + +import "github.com/9seconds/mtg/v2/mtglib/internal/tls" + +const ( + bufPoolSize = tls.MaxRecordPayloadSize +) diff --git a/mtglib/internal/relay/pools.go b/mtglib/internal/relay/pools.go new file mode 100644 index 000000000..8fb123086 --- /dev/null +++ b/mtglib/internal/relay/pools.go @@ -0,0 +1,18 @@ +package relay + +import "sync" + +var bufPool = sync.Pool{ + New: func() any { + b := make([]byte, bufPoolSize) + return &b + }, +} + +func acquireBuffer() *[]byte { + return bufPool.Get().(*[]byte) +} + +func releaseBuffer(p *[]byte) { + bufPool.Put(p) +} diff --git a/mtglib/internal/relay/relay.go b/mtglib/internal/relay/relay.go index 70ee14072..e734b713a 100644 --- a/mtglib/internal/relay/relay.go +++ b/mtglib/internal/relay/relay.go @@ -4,19 +4,10 @@ import ( "context" "errors" "io" - "sync" "github.com/9seconds/mtg/v2/essentials" - "github.com/9seconds/mtg/v2/mtglib/internal/tls" ) -var bufPool = sync.Pool{ - New: func() any { - b := make([]byte, tls.MaxRecordPayloadSize) - return &b - }, -} - func Relay(ctx context.Context, log Logger, telegramConn, clientConn essentials.Conn) { defer telegramConn.Close() //nolint: errcheck defer clientConn.Close() //nolint: errcheck @@ -44,13 +35,13 @@ func Relay(ctx context.Context, log Logger, telegramConn, clientConn essentials. } func pump(log Logger, src, dst essentials.Conn, direction string) { - bp := bufPool.Get().(*[]byte) - defer bufPool.Put(bp) + buf := acquireBuffer() + defer releaseBuffer(buf) defer src.CloseRead() //nolint: errcheck defer dst.CloseWrite() //nolint: errcheck - n, err := io.CopyBuffer(src, dst, *bp) + n, err := io.CopyBuffer(src, dst, *buf) switch { case err == nil: