Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

fix: a type switch nit #83

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ func (as *AmbientAutoNAT) background() {
timer := time.NewTimer(delay)
defer timer.Stop()
timerRunning := true
var peer peer.ID

for {
select {
// new inbound connection.
Expand All @@ -188,7 +186,7 @@ func (as *AmbientAutoNAT) background() {
}

case e := <-subChan:
switch e.(type) {
switch e := e.(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this free? I'd avoided doing it since the typed event wasn't needed in the localAddressesUpdated case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct... by about a half a nanosecond. It saves a copy.

Up to you. I prefer this style because I like to avoid type panicing type assertions where possible.

case event.EvtLocalAddressesUpdated:
if !lastAddrUpdated.Add(time.Second).After(time.Now()) {
lastAddrUpdated = time.Now()
Expand All @@ -197,13 +195,14 @@ func (as *AmbientAutoNAT) background() {
}
}
case event.EvtPeerIdentificationCompleted:
peer = e.(event.EvtPeerIdentificationCompleted).Peer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the variable definition on line 174 since it's now unused, i think

if s, err := as.host.Peerstore().SupportsProtocols(peer, AutoNATProto); err == nil && len(s) > 0 {
if s, err := as.host.Peerstore().SupportsProtocols(e.Peer, AutoNATProto); err == nil && len(s) > 0 {
currentStatus := as.status.Load().(autoNATResult)
if currentStatus.Reachability == network.ReachabilityUnknown {
as.tryProbe(peer)
as.tryProbe(e.Peer)
}
}
default:
log.Errorf("unknown event type: %T", e)
}

// probe finished.
Expand All @@ -213,7 +212,7 @@ func (as *AmbientAutoNAT) background() {
}
as.recordObservation(result)
case <-timer.C:
peer = as.getPeerToProbe()
peer := as.getPeerToProbe()
as.tryProbe(peer)
timerRunning = false
case <-as.ctx.Done():
Expand Down