Skip to content

Commit f37bcd5

Browse files
committed
New multithreaded model
This new model is based on the idea of having only one thread monitor the socket and then distribute the responses to every other thread that's waiting. Whenever a request is made, a new mpsc channel is created and added to an internal map. Then, if there are not other threads waiting for pending responses, that thread will become the "reader thread" and monitor the socket until it receives the response it was waiting for. In the meantime, if it sees responses coming on the socket with different ids, it will lookup the correct Sender from the internal map and send that message to the thread that created the request in the first place. The other threads will be waiting trying to read from their Receiver, where they will either get the message in response to their request, or a signal to wake up and become the new "reader thread".
1 parent 7b4e99e commit f37bcd5

File tree

5 files changed

+390
-158
lines changed

5 files changed

+390
-158
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ path = "src/lib.rs"
2121

2222
[dependencies]
2323
log = "^0.4"
24+
env_logger = "0.7"
2425
bitcoin = { version = "0.23", features = ["use-serde"] }
2526
serde = { version = "^1.0", features = ["derive"] }
2627
serde_json = { version = "^1.0" }

examples/plaintext.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
extern crate electrum_client;
2+
extern crate env_logger;
3+
4+
use std::sync::Arc;
5+
use std::thread;
26

37
use electrum_client::Client;
48

59
fn main() {
6-
let mut client = Client::new("kirsche.emzy.de:50001").unwrap();
7-
let res = client.server_features();
8-
println!("{:#?}", res);
10+
env_logger::init();
11+
12+
let client = Arc::new(Client::new("electrum.blockstream.info:50001").unwrap());
13+
14+
let mut handles = Vec::new();
15+
16+
/*let _client = Arc::clone(&client);
17+
let handle = thread::spawn(move || {
18+
_client.reader_thread().unwrap();
19+
println!("reader thread exited");
20+
});
21+
22+
handles.push(handle);*/
23+
24+
thread::sleep(std::time::Duration::from_secs(1));
25+
26+
for _ in 0..4 {
27+
let client = Arc::clone(&client);
28+
let handle = thread::spawn(move || {
29+
let res = client.batch_estimate_fee(vec![1, 3, 6, 12]);
30+
println!("{:?}", res);
31+
});
32+
33+
handles.push(handle);
34+
}
35+
36+
for h in handles {
37+
h.join().unwrap();
38+
}
939
}

0 commit comments

Comments
 (0)