From a6b816b1c69abc1fffc9a686cac18196308d7c46 Mon Sep 17 00:00:00 2001 From: Larvan2 <78135608+Larvan2@users.noreply.github.com> Date: Mon, 20 Nov 2023 14:40:21 +0800 Subject: [PATCH] chore: reduce memory alloc --- adapter/outbound/reject.go | 62 ++++++++++++++++---------------------- constant/adapters.go | 2 +- listener/sing/sing.go | 1 + 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/adapter/outbound/reject.go b/adapter/outbound/reject.go index b564e28d..199c4452 100644 --- a/adapter/outbound/reject.go +++ b/adapter/outbound/reject.go @@ -23,7 +23,9 @@ type RejectOption struct { // DialContext implements C.ProxyAdapter func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) { if r.drop { - return NewConn(dropConn{}, r), nil + c, _ := net.Pipe() + _ = c.SetDeadline(time.Now().Add(C.DefaultDropTime)) + return NewConn(c, r), nil } return NewConn(nopConn{}, r), nil } @@ -31,7 +33,11 @@ func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ... // ListenPacketContext implements C.ProxyAdapter func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) { if r.drop { - return newPacketConn(&dropPacketConn{}, r), nil + c, _ := net.Pipe() + _ = c.SetDeadline(time.Now().Add(C.DefaultDropTime)) + pc := newDropPacketConnWrapper(c) + return newPacketConn(pc, r), nil + } return newPacketConn(&nopPacketConn{}, r), nil } @@ -99,12 +105,8 @@ var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0} type nopPacketConn struct{} -func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { - return len(b), nil -} -func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { - return 0, nil, io.EOF -} +func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { return len(b), nil } +func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF } func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) { return nil, nil, nil, io.EOF } @@ -114,37 +116,25 @@ func (npc nopPacketConn) SetDeadline(time.Time) error { return nil } func (npc nopPacketConn) SetReadDeadline(time.Time) error { return nil } func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil } -type dropConn struct{} - -func (rw dropConn) Read(b []byte) (int, error) { return 0, io.EOF } -func (rw dropConn) ReadBuffer(buffer *buf.Buffer) error { - time.Sleep(C.DefaultDropTime) - return io.EOF +type dropPacketConn struct { + conn net.Conn } -func (rw dropConn) Write(b []byte) (int, error) { return 0, io.EOF } -func (rw dropConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF } -func (rw dropConn) Close() error { return nil } -func (rw dropConn) LocalAddr() net.Addr { return nil } -func (rw dropConn) RemoteAddr() net.Addr { return nil } -func (rw dropConn) SetDeadline(time.Time) error { return nil } -func (rw dropConn) SetReadDeadline(time.Time) error { return nil } -func (rw dropConn) SetWriteDeadline(time.Time) error { return nil } -type dropPacketConn struct{} - -func (npc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { - time.Sleep(C.DefaultDropTime) +func newDropPacketConnWrapper(conn net.Conn) net.PacketConn { + return &dropPacketConn{conn} +} +func (dpc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { return len(b), nil } -func (npc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { - time.Sleep(C.DefaultDropTime) - return 0, nil, io.EOF -} -func (npc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) { +func (dpc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF } +func (dpc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) { return nil, nil, nil, io.EOF } -func (npc dropPacketConn) Close() error { return nil } -func (npc dropPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified } -func (npc dropPacketConn) SetDeadline(time.Time) error { return nil } -func (npc dropPacketConn) SetReadDeadline(time.Time) error { return nil } -func (npc dropPacketConn) SetWriteDeadline(time.Time) error { return nil } +func (dpc dropPacketConn) Close() error { + dpc.conn = nil + return nil +} +func (dpc dropPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified } +func (dpc dropPacketConn) SetDeadline(time.Time) error { return nil } +func (dpc dropPacketConn) SetReadDeadline(time.Time) error { return nil } +func (dpc dropPacketConn) SetWriteDeadline(time.Time) error { return nil } diff --git a/constant/adapters.go b/constant/adapters.go index 757068f3..a2fe15a2 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -44,7 +44,7 @@ const ( const ( DefaultTCPTimeout = 5 * time.Second - DefaultDropTime = 12 * DefaultTCPTimeout + DefaultDropTime = 30 * time.Second DefaultUDPTimeout = DefaultTCPTimeout DefaultTLSTimeout = DefaultTCPTimeout DefaultMaxHealthCheckUrlNum = 16 diff --git a/listener/sing/sing.go b/listener/sing/sing.go index 990bbb14..017c3f79 100644 --- a/listener/sing/sing.go +++ b/listener/sing/sing.go @@ -129,6 +129,7 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network. conn2 = nil }() var buff *buf.Buffer + defer buff.Release() newBuffer := func() *buf.Buffer { buff = buf.NewPacket() // do not use stack buffer return buff