-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Review the Mutex usage on PeerManager's peers #533
Comments
i also wonder why we have separate dead peer check on peer manager & router. mycelium/mycelium/src/peer_manager.rs Line 404 in 74719ba
mycelium/mycelium/src/router.rs Line 282 in 74719ba
|
The reason for a mutex is basically that this field is accessed only very infrequently outside of the peer managers peer check loop, and for simplicity. Even for the public nodes right now, this is not an issue. I also don't think changing to an RWLock would solve a performance issue here, if there ever is one, compared to a mutex. The main issue would be contention, and RWLocks also behave poorly under contention. On another note, we'll increase the delay for scanning for dead peers in the peer manager in the future.
This is a historic thing, however the router uses a different condition to determine if a peer is dead or not. In the router, we check the time since we last received an IHU, whereas the peer_manager is concerned about the liveness of the actual connection. Therefore, to maintain some separation of concern, they both have a different task. In theory, the router could always know about dead peers, so there could be a channel for the router to notify the peer_manager, though for now this is sufficient. In that scenario, the peer_manager would also need to add some timeouts before reconnecting, as we don't want a hot reconnect loop to a potential malfunctioning peer |
We currently use
Mutex
to protect PeerManager'speers
HashMap
.mycelium/mycelium/src/peer_manager.rs
Line 159 in 74719ba
There are two places that i think could be made read-only:
fn peers
, i'm pretty sure that we can make it read-onlymycelium/mycelium/src/peer_manager.rs
Lines 319 to 339 in 74719ba
peer_check_interval
It holds the
lock
while iterating the wholeHashMap
, twice, which might be quite long for public nodesmycelium/mycelium/src/peer_manager.rs
Lines 404 to 422 in 74719ba
With the current code, we can't simply replace the
lock
withread
, we have to restructure it.for example this line
could be refactored to
Similar with the second
lock
/ iterationI wondered the similar thing with the
router
, but looks like it is not significantmycelium/mycelium/src/peer_manager.rs
Line 975 in 74719ba
mycelium/mycelium/src/peer_manager.rs
Line 1125 in 74719ba
The text was updated successfully, but these errors were encountered: