mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
use read lock over write lock where possible (#363)
fix issue with sync starting twice
This commit is contained in:
parent
c325c99b87
commit
7573f6a556
2 changed files with 14 additions and 8 deletions
|
@ -259,7 +259,7 @@ impl NetAdapter for NetToChainAdapter {
|
||||||
);
|
);
|
||||||
|
|
||||||
if diff.into_num() > 0 {
|
if diff.into_num() > 0 {
|
||||||
let peers = self.connected_peers.write().unwrap();
|
let peers = self.connected_peers.read().unwrap();
|
||||||
if let Some(peer) = peers.get(&addr) {
|
if let Some(peer) = peers.get(&addr) {
|
||||||
let mut peer = peer.write().unwrap();
|
let mut peer = peer.write().unwrap();
|
||||||
peer.info.total_difficulty = diff;
|
peer.info.total_difficulty = diff;
|
||||||
|
@ -293,7 +293,7 @@ impl NetToChainAdapter {
|
||||||
.name("syncer".to_string())
|
.name("syncer".to_string())
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
let res = arc_sync.run();
|
let res = arc_sync.run();
|
||||||
if let Err(e) = arc_sync.run() {
|
if let Err(e) = res {
|
||||||
panic!("Error during sync, aborting: {:?}", e);
|
panic!("Error during sync, aborting: {:?}", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -278,6 +278,10 @@ impl Server {
|
||||||
let p = p.read().unwrap();
|
let p = p.read().unwrap();
|
||||||
p.info.total_difficulty.clone()
|
p.info.total_difficulty.clone()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO we should shuffle here
|
||||||
|
// we want a random peer with highest total_difficulty
|
||||||
|
|
||||||
let peer = peers.last().unwrap();
|
let peer = peers.last().unwrap();
|
||||||
Some(peer.clone())
|
Some(peer.clone())
|
||||||
}
|
}
|
||||||
|
@ -329,16 +333,16 @@ impl Server {
|
||||||
|
|
||||||
/// Number of peers we're currently connected to.
|
/// Number of peers we're currently connected to.
|
||||||
pub fn peer_count(&self) -> u32 {
|
pub fn peer_count(&self) -> u32 {
|
||||||
self.peers.read().unwrap().len() as u32
|
self.all_peers().len() as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stops the server. Disconnect from all peers at the same time.
|
/// Stops the server. Disconnect from all peers at the same time.
|
||||||
pub fn stop(self) {
|
pub fn stop(self) {
|
||||||
info!(LOGGER, "calling stop on server");
|
info!(LOGGER, "calling stop on server");
|
||||||
let peers = self.all_peers();
|
let peers = self.all_peers();
|
||||||
for p in peers {
|
for peer in peers {
|
||||||
let p = p.write().unwrap();
|
let peer = peer.read().unwrap();
|
||||||
p.stop();
|
peer.stop();
|
||||||
}
|
}
|
||||||
self.stop.into_inner().unwrap().send(()).unwrap();
|
self.stop.into_inner().unwrap().send(()).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -357,8 +361,10 @@ where
|
||||||
adapter.peer_connected(&peer.info);
|
adapter.peer_connected(&peer.info);
|
||||||
let addr = peer.info.addr.clone();
|
let addr = peer.info.addr.clone();
|
||||||
let apeer = Arc::new(RwLock::new(peer));
|
let apeer = Arc::new(RwLock::new(peer));
|
||||||
let mut peers = peers.write().unwrap();
|
{
|
||||||
peers.insert(addr, apeer.clone());
|
let mut peers = peers.write().unwrap();
|
||||||
|
peers.insert(addr, apeer.clone());
|
||||||
|
}
|
||||||
Ok((conn, apeer))
|
Ok((conn, apeer))
|
||||||
});
|
});
|
||||||
Box::new(peer_add)
|
Box::new(peer_add)
|
||||||
|
|
Loading…
Reference in a new issue