Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Networking refactoring #1172

Merged
merged 4 commits into from Jun 2, 2016
Merged

Networking refactoring #1172

merged 4 commits into from Jun 2, 2016

Conversation

arkpar
Copy link
Collaborator

@arkpar arkpar commented May 30, 2016

Refactoring required for windows compatibility. Windows async IO does not support changing the registration token ID for a socket, and that's what we were doing when promoting a Handshake object to a Session, This refactors the Sessions to manage the handshake and use the same registration ID.

@arkpar arkpar added the A0-pleasereview 🤓 Pull request needs code review. label May 30, 2016
/// Get remote peer address
pub fn remote_addr(&self) -> io::Result<SocketAddr> {
self.socket.peer_addr()
}

/// Get remote peer address string
pub fn remote_addr_str(&self) -> String {
self.socket.peer_addr().map(|a| a.to_string()).unwrap_or_else(|_| "Unkwnown".to_owned())
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo.

@arkpar arkpar added A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. and removed A0-pleasereview 🤓 Pull request needs code review. labels May 30, 2016
}
}
}
self.num_sessions.fetch_add(1, AtomicOrdering::Relaxed);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't it be SeqCst (or at least something more then Relaxed)?
Isn't using Relaxed here a potential overflow? (when fetch_sub is called)

(sorry if it's a lame question :))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Decrement is always atomic, Relaxed is fine here because we don't use the returned value and other threads that read the value do not require any synchronization on it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the Relaxed ordering would lead to potentially incorrect values being returned from session_count. This doesn't pose a problem on the x86 or x86_64 memory model, so any bugs introduced from Relaxed atomics could be hidden from most of us.

The performance gains from Relaxed vs SeqCst are not that significant, so why not trend towards the safe side?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is no synchronization on the session count, so there is no potential for races. It is perfectly fine if other threads see an updated value with a delay.

@arkpar arkpar added A0-pleasereview 🤓 Pull request needs code review. and removed A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. labels May 31, 2016
@@ -477,19 +474,19 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
}

fn have_session(&self, id: &NodeId) -> bool {
self.sessions.read().unwrap().iter().any(|e| e.lock().unwrap().info.id.eq(&id))
self.sessions.read().unwrap().iter().any(|e| e.lock().unwrap().info.id.eq(&Some(id.clone())))
Copy link
Contributor

Choose a reason for hiding this comment

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

eq vs ==?

@@ -205,7 +205,7 @@ impl Connection {
/// Update connection registration. Should be called at the end of the IO handler.
pub fn update_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
trace!(target: "network", "connection reregister; token={:?}", reg);
event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() /* | PollOpt::oneshot() */ ).or_else(|e| { // TODO: oneshot is broken on windows
Copy link
Contributor

Choose a reason for hiding this comment

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

what ramifications are there for other platforms with this removal?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No major issues, except some IO events are now generated earlier than required, and automatically. "oneshot" mode requires updating registration after each event handler and gives more precise control so that events don't queue up, but in our case it does not matter much since the handler is not a bottleneck.

@gavofyork
Copy link
Contributor

would be lovely to see some intergration tests for this :-)

@arkpar
Copy link
Collaborator Author

arkpar commented May 31, 2016

There are integration tests in util/src/network/test.rs already. They helped a lot

@@ -790,12 +722,13 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
if s.is_ready() {
for (p, _) in self.handlers.read().unwrap().iter() {
if s.have_capability(p) {
self.num_sessions.fetch_sub(1, AtomicOrdering::SeqCst);
to_disconnect.push(p);
Copy link
Contributor

Choose a reason for hiding this comment

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

trace/debug to help with the reason of disconnect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The reason is logged before calling kill_connection

@NikVolf NikVolf added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jun 1, 2016
@arkpar arkpar added A0-pleasereview 🤓 Pull request needs code review. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Jun 1, 2016
@NikVolf NikVolf added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jun 1, 2016
@gavofyork gavofyork merged commit 8596a34 into master Jun 2, 2016
@gavofyork gavofyork deleted the net branch June 2, 2016 09:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants