@@ -6,6 +6,7 @@ extern crate electrs;
6
6
7
7
use error_chain:: ChainedError ;
8
8
use std:: process;
9
+ use std:: sync:: mpsc:: channel;
9
10
use std:: sync:: { Arc , RwLock } ;
10
11
use std:: time:: Duration ;
11
12
@@ -45,8 +46,9 @@ fn run_server(config: Arc<Config>) -> Result<()> {
45
46
let metrics = Metrics :: new ( config. monitoring_addr ) ;
46
47
metrics. start ( ) ;
47
48
49
+ let ( block_hash_notify, block_hash_receive) = channel ( ) ;
48
50
if let Some ( zmq_addr) = config. zmq_addr . as_ref ( ) {
49
- zmq:: start ( & format ! ( "tcp://{zmq_addr}" ) , None ) ;
51
+ zmq:: start ( & format ! ( "tcp://{zmq_addr}" ) , Some ( block_hash_notify ) ) ;
50
52
}
51
53
52
54
let daemon = Arc :: new ( Daemon :: new (
@@ -123,14 +125,33 @@ fn run_server(config: Arc<Config>) -> Result<()> {
123
125
"count of iterations of electrs main loop each 5 seconds or after interrupts" ,
124
126
) ) ;
125
127
126
- loop {
128
+ ' outer : loop {
127
129
main_loop_count. inc ( ) ;
128
130
129
- if let Err ( err) = signal. wait ( Duration :: from_secs ( 5 ) , true ) {
130
- info ! ( "stopping server: {}" , err) ;
131
- rest_server. stop ( ) ;
132
- // the electrum server is stopped when dropped
133
- break ;
131
+ // In the next for loop:
132
+ // We are going to wait 5 secs (50*100ms) if nothings happens.
133
+ // We are stopping if we receive a TERM or INT signal.
134
+ // We are interrupting the wait if we receive a block hash notification (if zmq enabled) or receiving a USR1 signal.
135
+ for _ in 0 ..50 {
136
+ match signal. wait ( Duration :: from_millis ( 100 ) , true ) {
137
+ Ok ( is_sigusr) if is_sigusr => break ,
138
+ Ok ( _) => ( ) ,
139
+ Err ( err) => {
140
+ info ! ( "stopping server: {}" , err) ;
141
+ rest_server. stop ( ) ;
142
+ // the electrum server is stopped when dropped
143
+ break ' outer;
144
+ }
145
+ }
146
+
147
+ if let Ok ( block_hash) = block_hash_receive. try_recv ( ) {
148
+ debug ! ( "Main loop notified of a new block {block_hash}" ) ;
149
+ while let Ok ( block_hash) = block_hash_receive. try_recv ( ) {
150
+ // let's deplete the queue in the case there is a block burst
151
+ debug ! ( "Main loop notified of a new block {block_hash}" ) ;
152
+ }
153
+ break ;
154
+ }
134
155
}
135
156
136
157
// Index new blocks
0 commit comments