Skip to content

Commit

Permalink
fix: Fast Trace Bug && Performance Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlleo committed Jan 30, 2023
1 parent 471412b commit 7ae47fd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
4 changes: 3 additions & 1 deletion fast_trace/fast_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions trace/icmp_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions trace/icmp_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7ae47fd

Please sign in to comment.