Skip to content

Commit 6cc5a7b

Browse files
uint48markpash
authored andcommitted
fix: buffer pool redundant allocation
1 parent 35e1db7 commit 6cc5a7b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ require (
7474
github.com/refraction-networking/gotapdance v1.7.10 // indirect
7575
github.com/refraction-networking/obfs4 v0.1.2 // indirect
7676
github.com/rogpeppe/go-internal v1.9.0 // indirect
77+
github.com/sagernet/sing v0.5.1 // indirect
7778
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507 // indirect
7879
github.com/sirupsen/logrus v1.9.3 // indirect
80+
github.com/stretchr/testify v1.9.0 // indirect
7981
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
8082
github.com/wader/filtertransport v0.0.0-20200316221534-bdd9e61eee78 // indirect
8183
gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib v1.5.0 // indirect

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
190190
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
191191
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735 h1:7YvPJVmEeFHR1Tj9sZEYsmarJEQfMVYpd/Vyy/A8dqE=
192192
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
193+
github.com/sagernet/sing v0.5.1 h1:mhL/MZVq0TjuvHcpYcFtmSD1BFOxZ/+8ofbNZcg1k1Y=
194+
github.com/sagernet/sing v0.5.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
193195
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507 h1:ML7ZNtcln5UBo5Wv7RIv9Xg3Pr5VuRCWLFXEwda54Y4=
194196
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507/go.mod h1:DbI1gxrXI2jRGw7XGEUZQOOMd6PsnKzRrCKabvvMrwM=
195197
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
@@ -202,8 +204,9 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
202204
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
203205
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
204206
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
205-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
206207
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
208+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
209+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
207210
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
208211
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
209212
github.com/things-go/go-socks5 v0.0.5 h1:qvKaGcBkfDrUL33SchHN93srAmYGzb4CxSM2DPYufe8=

wiresocks/proxy.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package wiresocks
33
import (
44
"context"
55
"errors"
6+
"github.com/sagernet/sing/common/buf"
67
"io"
78
"log/slog"
89
"net"
@@ -14,7 +15,6 @@ import (
1415
"github.com/bepass-org/warp-plus/proxy/pkg/statute"
1516
"github.com/bepass-org/warp-plus/wireguard/device"
1617
"github.com/bepass-org/warp-plus/wireguard/tun/netstack"
17-
"github.com/things-go/go-socks5/bufferpool"
1818
)
1919

2020
// VirtualTun stores a reference to netstack network and DNS configuration
@@ -23,9 +23,12 @@ type VirtualTun struct {
2323
Logger *slog.Logger
2424
Dev *device.Device
2525
Ctx context.Context
26-
pool bufferpool.BufPool
26+
pool buf.Allocator
27+
//pool bufferpool.BufPool
2728
}
2829

30+
var BuffSize = 65536
31+
2932
// StartProxy spawns a socks5 server.
3033
func StartProxy(ctx context.Context, l *slog.Logger, tnet *netstack.Net, bindAddress netip.AddrPort) (netip.AddrPort, error) {
3134
ln, err := net.Listen("tcp", bindAddress.String())
@@ -38,7 +41,7 @@ func StartProxy(ctx context.Context, l *slog.Logger, tnet *netstack.Net, bindAdd
3841
Logger: l.With("subsystem", "vtun"),
3942
Dev: nil,
4043
Ctx: ctx,
41-
pool: bufferpool.NewPool(256 * 1024),
44+
pool: buf.DefaultAllocator,
4245
}
4346

4447
proxy := mixed.NewProxy(
@@ -80,9 +83,11 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
8083
done := make(chan error, 1)
8184
// Copy data from req.Conn to conn
8285
go func() {
83-
buf1 := vt.pool.Get()
84-
defer vt.pool.Put(buf1)
85-
_, err := copyConnTimeout(conn, req.Conn, buf1[:cap(buf1)], timeout)
86+
buf1 := vt.pool.Get(BuffSize)
87+
defer func(pool buf.Allocator, buf []byte) {
88+
_ = pool.Put(buf)
89+
}(vt.pool, buf1)
90+
_, err := copyConnTimeout(conn, req.Conn, buf1, timeout)
8691
if errors.Is(err, syscall.ECONNRESET) {
8792
done <- nil
8893
return
@@ -91,9 +96,11 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
9196
}()
9297
// Copy data from conn to req.Conn
9398
go func() {
94-
buf2 := vt.pool.Get()
95-
defer vt.pool.Put(buf2)
96-
_, err := copyConnTimeout(req.Conn, conn, buf2[:cap(buf2)], timeout)
99+
buf2 := vt.pool.Get(BuffSize)
100+
defer func(pool buf.Allocator, buf []byte) {
101+
_ = pool.Put(buf)
102+
}(vt.pool, buf2)
103+
_, err := copyConnTimeout(req.Conn, conn, buf2, timeout)
97104
done <- err
98105
}()
99106
// Wait for one of the copy operations to finish

0 commit comments

Comments
 (0)