fix: Fast Trace Bug && Performance Improvement

This commit is contained in:
Leo
2023-01-30 09:59:10 +08:00
parent 471412b740
commit 7ae47fdb6f
3 changed files with 32 additions and 31 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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,