Replace `Mutex` with usage of `&mut self` (#2742)

Using `&mut self` as the receiver in the method signatures allows Rust
to infer that the type is properly `Sync`, and therefore `Send`. This
allows removing the `Mutex` work-around.
This commit is contained in:
Janito Vaqueiro Ferreira Filho 2021-09-08 09:50:46 -03:00 committed by GitHub
parent 1c4ac18df2
commit be59dd2b93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions

View File

@ -5,7 +5,7 @@
use std::time::Duration;
use futures::{stream::FuturesUnordered, StreamExt};
use tokio::{sync::Mutex, task::JoinHandle, time::sleep};
use tokio::{task::JoinHandle, time::sleep};
use tower::{timeout::Timeout, BoxError, Service, ServiceExt};
use zebra_network::{Request, Response};
@ -32,7 +32,7 @@ const PEER_RESPONSE_TIMEOUT: Duration = Duration::from_secs(6);
/// The mempool transaction crawler.
pub struct Crawler<S> {
peer_set: Mutex<Timeout<S>>,
peer_set: Timeout<S>,
status: SyncStatus,
}
@ -44,7 +44,7 @@ where
/// Spawn an asynchronous task to run the mempool crawler.
pub fn spawn(peer_set: S, status: SyncStatus) -> JoinHandle<Result<(), BoxError>> {
let crawler = Crawler {
peer_set: Mutex::new(Timeout::new(peer_set, PEER_RESPONSE_TIMEOUT)),
peer_set: Timeout::new(peer_set, PEER_RESPONSE_TIMEOUT),
status,
};
@ -67,8 +67,8 @@ where
/// Crawl peers for transactions.
///
/// Concurrently request [`FANOUT`] peers for transactions to include in the mempool.
async fn crawl_transactions(&self) -> Result<(), BoxError> {
let peer_set = self.peer_set.lock().await.clone();
async fn crawl_transactions(&mut self) -> Result<(), BoxError> {
let peer_set = self.peer_set.clone();
trace!("Crawling for mempool transactions");
@ -95,7 +95,7 @@ where
}
/// Handle a peer's response to the crawler's request for transactions.
async fn handle_response(&self, response: Response) {
async fn handle_response(&mut self, response: Response) {
let transaction_ids = match response {
Response::TransactionIds(ids) => ids,
_ => unreachable!("Peer set did not respond with transaction IDs to mempool crawler"),