From 21b9bb98a62457849cd2c7c533dd8e410aaae7af Mon Sep 17 00:00:00 2001 From: keakon Date: Fri, 26 Jan 2024 14:57:12 +0800 Subject: [PATCH] remove chan --- .../hysteria2/extras/transport/udphop/conn.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/transport/hysteria2/extras/transport/udphop/conn.go b/transport/hysteria2/extras/transport/udphop/conn.go index 8975b5c3..3c875ad0 100644 --- a/transport/hysteria2/extras/transport/udphop/conn.go +++ b/transport/hysteria2/extras/transport/udphop/conn.go @@ -19,8 +19,7 @@ type udpHopPacketConn struct { conn net.PacketConn addrIndex int - closeChan chan struct{} - closed bool + closed bool } type udpPacket struct { @@ -50,7 +49,6 @@ func NewUDPHopPacketConn(addr *UDPHopAddr, hopInterval time.Duration) (net.Packe HopInterval: hopInterval, conn: conn, addrIndex: index, - closeChan: make(chan struct{}), } go hConn.hopLoop() return hConn, nil @@ -62,19 +60,20 @@ func (u *udpHopPacketConn) hopLoop() { for { select { case <-ticker.C: - u.hop() - case <-u.closeChan: - return + if u.hop() { + return + } } } } -func (u *udpHopPacketConn) hop() { +func (u *udpHopPacketConn) hop() bool { if u.closed { - return + return true } u.addrIndex = rand.Intn(len(u.Addrs)) log.Infoln("hopped to %s", u.Addrs[u.addrIndex]) + return false } func (u *udpHopPacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) { @@ -93,7 +92,6 @@ func (u *udpHopPacketConn) Close() error { return nil } err := u.conn.Close() - close(u.closeChan) u.closed = true u.Addrs = nil // For GC return err