Skip to content

Commit cdfc264

Browse files
author
Ludo Galabru
committed
fix: serialize handlers in one thread
1 parent 3443915 commit cdfc264

File tree

3 files changed

+27
-45
lines changed

3 files changed

+27
-45
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ordhook-core/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ redis = "0.21.5"
1212
serde-redis = "0.12.0"
1313
hex = "0.4.3"
1414
rand = "0.8.5"
15-
chainhook-sdk = { version = "=0.8.3", default-features = false, features = ["zeromq"] }
16-
# chainhook-sdk = { version = "=0.8.3", path = "../../../chainhook/components/chainhook-sdk", default-features = false, features = ["zeromq"] }
15+
chainhook-sdk = { version = "=0.8.4", default-features = false, features = ["zeromq"] }
16+
# chainhook-sdk = { version = "=0.8.4", path = "../../../chainhook/components/chainhook-sdk", default-features = false, features = ["zeromq"] }
1717
hiro-system-kit = "0.1.0"
1818
reqwest = { version = "0.11", features = ["stream", "json"] }
1919
tokio = { version = "=1.24", features = ["full"] }

components/ordhook-core/src/service/mod.rs

+23-41
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use chainhook_sdk::observer::{
3333
};
3434
use chainhook_sdk::types::{BitcoinBlockData, BlockIdentifier};
3535
use chainhook_sdk::utils::Context;
36+
use crossbeam_channel::select;
3637
use crossbeam_channel::unbounded;
3738
use dashmap::DashMap;
3839
use fxhash::FxHasher;
@@ -132,9 +133,9 @@ impl Service {
132133

133134
// Sidecar channels setup
134135
let (observer_command_tx, observer_command_rx) = channel();
135-
let (block_mutator_in_tx, block_mutator_in_rx) = channel();
136-
let (block_mutator_out_tx, block_mutator_out_rx) = channel();
137-
let (chain_event_notifier_tx, chain_event_notifier_rx) = channel();
136+
let (block_mutator_in_tx, block_mutator_in_rx) = crossbeam_channel::unbounded();
137+
let (block_mutator_out_tx, block_mutator_out_rx) = crossbeam_channel::unbounded();
138+
let (chain_event_notifier_tx, chain_event_notifier_rx) = crossbeam_channel::unbounded();
138139
let observer_sidecar = ObserverSidecar {
139140
bitcoin_blocks_mutator: Some((block_mutator_in_tx, block_mutator_out_rx)),
140141
bitcoin_chain_event_notifier: Some(chain_event_notifier_tx),
@@ -198,46 +199,27 @@ impl Service {
198199
let config = self.config.clone();
199200
let cache_l2 = traversals_cache.clone();
200201

201-
let _ = hiro_system_kit::thread_named("Sidecar block mutator").spawn(move || loop {
202-
let (mut blocks_to_mutate, blocks_ids_to_rollback) = match block_mutator_in_rx.recv() {
203-
Ok(block) => block,
204-
Err(e) => {
205-
error!(
206-
ctx.expect_logger(),
207-
"Error: broken channel {}",
208-
e.to_string()
209-
);
210-
break;
211-
}
212-
};
213-
chainhook_sidecar_mutate_blocks(
214-
&mut blocks_to_mutate,
215-
&blocks_ids_to_rollback,
216-
&cache_l2,
217-
&config,
218-
&ctx,
219-
);
220-
let _ = block_mutator_out_tx.send(blocks_to_mutate);
221-
});
222-
223-
let ctx = self.ctx.clone();
224-
let config = self.config.clone();
225-
let _ =
226-
hiro_system_kit::thread_named("Chain event notification handler").spawn(move || loop {
227-
let command = match chain_event_notifier_rx.recv() {
228-
Ok(cmd) => cmd,
229-
Err(e) => {
230-
error!(
231-
ctx.expect_logger(),
232-
"Error: broken channel {}",
233-
e.to_string()
202+
let _ = hiro_system_kit::thread_named("Observer Sidecar Runloop").spawn(move || loop {
203+
select! {
204+
recv(block_mutator_in_rx) -> msg => {
205+
if let Ok((mut blocks_to_mutate, blocks_ids_to_rollback)) = msg {
206+
chainhook_sidecar_mutate_blocks(
207+
&mut blocks_to_mutate,
208+
&blocks_ids_to_rollback,
209+
&cache_l2,
210+
&config,
211+
&ctx,
234212
);
235-
break;
213+
let _ = block_mutator_out_tx.send(blocks_to_mutate);
236214
}
237-
};
238-
239-
chainhook_sidecar_mutate_ordhook_db(command, &config, &ctx)
240-
});
215+
}
216+
recv(chain_event_notifier_rx) -> msg => {
217+
if let Ok(command) = msg {
218+
chainhook_sidecar_mutate_ordhook_db(command, &config, &ctx)
219+
}
220+
}
221+
}
222+
});
241223

242224
loop {
243225
let event = match observer_event_rx.recv() {

0 commit comments

Comments
 (0)