Decrease a relay buffer size for MIPS devices

This commit is contained in:
9seconds
2026-03-29 21:42:26 +02:00
parent 1725a0d721
commit d3a090d6b4
4 changed files with 43 additions and 12 deletions
@@ -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
)
@@ -0,0 +1,9 @@
//go:build !mips && !mipsle
package relay
import "github.com/9seconds/mtg/v2/mtglib/internal/tls"
const (
bufPoolSize = tls.MaxRecordPayloadSize
)
+18
View File
@@ -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)
}
+3 -12
View File
@@ -4,19 +4,10 @@ import (
"context" "context"
"errors" "errors"
"io" "io"
"sync"
"github.com/9seconds/mtg/v2/essentials" "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) { func Relay(ctx context.Context, log Logger, telegramConn, clientConn essentials.Conn) {
defer telegramConn.Close() //nolint: errcheck defer telegramConn.Close() //nolint: errcheck
defer clientConn.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) { func pump(log Logger, src, dst essentials.Conn, direction string) {
bp := bufPool.Get().(*[]byte) buf := acquireBuffer()
defer bufPool.Put(bp) defer releaseBuffer(buf)
defer src.CloseRead() //nolint: errcheck defer src.CloseRead() //nolint: errcheck
defer dst.CloseWrite() //nolint: errcheck defer dst.CloseWrite() //nolint: errcheck
n, err := io.CopyBuffer(src, dst, *bp) n, err := io.CopyBuffer(src, dst, *buf)
switch { switch {
case err == nil: case err == nil: