Reset inner futures_timer::Delay inside futures::future::Fuse

Hi

If there is no request arrived for last 3 seconds, I want idle_timeout branch to be triggered.
Hence whenever request branch is fired, I need reset() the delay.

let request = request_rx.fuse();
let response = response_rx.fuse();


let delay = futures_timer::Delay::new(Duration::from_secs(3)); // <-- timer created here
let idle_timeout = delay.fuse(); 


pin_mut!(request, response, idle_timeout);
loop {
    select! {
        _ = idle_timeout => {
            info!("No request in last 3 seconds");
        },

        _ = request.next() => {
           idle_timeout.get_mut().reset(Duration::from_secs(3));  // <-- reset timer whenever any request arrives
        },


        _ = response.next() => {
            // ...
        },

        complete => {
            return;
        }
    }
}

The problem is -- futures::future::Fuse<futures_timer::Delay> does not allow me to access the inner timer. The above code fails with following compliation.

    |
251 | ...                   idle_timeout.get_mut().reset(Duration::from_secs(3));
    |                                              ^^^^^ method not found in `&mut futures_util::future::fuse::Fuse<futures_timer::delay::Delay>`

How can I reset the timer in an Fuse?

futures-timer = 0.4.0
futures-preview = 0.3.0-alpha.19

thank you.

Can't you just make it mutable and replace the fused future with a new fused future?

1 Like

Thanks @alice

Now I create a new timer as below. Would it be better if I could reuse the same timer by reset it ?

let request = request_rx.fuse();
let response = response_rx.fuse();

let idle_duration = Duration::from_secs(3);
let idle_timeout = Delay::new(idle_duration).fuse(); 


pin_mut!(request, response, idle_timeout);
loop {
    select! {
        _ = idle_timeout => {
            info!("No request in last 3 seconds");
        },

        _ = request.next() => {
           idle_timeout.set(Delay::new(idle_duration).fuse());
        },


        _ = response.next() => {
            // ...
        },

        complete => {
            return;
        }
    }
}

Hey! I have the same question.. Flamegraphing my program show that 40% of the time is spent creating new threads for futures-timer::Delay... Do we have a way to reset the timer inside a Fuse?