From b98e9ea202b81189546da0fb6528d36d2cfd2c13 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Thu, 29 Oct 2020 00:32:31 +0800 Subject: [PATCH] Improve: #1038 and #1041 --- tunnel/tracker.go | 9 +++++---- tunnel/tunnel.go | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tunnel/tracker.go b/tunnel/tracker.go index e39caec7..19e79110 100644 --- a/tunnel/tracker.go +++ b/tunnel/tracker.go @@ -2,6 +2,7 @@ package tunnel import ( "net" + "sync/atomic" "time" C "github.com/Dreamacro/clash/constant" @@ -38,7 +39,7 @@ func (tt *tcpTracker) Read(b []byte) (int, error) { n, err := tt.Conn.Read(b) download := int64(n) tt.manager.PushDownloaded(download) - tt.DownloadTotal += download + atomic.AddInt64(&tt.DownloadTotal, download) return n, err } @@ -46,7 +47,7 @@ func (tt *tcpTracker) Write(b []byte) (int, error) { n, err := tt.Conn.Write(b) upload := int64(n) tt.manager.PushUploaded(upload) - tt.UploadTotal += upload + atomic.AddInt64(&tt.UploadTotal, upload) return n, err } @@ -93,7 +94,7 @@ func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) { n, addr, err := ut.PacketConn.ReadFrom(b) download := int64(n) ut.manager.PushDownloaded(download) - ut.DownloadTotal += download + atomic.AddInt64(&ut.DownloadTotal, download) return n, addr, err } @@ -101,7 +102,7 @@ func (ut *udpTracker) WriteTo(b []byte, addr net.Addr) (int, error) { n, err := ut.PacketConn.WriteTo(b, addr) upload := int64(n) ut.manager.PushUploaded(upload) - ut.UploadTotal += upload + atomic.AddInt64(&ut.UploadTotal, upload) return n, err } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 2e7b9696..e6a274b0 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -42,7 +42,10 @@ func Add(req C.ServerAdapter) { // AddPacket add udp Packet to queue func AddPacket(packet *inbound.PacketAdapter) { - udpQueue <- packet + select { + case udpQueue <- packet: + default: + } } // Rules return all rules