1- use crossbeam_channel:: { self as channel, after, select} ;
1+ use bitcoin:: BlockHash ;
2+ use crossbeam_channel:: { self as channel, after, select, Sender } ;
23use std:: thread;
34use std:: time:: { Duration , Instant } ;
45
@@ -9,6 +10,7 @@ use crate::errors::*;
910#[ derive( Clone ) ] // so multiple threads could wait on signals
1011pub struct Waiter {
1112 receiver : channel:: Receiver < i32 > ,
13+ zmq_receiver : channel:: Receiver < BlockHash > ,
1214}
1315
1416fn notify ( signals : & [ i32 ] ) -> channel:: Receiver < i32 > {
@@ -25,34 +27,54 @@ fn notify(signals: &[i32]) -> channel::Receiver<i32> {
2527}
2628
2729impl Waiter {
28- pub fn start ( ) -> Waiter {
29- Waiter {
30- receiver : notify ( & [
31- SIGINT , SIGTERM ,
32- SIGUSR1 , // allow external triggering (e.g. via bitcoind `blocknotify`)
33- ] ) ,
34- }
30+ pub fn start ( ) -> ( Sender < BlockHash > , Waiter ) {
31+ let ( block_hash_notify, block_hash_receive) = channel:: bounded ( 1 ) ;
32+
33+ (
34+ block_hash_notify,
35+ Waiter {
36+ receiver : notify ( & [
37+ SIGINT , SIGTERM ,
38+ SIGUSR1 , // allow external triggering (e.g. via bitcoind `blocknotify`)
39+ ] ) ,
40+ zmq_receiver : block_hash_receive,
41+ } ,
42+ )
3543 }
3644
37- pub fn wait ( & self , duration : Duration , accept_sigusr : bool ) -> Result < ( ) > {
45+ pub fn wait ( & self , duration : Duration , accept_block_notification : bool ) -> Result < ( ) > {
3846 let start = Instant :: now ( ) ;
3947 select ! {
4048 recv( self . receiver) -> msg => {
4149 match msg {
4250 Ok ( sig) if sig == SIGUSR1 => {
4351 trace!( "notified via SIGUSR1" ) ;
44- if accept_sigusr {
52+ if accept_block_notification {
4553 Ok ( ( ) )
4654 } else {
4755 let wait_more = duration. saturating_sub( start. elapsed( ) ) ;
48- self . wait( wait_more, accept_sigusr )
56+ self . wait( wait_more, accept_block_notification )
4957 }
5058 }
5159 Ok ( sig) => bail!( ErrorKind :: Interrupt ( sig) ) ,
5260 Err ( _) => bail!( "signal hook channel disconnected" ) ,
5361 }
5462 } ,
63+ recv( self . zmq_receiver) -> msg => {
64+ match msg {
65+ Ok ( _) => {
66+ if accept_block_notification {
67+ Ok ( ( ) )
68+ } else {
69+ let wait_more = duration. saturating_sub( start. elapsed( ) ) ;
70+ self . wait( wait_more, accept_block_notification)
71+ }
72+ }
73+ Err ( _) => bail!( "signal hook channel disconnected" ) ,
74+ }
75+ } ,
5576 recv( after( duration) ) -> _ => Ok ( ( ) ) ,
77+
5678 }
5779 }
5880}
0 commit comments