mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-14 05:11:17 +08:00
chore: reduce memory alloc
This commit is contained in:
parent
bb9ad6cac0
commit
a6b816b1c6
@ -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 }
|
||||
|
@ -44,7 +44,7 @@ const (
|
||||
|
||||
const (
|
||||
DefaultTCPTimeout = 5 * time.Second
|
||||
DefaultDropTime = 12 * DefaultTCPTimeout
|
||||
DefaultDropTime = 30 * time.Second
|
||||
DefaultUDPTimeout = DefaultTCPTimeout
|
||||
DefaultTLSTimeout = DefaultTCPTimeout
|
||||
DefaultMaxHealthCheckUrlNum = 16
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user