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

react to incoming events #65

Merged
merged 4 commits into from
Apr 30, 2020
Merged

react to incoming events #65

merged 4 commits into from
Apr 30, 2020

Conversation

willscott
Copy link
Contributor

largely addresses #57

autonat.go Outdated Show resolved Hide resolved
autonat.go Outdated
timerRunning = true
}
}

// scheduleProbe calculates when the next probe should be scheduled for,
// and launches it if that time is now.
func (as *AmbientAutoNAT) scheduleProbe() time.Duration {
func (as *AmbientAutoNAT) scheduleProbe(peer peer.ID) time.Duration {
Copy link
Member

Choose a reason for hiding this comment

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

This function is starting to do way too many things, only one of which is scheduling a probe.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @Stebalien. I tried reading it to understand what it does and it does take some effort.

autonat.go Outdated Show resolved Hide resolved
@Stebalien
Copy link
Member

Given that this isn't critical to land immediately, I'd like to think through the right way to do this. The old code was highly geared towards periodic probing and this feels like we're trying to bolt on probing specific peers. I'd consider making this two separate code paths.

@willscott
Copy link
Contributor Author

restructured the functions to make things more straight forward, hopefully addressing the main confusion. (although there's a bit of 2 flows happening in the main eventloop + timer scheduling functions still)

@aarshkshah1992 aarshkshah1992 self-requested a review April 8, 2020 09:14
autonat.go Outdated Show resolved Hide resolved
autonat.go Outdated
timerRunning = true
}
}

// scheduleProbe calculates when the next probe should be scheduled for,
// and launches it if that time is now.
func (as *AmbientAutoNAT) scheduleProbe() time.Duration {
func (as *AmbientAutoNAT) scheduleProbe(peer peer.ID) time.Duration {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @Stebalien. I tried reading it to understand what it does and it does take some effort.

autonat.go Outdated Show resolved Hide resolved
autonat.go Show resolved Hide resolved
@willscott
Copy link
Contributor Author

this has been waiting for review for a while, @Stebalien / @aarshkshah1992 - will make startup quite a bit faster.

@Stebalien
Copy link
Member

This still doesn't make much sense to me. scheduleProbe was designed to schedule periodic probes, not probe specific peers as we discover them. If we're going to start reacting to events and probing specific peers, we're going to need to rethink this. For example, when would it actually make sens to not probe new peers immediately?

Let's re-think this from the ground up:

  1. When and why does it make sense to schedule a periodic probe? When and why does it make sense to probe immediately?
  2. When should we probe specific peers versus all peers?

@willscott
Copy link
Contributor Author

Did a fairly major re-factor of this PR.

  • Added a map tracking recent probes, to avoid requerying the same peer. That helps with complexity.
  • functions should be better about doing one thing.

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

This is so much easier to read. I have some nits but merge when you've addressed them.

autonat.go Outdated
addrUpdatedChan := subAddrUpdated.Out()
defer subAddrUpdated.Close()

subIDOccured, _ := as.host.EventBus().Subscribe(new(event.EvtPeerIdentificationCompleted))
Copy link
Member

Choose a reason for hiding this comment

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

Check error?

Copy link
Member

Choose a reason for hiding this comment

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

Also, we can probably just subscribe to both events at the same time.

autonat.go Outdated
// peer identification occured.
case idev := <-IDChan:
peer = idev.(event.EvtPeerIdentificationCompleted).Peer
if s, err := as.host.Peerstore().SupportsProtocols(peer, AutoNATProto); err != nil || len(s) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

If currentStatus is Unknown? We don't want to probe everyone.


for {
select {
// new connection occured.
// new inbound connection.
case conn := <-as.inboundConn:
Copy link
Member

Choose a reason for hiding this comment

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

We should also check "is the remote addr public". We can do this in a followup PR.

autonat.go Show resolved Hide resolved
@@ -321,13 +376,13 @@ func (as *AmbientAutoNAT) probe(pi *peer.AddrInfo) {
}
Copy link
Member

Choose a reason for hiding this comment

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

OT: above: we need to select on the service context in case we shutdown.

if !as.config.dialPolicy.skipPeer(info.Addrs) {
addrs = append(addrs, info)
candidates = append(candidates, p)
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: use if ... { continue } for all checks, then append at the end. It makes this kind of logic a bit easier to read.

@Stebalien
Copy link
Member

For "OT" things, feel free to file issues or address in a followup.

@willscott willscott merged commit d5ed7bf into master Apr 30, 2020
@willscott willscott deleted the feat/incomingevent branch April 30, 2020 02:06
@Stebalien Stebalien mentioned this pull request May 26, 2020
77 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants