Skip to content

Commit

Permalink
don't emit events unless reachability changed
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Feb 14, 2023
1 parent 3d9cc01 commit d3d3173
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
25 changes: 11 additions & 14 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,18 @@ func (as *AmbientAutoNAT) scheduleProbe() time.Duration {
// Update the current status based on an observed result.
func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
currentStatus := as.status.Load()

// Ignore public observations with no address
if observation.Reachability == network.ReachabilityPublic && observation.address == nil {
return
}

if observation.Reachability == network.ReachabilityPublic {
log.Debugf("NAT status is public")
changed := false
if currentStatus.Reachability != network.ReachabilityPublic {
// Aggressively switch to public from other states ignoring confidence

// we are flipping our NATStatus, so confidence drops to 0
as.confidence = 0
if as.service != nil {
Expand All @@ -299,21 +307,13 @@ func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
} else if as.confidence < 3 {
as.confidence++
}
if observation.address != nil {
if !changed && currentStatus.address != nil && !observation.address.Equal(currentStatus.address) {
as.confidence--
}
if currentStatus.address == nil || !observation.address.Equal(currentStatus.address) {
changed = true
}
as.status.Store(&observation)
}
if observation.address != nil && changed {
as.status.Store(&observation)
if changed {
as.emitStatus()
}
} else if observation.Reachability == network.ReachabilityPrivate {
log.Debugf("NAT status is private")
if currentStatus.Reachability == network.ReachabilityPublic {
if currentStatus.Reachability != network.ReachabilityPrivate {
if as.confidence > 0 {
as.confidence--
} else {
Expand All @@ -328,9 +328,6 @@ func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
} else if as.confidence < 3 {
as.confidence++
as.status.Store(&observation)
if currentStatus.Reachability != network.ReachabilityPrivate {
as.emitStatus()
}
}
} else if as.confidence > 0 {
// don't just flip to unknown, reduce confidence first
Expand Down
15 changes: 15 additions & 0 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ func TestAutoNATObservationRecording(t *testing.T) {
t.Fatalf("too-extreme private transition.")
}

// don't emit events on observed address change
newAddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/udp/12345")
an.recordObservation(autoNATResult{network.ReachabilityPublic, newAddr})
if an.Status() != network.ReachabilityPublic {
t.Fatalf("reachability should stay public")
}
select {
case e := <-s.Out():
_, ok := e.(event.EvtLocalReachabilityChanged)
if ok {
t.Fatal("received event without state transition")
}

case <-time.After(1 * time.Second):
}
}

func TestStaticNat(t *testing.T) {
Expand Down

0 comments on commit d3d3173

Please sign in to comment.