Browse Source

Merge pull request #421 from 9seconds/mips-save-mem

Decrease a relay buffer size for MIPS devices
tags/v2.2.5^2^2
Sergei Arkhipov 1 month ago
parent
commit
0ad3a06863
No account linked to committer's email address

+ 13
- 0
mtglib/internal/relay/pool_settings_mips.go View File

@@ -0,0 +1,13 @@
1
+//go:build mips || mipsle
2
+
3
+package relay
4
+
5
+import "github.com/9seconds/mtg/v2/mtglib/internal/tls"
6
+
7
+const (
8
+	// MIPS is quite short in resources, and usually it means that it will run
9
+	// on Microtiks, OpenWRT-based routers or similar hardware. I think it worth
10
+	// to sacrifice a number of read syscalls (read, CPU load) to shrink
11
+	// limited RAM resources.
12
+	bufPoolSize = tls.MaxRecordPayloadSize / 2
13
+)

+ 9
- 0
mtglib/internal/relay/pool_settings_other.go View File

@@ -0,0 +1,9 @@
1
+//go:build !mips && !mipsle
2
+
3
+package relay
4
+
5
+import "github.com/9seconds/mtg/v2/mtglib/internal/tls"
6
+
7
+const (
8
+	bufPoolSize = tls.MaxRecordPayloadSize
9
+)

+ 18
- 0
mtglib/internal/relay/pools.go View File

@@ -0,0 +1,18 @@
1
+package relay
2
+
3
+import "sync"
4
+
5
+var bufPool = sync.Pool{
6
+	New: func() any {
7
+		b := make([]byte, bufPoolSize)
8
+		return &b
9
+	},
10
+}
11
+
12
+func acquireBuffer() *[]byte {
13
+	return bufPool.Get().(*[]byte)
14
+}
15
+
16
+func releaseBuffer(p *[]byte) {
17
+	bufPool.Put(p)
18
+}

+ 3
- 12
mtglib/internal/relay/relay.go View File

@@ -4,19 +4,10 @@ import (
4 4
 	"context"
5 5
 	"errors"
6 6
 	"io"
7
-	"sync"
8 7
 
9 8
 	"github.com/9seconds/mtg/v2/essentials"
10
-	"github.com/9seconds/mtg/v2/mtglib/internal/tls"
11 9
 )
12 10
 
13
-var bufPool = sync.Pool{
14
-	New: func() any {
15
-		b := make([]byte, tls.MaxRecordPayloadSize)
16
-		return &b
17
-	},
18
-}
19
-
20 11
 func Relay(ctx context.Context, log Logger, telegramConn, clientConn essentials.Conn) {
21 12
 	defer telegramConn.Close() //nolint: errcheck
22 13
 	defer clientConn.Close()   //nolint: errcheck
@@ -44,13 +35,13 @@ func Relay(ctx context.Context, log Logger, telegramConn, clientConn essentials.
44 35
 }
45 36
 
46 37
 func pump(log Logger, src, dst essentials.Conn, direction string) {
47
-	bp := bufPool.Get().(*[]byte)
48
-	defer bufPool.Put(bp)
38
+	buf := acquireBuffer()
39
+	defer releaseBuffer(buf)
49 40
 
50 41
 	defer src.CloseRead()  //nolint: errcheck
51 42
 	defer dst.CloseWrite() //nolint: errcheck
52 43
 
53
-	n, err := io.CopyBuffer(src, dst, *bp)
44
+	n, err := io.CopyBuffer(src, dst, *buf)
54 45
 
55 46
 	switch {
56 47
 	case err == nil:

Loading…
Cancel
Save