From fc4c5c4c55b788f742a4c57e5209a89f19ed7196 Mon Sep 17 00:00:00 2001 From: Jason Lyu Date: Sun, 1 Sep 2024 04:23:25 +0800 Subject: [PATCH] Refactor(pool): move to internal (#398) --- common/pool/buffer.go | 17 ----------------- {common => internal}/pool/alloc.go | 3 +-- {common => internal}/pool/alloc_test.go | 0 internal/pool/buffer.go | 17 +++++++++++++++++ {common => internal}/pool/pool.go | 0 proxy/relay.go | 2 +- transport/shadowsocks/shadowaead/packet.go | 2 +- transport/shadowsocks/shadowaead/stream.go | 2 +- transport/shadowsocks/shadowstream/packet.go | 2 +- transport/simple-obfs/http.go | 2 +- transport/simple-obfs/tls.go | 2 +- tunnel/tcp.go | 2 +- tunnel/udp.go | 2 +- 13 files changed, 26 insertions(+), 27 deletions(-) delete mode 100644 common/pool/buffer.go rename {common => internal}/pool/alloc.go (96%) rename {common => internal}/pool/alloc_test.go (100%) create mode 100644 internal/pool/buffer.go rename {common => internal}/pool/pool.go (100%) diff --git a/common/pool/buffer.go b/common/pool/buffer.go deleted file mode 100644 index 18d14b81..00000000 --- a/common/pool/buffer.go +++ /dev/null @@ -1,17 +0,0 @@ -package pool - -import ( - "bytes" - "sync" -) - -var bufferPool = sync.Pool{New: func() any { return &bytes.Buffer{} }} - -func GetBuffer() *bytes.Buffer { - return bufferPool.Get().(*bytes.Buffer) -} - -func PutBuffer(buf *bytes.Buffer) { - buf.Reset() - bufferPool.Put(buf) -} diff --git a/common/pool/alloc.go b/internal/pool/alloc.go similarity index 96% rename from common/pool/alloc.go rename to internal/pool/alloc.go index 80f320f1..9a4cc106 100644 --- a/common/pool/alloc.go +++ b/internal/pool/alloc.go @@ -51,8 +51,7 @@ func (alloc *Allocator) Put(buf []byte) error { return errors.New("allocator Put() incorrect buffer size") } - //lint:ignore SA6002 ignore temporarily - //nolint + //nolint:staticcheck alloc.buffers[b].Put(buf) return nil } diff --git a/common/pool/alloc_test.go b/internal/pool/alloc_test.go similarity index 100% rename from common/pool/alloc_test.go rename to internal/pool/alloc_test.go diff --git a/internal/pool/buffer.go b/internal/pool/buffer.go new file mode 100644 index 00000000..9762b087 --- /dev/null +++ b/internal/pool/buffer.go @@ -0,0 +1,17 @@ +package pool + +import ( + "bytes" + "sync" +) + +var _bufferPool = sync.Pool{New: func() any { return &bytes.Buffer{} }} + +func GetBuffer() *bytes.Buffer { + return _bufferPool.Get().(*bytes.Buffer) +} + +func PutBuffer(buf *bytes.Buffer) { + buf.Reset() + _bufferPool.Put(buf) +} diff --git a/common/pool/pool.go b/internal/pool/pool.go similarity index 100% rename from common/pool/pool.go rename to internal/pool/pool.go diff --git a/proxy/relay.go b/proxy/relay.go index 2fba60af..87343e7a 100644 --- a/proxy/relay.go +++ b/proxy/relay.go @@ -13,8 +13,8 @@ import ( "github.com/go-gost/relay" - "github.com/xjasonlyu/tun2socks/v2/common/pool" "github.com/xjasonlyu/tun2socks/v2/dialer" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" M "github.com/xjasonlyu/tun2socks/v2/metadata" "github.com/xjasonlyu/tun2socks/v2/proxy/proto" ) diff --git a/transport/shadowsocks/shadowaead/packet.go b/transport/shadowsocks/shadowaead/packet.go index ecbb914d..91b0bf4c 100755 --- a/transport/shadowsocks/shadowaead/packet.go +++ b/transport/shadowsocks/shadowaead/packet.go @@ -6,7 +6,7 @@ import ( "io" "net" - "github.com/xjasonlyu/tun2socks/v2/common/pool" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" ) // ErrShortPacket means that the packet is too short for a valid encrypted packet. diff --git a/transport/shadowsocks/shadowaead/stream.go b/transport/shadowsocks/shadowaead/stream.go index a6b4d30b..83cc6410 100755 --- a/transport/shadowsocks/shadowaead/stream.go +++ b/transport/shadowsocks/shadowaead/stream.go @@ -7,7 +7,7 @@ import ( "io" "net" - "github.com/xjasonlyu/tun2socks/v2/common/pool" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" ) const ( diff --git a/transport/shadowsocks/shadowstream/packet.go b/transport/shadowsocks/shadowstream/packet.go index 54b5ece2..082a6d59 100755 --- a/transport/shadowsocks/shadowstream/packet.go +++ b/transport/shadowsocks/shadowstream/packet.go @@ -6,7 +6,7 @@ import ( "io" "net" - "github.com/xjasonlyu/tun2socks/v2/common/pool" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" ) // ErrShortPacket means the packet is too short to be a valid encrypted packet. diff --git a/transport/simple-obfs/http.go b/transport/simple-obfs/http.go index f9a2ed1e..bdae1a35 100644 --- a/transport/simple-obfs/http.go +++ b/transport/simple-obfs/http.go @@ -10,7 +10,7 @@ import ( "net" "net/http" - "github.com/xjasonlyu/tun2socks/v2/common/pool" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" ) // HTTPObfs is shadowsocks http simple-obfs implementation diff --git a/transport/simple-obfs/tls.go b/transport/simple-obfs/tls.go index b3d5d1cb..a9a39263 100644 --- a/transport/simple-obfs/tls.go +++ b/transport/simple-obfs/tls.go @@ -8,7 +8,7 @@ import ( "net" "time" - "github.com/xjasonlyu/tun2socks/v2/common/pool" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" ) const ( diff --git a/tunnel/tcp.go b/tunnel/tcp.go index 45af37cb..6ac563dc 100644 --- a/tunnel/tcp.go +++ b/tunnel/tcp.go @@ -7,8 +7,8 @@ import ( "sync" "time" - "github.com/xjasonlyu/tun2socks/v2/common/pool" "github.com/xjasonlyu/tun2socks/v2/core/adapter" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" "github.com/xjasonlyu/tun2socks/v2/log" M "github.com/xjasonlyu/tun2socks/v2/metadata" "github.com/xjasonlyu/tun2socks/v2/tunnel/statistic" diff --git a/tunnel/udp.go b/tunnel/udp.go index 667a92bf..03c5e3eb 100644 --- a/tunnel/udp.go +++ b/tunnel/udp.go @@ -6,8 +6,8 @@ import ( "sync" "time" - "github.com/xjasonlyu/tun2socks/v2/common/pool" "github.com/xjasonlyu/tun2socks/v2/core/adapter" + "github.com/xjasonlyu/tun2socks/v2/internal/pool" "github.com/xjasonlyu/tun2socks/v2/log" M "github.com/xjasonlyu/tun2socks/v2/metadata" "github.com/xjasonlyu/tun2socks/v2/tunnel/statistic"