mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
chore: improve read waiter interface
This commit is contained in:
parent
4c38b2f0bf
commit
fe51e95d88
2
go.mod
2
go.mod
|
@ -29,7 +29,7 @@ require (
|
|||
github.com/openacid/low v0.1.21
|
||||
github.com/oschwald/geoip2-golang v1.8.0
|
||||
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97
|
||||
github.com/sagernet/sing v0.2.5-0.20230510160026-237a991b46d0
|
||||
github.com/sagernet/sing v0.2.5-0.20230512033628-9be7806bab51
|
||||
github.com/sagernet/sing-mux v0.0.0-20230427141602-9836fc9b052e
|
||||
github.com/sagernet/sing-shadowtls v0.1.2-0.20230417103049-4f682e05f19b
|
||||
github.com/sagernet/sing-vmess v0.1.5-0.20230417103030-8c3070ae3fb3
|
||||
|
|
4
go.sum
4
go.sum
|
@ -143,8 +143,8 @@ github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6E
|
|||
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
|
||||
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
|
||||
github.com/sagernet/sing v0.2.5-0.20230510160026-237a991b46d0 h1:NS4U+yQ4ToyRqFvCS91BM1D6fQbriW1qvioxSj5PvI4=
|
||||
github.com/sagernet/sing v0.2.5-0.20230510160026-237a991b46d0/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
||||
github.com/sagernet/sing v0.2.5-0.20230512033628-9be7806bab51 h1:ySJuouhl890pBZly51Hkb2mbDnz+qSQCLF5j+4IFHis=
|
||||
github.com/sagernet/sing v0.2.5-0.20230512033628-9be7806bab51/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
||||
github.com/sagernet/sing-mux v0.0.0-20230427141602-9836fc9b052e h1:t8nuY9plpHEzlnPxOpuv64jhjz3teIvccu3YMFX4fJI=
|
||||
github.com/sagernet/sing-mux v0.0.0-20230427141602-9836fc9b052e/go.mod h1:pF+RnLvCAOhECrvauy6LYOpBakJ/vuaF1Wm4lPsWryI=
|
||||
github.com/sagernet/sing-shadowtls v0.1.2-0.20230417103049-4f682e05f19b h1:ouW/6IDCrxkBe19YSbdCd7buHix7b+UZ6BM4Zz74XF4=
|
||||
|
|
|
@ -124,21 +124,23 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
|||
defer mutex.Unlock()
|
||||
conn2 = nil
|
||||
}()
|
||||
connReader := network.UnwrapPacketReader(conn) // decrease runtime cost for bufio.CreatePacketReadWaiter
|
||||
var buff *buf.Buffer
|
||||
newBuffer := func() *buf.Buffer {
|
||||
buff = buf.NewPacket() // do not use stack buffer
|
||||
return buff
|
||||
}
|
||||
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(conn)
|
||||
if isReadWaiter {
|
||||
readWaiter.InitializeReadWaiter(newBuffer)
|
||||
}
|
||||
for {
|
||||
var (
|
||||
buff *buf.Buffer
|
||||
dest M.Socksaddr
|
||||
err error
|
||||
)
|
||||
newBuffer := func() *buf.Buffer {
|
||||
buff = buf.NewPacket() // do not use stack buffer
|
||||
return buff
|
||||
}
|
||||
// syscallPacketReadWaiter.WaitReadPacket will cache newBuffer function
|
||||
// so create new PacketReadWaiter in each loop
|
||||
if readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(connReader); isReadWaiter {
|
||||
dest, err = readWaiter.WaitReadPacket(newBuffer)
|
||||
buff = nil // clear last loop status, avoid repeat release
|
||||
if isReadWaiter {
|
||||
dest, err = readWaiter.WaitReadPacket()
|
||||
} else {
|
||||
dest, err = conn.ReadPacket(newBuffer())
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@ import (
|
|||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
"github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
type Listener struct {
|
||||
|
@ -93,21 +92,23 @@ func New(config LC.ShadowsocksServer, tcpIn chan<- C.ConnContext, udpIn chan<- C
|
|||
|
||||
go func() {
|
||||
conn := bufio.NewPacketConn(ul)
|
||||
connReader := network.UnwrapPacketReader(conn) // decrease runtime cost for bufio.CreatePacketReadWaiter
|
||||
var buff *buf.Buffer
|
||||
newBuffer := func() *buf.Buffer {
|
||||
buff = buf.NewPacket() // do not use stack buffer
|
||||
return buff
|
||||
}
|
||||
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(conn)
|
||||
if isReadWaiter {
|
||||
readWaiter.InitializeReadWaiter(newBuffer)
|
||||
}
|
||||
for {
|
||||
var (
|
||||
buff *buf.Buffer
|
||||
dest M.Socksaddr
|
||||
err error
|
||||
)
|
||||
newBuffer := func() *buf.Buffer {
|
||||
buff = buf.NewPacket() // do not use stack buffer
|
||||
return buff
|
||||
}
|
||||
// syscallPacketReadWaiter.WaitReadPacket will cache newBuffer function
|
||||
// so create new PacketReadWaiter in each loop
|
||||
if readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(connReader); isReadWaiter {
|
||||
dest, err = readWaiter.WaitReadPacket(newBuffer)
|
||||
buff = nil // clear last loop status, avoid repeat release
|
||||
if isReadWaiter {
|
||||
dest, err = readWaiter.WaitReadPacket()
|
||||
} else {
|
||||
dest, err = conn.ReadPacket(newBuffer())
|
||||
}
|
||||
|
|
|
@ -110,24 +110,26 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
|||
conn2 = nil
|
||||
}()
|
||||
|
||||
connReader := network.UnwrapPacketReader(conn) // decrease runtime cost for bufio.CreatePacketReadWaiter
|
||||
var buff *buf.Buffer
|
||||
newBuffer := func() *buf.Buffer {
|
||||
// safe size which is 1232 from https://dnsflagday.net/2020/.
|
||||
// so 2048 is enough
|
||||
buff = buf.NewSize(2 * 1024)
|
||||
return buff
|
||||
}
|
||||
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(conn)
|
||||
if isReadWaiter {
|
||||
readWaiter.InitializeReadWaiter(newBuffer)
|
||||
}
|
||||
for {
|
||||
var (
|
||||
buff *buf.Buffer
|
||||
dest M.Socksaddr
|
||||
err error
|
||||
)
|
||||
newBuffer := func() *buf.Buffer {
|
||||
// safe size which is 1232 from https://dnsflagday.net/2020/.
|
||||
// so 2048 is enough
|
||||
buff = buf.NewSize(2 * 1024)
|
||||
return buff
|
||||
}
|
||||
_ = conn.SetReadDeadline(time.Now().Add(DefaultDnsReadTimeout))
|
||||
// syscallPacketReadWaiter.WaitReadPacket will cache newBuffer function
|
||||
// so create new PacketReadWaiter in each loop
|
||||
if readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(connReader); isReadWaiter {
|
||||
dest, err = readWaiter.WaitReadPacket(newBuffer)
|
||||
buff = nil // clear last loop status, avoid repeat release
|
||||
if isReadWaiter {
|
||||
dest, err = readWaiter.WaitReadPacket()
|
||||
} else {
|
||||
dest, err = conn.ReadPacket(newBuffer())
|
||||
}
|
||||
|
@ -140,7 +142,7 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
|||
}
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
go func(buff *buf.Buffer) {
|
||||
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
|
||||
defer cancel()
|
||||
inData := buff.Bytes()
|
||||
|
@ -165,7 +167,7 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
}(buff) // catch buff at goroutine create, avoid next loop change buff
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user