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