Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mtglib/internal/relay/pool_settings_mips.go
Original file line number Diff line number Diff line change
@@ -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
)
9 changes: 9 additions & 0 deletions mtglib/internal/relay/pool_settings_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !mips && !mipsle

package relay

import "github.com/9seconds/mtg/v2/mtglib/internal/tls"

const (
bufPoolSize = tls.MaxRecordPayloadSize
)
18 changes: 18 additions & 0 deletions mtglib/internal/relay/pools.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 3 additions & 12 deletions mtglib/internal/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading