chore: reduce memory alloc
This commit is contained in:
parent
08094ce285
commit
bf64cede7d
3 changed files with 28 additions and 37 deletions
|
@ -23,7 +23,9 @@ type RejectOption struct {
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
if r.drop {
|
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
|
return NewConn(nopConn{}, r), nil
|
||||||
}
|
}
|
||||||
|
@ -31,7 +33,11 @@ func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
if r.drop {
|
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
|
return newPacketConn(&nopPacketConn{}, r), nil
|
||||||
}
|
}
|
||||||
|
@ -99,12 +105,8 @@ var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
||||||
|
|
||||||
type nopPacketConn struct{}
|
type nopPacketConn struct{}
|
||||||
|
|
||||||
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { return len(b), nil }
|
||||||
return len(b), nil
|
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF }
|
||||||
}
|
|
||||||
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
||||||
return 0, nil, io.EOF
|
|
||||||
}
|
|
||||||
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
||||||
return nil, nil, nil, io.EOF
|
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) SetReadDeadline(time.Time) error { return nil }
|
||||||
func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
type dropConn struct{}
|
type dropPacketConn struct {
|
||||||
|
conn net.Conn
|
||||||
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
|
|
||||||
}
|
}
|
||||||
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 newDropPacketConnWrapper(conn net.Conn) net.PacketConn {
|
||||||
|
return &dropPacketConn{conn}
|
||||||
func (npc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
}
|
||||||
time.Sleep(C.DefaultDropTime)
|
func (dpc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
func (npc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
func (dpc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF }
|
||||||
time.Sleep(C.DefaultDropTime)
|
func (dpc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
||||||
return 0, nil, io.EOF
|
|
||||||
}
|
|
||||||
func (npc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
|
||||||
return nil, nil, nil, io.EOF
|
return nil, nil, nil, io.EOF
|
||||||
}
|
}
|
||||||
func (npc dropPacketConn) Close() error { return nil }
|
func (dpc dropPacketConn) Close() error {
|
||||||
func (npc dropPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
dpc.conn = nil
|
||||||
func (npc dropPacketConn) SetDeadline(time.Time) error { return nil }
|
return nil
|
||||||
func (npc dropPacketConn) SetReadDeadline(time.Time) error { return nil }
|
}
|
||||||
func (npc dropPacketConn) SetWriteDeadline(time.Time) error { 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 }
|
||||||
|
|
|
@ -44,7 +44,7 @@ const (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultTCPTimeout = 5 * time.Second
|
DefaultTCPTimeout = 5 * time.Second
|
||||||
DefaultDropTime = 12 * DefaultTCPTimeout
|
DefaultDropTime = 30 * time.Second
|
||||||
DefaultUDPTimeout = DefaultTCPTimeout
|
DefaultUDPTimeout = DefaultTCPTimeout
|
||||||
DefaultTLSTimeout = DefaultTCPTimeout
|
DefaultTLSTimeout = DefaultTCPTimeout
|
||||||
DefaultMaxHealthCheckUrlNum = 16
|
DefaultMaxHealthCheckUrlNum = 16
|
||||||
|
|
|
@ -129,6 +129,7 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
||||||
conn2 = nil
|
conn2 = nil
|
||||||
}()
|
}()
|
||||||
var buff *buf.Buffer
|
var buff *buf.Buffer
|
||||||
|
defer buff.Release()
|
||||||
newBuffer := func() *buf.Buffer {
|
newBuffer := func() *buf.Buffer {
|
||||||
buff = buf.NewPacket() // do not use stack buffer
|
buff = buf.NewPacket() // do not use stack buffer
|
||||||
return buff
|
return buff
|
||||||
|
|
Loading…
Reference in a new issue