mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:54:01 +03:00
Merge pull request #421 from 9seconds/mips-save-mem
Decrease a relay buffer size for MIPS devices
This commit is contained in:
@@ -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
|
||||||
|
)
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user