hermes icon indicating copy to clipboard operation
hermes copied to clipboard

Hermes needs to detect websocket pull message timeout

Open lyqingye opened this issue 2 years ago • 0 comments

Summary of Bug

Hermes needs to detect websocket pull message timeout In some scenarios, hermes cannot receive the data pushed by the server, and there are no errors related to websocket

Solution

detect timeout in tokio_select

    fn run_loop(&mut self) -> Next {
        // Take ownership of the subscriptions
        let subscriptions =
            core::mem::replace(&mut self.subscriptions, Box::new(futures::stream::empty()));

        // Convert the stream of RPC events into a stream of event batches.
        let batches = stream_batches(subscriptions, self.chain_id.clone());

        // Needed to be able to poll the stream
        pin_mut!(batches);

        // Work around double borrow
        let rt = self.rt.clone();
       
        loop {
            if let Ok(cmd) = self.rx_cmd.try_recv() {
                match cmd {
                    MonitorCmd::Shutdown => return Next::Abort,
                    MonitorCmd::Subscribe(tx) => {
                        if let Err(e) = tx.send(self.event_bus.subscribe()) {
                            error!("failed to send back subscription: {e}");
                        }
                    }
                }
            }

            let result = rt.block_on(async {
                tokio::select! {
                    Some(batch) = batches.next() => batch,
                    Some(e) = self.rx_err.recv() => Err(Error::web_socket_driver(e)),

                    // Select timout 
                    _ = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => Err(Error::channel_recv_time_out())
                }
            });

Version

all version

Steps to Reproduce

None

Acceptance Criteria


For Admin Use

  • [ ] Not duplicate issue
  • [ ] Appropriate labels applied
  • [ ] Appropriate milestone (priority) applied
  • [ ] Appropriate contributors tagged
  • [ ] Contributor assigned/self-assigned

lyqingye avatar Feb 23 '23 03:02 lyqingye