mirror of
https://github.com/go-ping/ping.git
synced 2025-06-25 13:41:45 +00:00
make InflightPackets
private
This commit is contained in:
parent
7d19def034
commit
035eb2feee
16
ping.go
16
ping.go
@ -108,7 +108,7 @@ func New(addr string) *Pinger {
|
|||||||
ipv4: false,
|
ipv4: false,
|
||||||
network: "ip",
|
network: "ip",
|
||||||
protocol: "udp",
|
protocol: "udp",
|
||||||
InFlightPackets: firstSequence,
|
inFlightPackets: firstSequence,
|
||||||
TTL: 64,
|
TTL: 64,
|
||||||
logger: StdLogger{Logger: log.New(log.Writer(), log.Prefix(), log.Flags())},
|
logger: StdLogger{Logger: log.New(log.Writer(), log.Prefix(), log.Flags())},
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ type Pinger struct {
|
|||||||
id int
|
id int
|
||||||
sequence int
|
sequence int
|
||||||
// awaitingSequences are in-flight sequence numbers we keep track of to help remove duplicate receipts
|
// awaitingSequences are in-flight sequence numbers we keep track of to help remove duplicate receipts
|
||||||
InFlightPackets map[uuid.UUID]map[int]InFlightPacket
|
inFlightPackets map[uuid.UUID]map[int]InFlightPacket
|
||||||
// network is one of "ip", "ip4", or "ip6".
|
// network is one of "ip", "ip4", or "ip6".
|
||||||
network string
|
network string
|
||||||
// protocol is "icmp" or "udp".
|
// protocol is "icmp" or "udp".
|
||||||
@ -526,10 +526,10 @@ func (p *Pinger) CheckInFlightPackets() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
for id, inflight := range p.InFlightPackets {
|
for id, inflight := range p.inFlightPackets {
|
||||||
for seq, pkt := range inflight {
|
for seq, pkt := range inflight {
|
||||||
if pkt.DispatchedTime.Add(p.PacketTimeout).Before(currentTime) {
|
if pkt.DispatchedTime.Add(p.PacketTimeout).Before(currentTime) {
|
||||||
delete(p.InFlightPackets[id], seq)
|
delete(p.inFlightPackets[id], seq)
|
||||||
if p.OnTimeout != nil {
|
if p.OnTimeout != nil {
|
||||||
p.OnTimeout(seq, &pkt)
|
p.OnTimeout(seq, &pkt)
|
||||||
}
|
}
|
||||||
@ -712,7 +712,7 @@ func (p *Pinger) processPacket(recv *packet) error {
|
|||||||
inPkt.Rtt = receivedAt.Sub(timestamp)
|
inPkt.Rtt = receivedAt.Sub(timestamp)
|
||||||
inPkt.Seq = pkt.Seq
|
inPkt.Seq = pkt.Seq
|
||||||
// If we've already received this sequence, ignore it.
|
// If we've already received this sequence, ignore it.
|
||||||
if _, inflight := p.InFlightPackets[*pktUUID][pkt.Seq]; !inflight {
|
if _, inflight := p.inFlightPackets[*pktUUID][pkt.Seq]; !inflight {
|
||||||
p.PacketsRecvDuplicates++
|
p.PacketsRecvDuplicates++
|
||||||
if p.OnDuplicateRecv != nil {
|
if p.OnDuplicateRecv != nil {
|
||||||
p.OnDuplicateRecv(inPkt)
|
p.OnDuplicateRecv(inPkt)
|
||||||
@ -720,7 +720,7 @@ func (p *Pinger) processPacket(recv *packet) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// remove it from the list of sequences we're waiting for so we don't get duplicates.
|
// remove it from the list of sequences we're waiting for so we don't get duplicates.
|
||||||
delete(p.InFlightPackets[*pktUUID], pkt.Seq)
|
delete(p.inFlightPackets[*pktUUID], pkt.Seq)
|
||||||
p.updateStatistics(inPkt)
|
p.updateStatistics(inPkt)
|
||||||
default:
|
default:
|
||||||
// Very bad, not sure how this can happen
|
// Very bad, not sure how this can happen
|
||||||
@ -789,7 +789,7 @@ func (p *Pinger) sendICMP(conn packetConn) error {
|
|||||||
handler(outPkt)
|
handler(outPkt)
|
||||||
}
|
}
|
||||||
// mark this sequence as in-flight
|
// mark this sequence as in-flight
|
||||||
p.InFlightPackets[currentUUID][p.sequence] = InFlightPacket{
|
p.inFlightPackets[currentUUID][p.sequence] = InFlightPacket{
|
||||||
DispatchedTime: time.Now(),
|
DispatchedTime: time.Now(),
|
||||||
}
|
}
|
||||||
p.PacketsSent++
|
p.PacketsSent++
|
||||||
@ -797,7 +797,7 @@ func (p *Pinger) sendICMP(conn packetConn) error {
|
|||||||
if p.sequence > 65535 {
|
if p.sequence > 65535 {
|
||||||
newUUID := uuid.New()
|
newUUID := uuid.New()
|
||||||
p.trackerUUIDs = append(p.trackerUUIDs, newUUID)
|
p.trackerUUIDs = append(p.trackerUUIDs, newUUID)
|
||||||
p.InFlightPackets[newUUID] = make(map[int]InFlightPacket)
|
p.inFlightPackets[newUUID] = make(map[int]InFlightPacket)
|
||||||
p.sequence = 0
|
p.sequence = 0
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@ -38,7 +38,7 @@ func TestProcessPacket(t *testing.T) {
|
|||||||
Seq: pinger.sequence,
|
Seq: pinger.sequence,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
pinger.InFlightPackets[currentUUID][pinger.sequence] = InFlightPacket{}
|
pinger.inFlightPackets[currentUUID][pinger.sequence] = InFlightPacket{}
|
||||||
|
|
||||||
msg := &icmp.Message{
|
msg := &icmp.Message{
|
||||||
Type: ipv4.ICMPTypeEchoReply,
|
Type: ipv4.ICMPTypeEchoReply,
|
||||||
@ -608,7 +608,7 @@ func TestProcessPacket_IgnoresDuplicateSequence(t *testing.T) {
|
|||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
// register the sequence as sent
|
// register the sequence as sent
|
||||||
pinger.InFlightPackets[currentUUID][0] = InFlightPacket{}
|
pinger.inFlightPackets[currentUUID][0] = InFlightPacket{}
|
||||||
|
|
||||||
msg := &icmp.Message{
|
msg := &icmp.Message{
|
||||||
Type: ipv4.ICMPTypeEchoReply,
|
Type: ipv4.ICMPTypeEchoReply,
|
||||||
|
Loading…
Reference in New Issue
Block a user