From 7ae47fdb6ffbd19ce8be332a00770f1cc9c8a149 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 30 Jan 2023 09:59:10 +0800 Subject: [PATCH] fix: Fast Trace Bug && Performance Improvement --- fast_trace/fast_trace.go | 4 +++- trace/icmp_ipv4.go | 29 ++++++++++++++--------------- trace/icmp_ipv6.go | 30 +++++++++++++++--------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/fast_trace/fast_trace.go b/fast_trace/fast_trace.go index ee9e2db0..e0bbe6d6 100644 --- a/fast_trace/fast_trace.go +++ b/fast_trace/fast_trace.go @@ -43,6 +43,8 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { NumMeasurements: 3, ParallelRequests: 18, RDns: true, + PacketInterval: 100, + TTLInterval: 500, IPGeoSource: ipgeo.GetSource("LeoMoeAPI"), Timeout: 1 * time.Second, } @@ -106,7 +108,7 @@ func FastTest(tm bool, outEnable bool) { var c string oe = outEnable - + fmt.Println("Hi,欢迎使用 Fast Trace 功能,请注意 Fast Trace 功能只适合新手使用\n因为国内网络复杂,我们设置的测试目标有限,建议普通用户自测以获得更加精准的路由情况") fmt.Println("请您选择要测试的 IP 类型\n1. IPv4\n2. IPv6") fmt.Print("请选择选项:") fmt.Scanln(&c) diff --git a/trace/icmp_ipv4.go b/trace/icmp_ipv4.go index 474512ea..6e5d288d 100644 --- a/trace/icmp_ipv4.go +++ b/trace/icmp_ipv4.go @@ -18,14 +18,14 @@ import ( type ICMPTracer struct { Config - wg sync.WaitGroup - res Result - ctx context.Context - inflightRequest map[int]chan Hop - inflightRequestLock sync.Mutex - icmpListen net.PacketConn - final int - finalLock sync.Mutex + wg sync.WaitGroup + res Result + ctx context.Context + inflightRequest map[int]chan Hop + inflightRequestRWLock sync.RWMutex + icmpListen net.PacketConn + final int + finalLock sync.Mutex } func (t *ICMPTracer) PrintFunc() { @@ -52,9 +52,9 @@ func (t *ICMPTracer) PrintFunc() { } func (t *ICMPTracer) Execute() (*Result, error) { - t.inflightRequestLock.Lock() + t.inflightRequestRWLock.Lock() t.inflightRequest = make(map[int]chan Hop) - t.inflightRequestLock.Unlock() + t.inflightRequestRWLock.Unlock() if len(t.res.Hops) > 0 { return &t.res, ErrTracerouteExecuted @@ -77,9 +77,9 @@ func (t *ICMPTracer) Execute() (*Result, error) { t.wg.Add(1) go t.PrintFunc() for ttl := t.BeginHop; ttl <= t.MaxHops; ttl++ { - t.inflightRequestLock.Lock() + t.inflightRequestRWLock.Lock() t.inflightRequest[ttl] = make(chan Hop, t.NumMeasurements) - t.inflightRequestLock.Unlock() + t.inflightRequestRWLock.Unlock() if t.final != -1 && ttl > t.final { break } @@ -171,8 +171,8 @@ func (t *ICMPTracer) listenICMP() { } func (t *ICMPTracer) handleICMPMessage(msg ReceivedMessage, icmpType int8, data []byte, ttl int) { - t.inflightRequestLock.Lock() - defer t.inflightRequestLock.Unlock() + t.inflightRequestRWLock.RLock() + defer t.inflightRequestRWLock.RUnlock() if _, ok := t.inflightRequest[ttl]; ok { t.inflightRequest[ttl] <- Hop{ Success: true, @@ -272,7 +272,6 @@ func (t *ICMPTracer) send(ttl int) error { if err := t.icmpListen.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil { log.Fatal(err) } - select { case <-t.ctx.Done(): return nil diff --git a/trace/icmp_ipv6.go b/trace/icmp_ipv6.go index c723e8c5..8454e91f 100644 --- a/trace/icmp_ipv6.go +++ b/trace/icmp_ipv6.go @@ -16,15 +16,15 @@ import ( type ICMPTracerv6 struct { Config - wg sync.WaitGroup - res Result - ctx context.Context - resCh chan Hop - inflightRequest map[int]chan Hop - inflightRequestLock sync.Mutex - icmpListen net.PacketConn - final int - finalLock sync.Mutex + wg sync.WaitGroup + res Result + ctx context.Context + resCh chan Hop + inflightRequest map[int]chan Hop + inflightRequestRWLock sync.RWMutex + icmpListen net.PacketConn + final int + finalLock sync.Mutex } func (t *ICMPTracerv6) PrintFunc() { @@ -53,9 +53,9 @@ func (t *ICMPTracerv6) PrintFunc() { } func (t *ICMPTracerv6) Execute() (*Result, error) { - t.inflightRequestLock.Lock() + t.inflightRequestRWLock.Lock() t.inflightRequest = make(map[int]chan Hop) - t.inflightRequestLock.Unlock() + t.inflightRequestRWLock.Unlock() if len(t.res.Hops) > 0 { return &t.res, ErrTracerouteExecuted @@ -78,9 +78,9 @@ func (t *ICMPTracerv6) Execute() (*Result, error) { go t.listenICMP() go t.PrintFunc() for ttl := t.BeginHop; ttl <= t.MaxHops; ttl++ { - t.inflightRequestLock.Lock() + t.inflightRequestRWLock.Lock() t.inflightRequest[ttl] = make(chan Hop, t.NumMeasurements) - t.inflightRequestLock.Unlock() + t.inflightRequestRWLock.Unlock() if t.final != -1 && ttl > t.final { break } @@ -229,8 +229,8 @@ func (t *ICMPTracerv6) listenICMP() { } func (t *ICMPTracerv6) handleICMPMessage(msg ReceivedMessage, icmpType int8, data []byte, ttl int) { - t.inflightRequestLock.Lock() - defer t.inflightRequestLock.Unlock() + t.inflightRequestRWLock.RLock() + defer t.inflightRequestRWLock.RUnlock() if _, ok := t.inflightRequest[ttl]; ok { t.inflightRequest[ttl] <- Hop{ Success: true,