pub fn initialize(duration: Duration) -> Self {
        let tracker = Arc::new(Mutex::new(ConnTracker::new()));

        let tracker_2 = Arc::clone(&tracker);
        let task = tokio::spawn(async move {
            loop {
                // Get the time since the last connection joined/left
                let (base_time, cnt) = tracker_2.lock().await.time_and_cnt();

                // If we have no connections left, we want to wait
                // until the remaining period has passed and then
                // verify that we still have no connections
                if cnt == 0 {
                    // Get the time we should wait based on when the last connection
                    // was dropped; this closes the gap in the case where we start
                    // sometime later than exactly duration since the last check
                    let next_time = base_time + duration;
                    let wait_duration = next_time
                        .checked_duration_since(Instant::now())
                        .unwrap_or_default()
                        + Duration::from_millis(1);

                    // Wait until we've reached our desired duration since the
                    // last connection was dropped
                    time::sleep(wait_duration).await;

                    // If we do have a connection at this point, don't exit
                    if !tracker_2.lock().await.has_reached_timeout(duration) {
                        continue;
                    }

                    // Otherwise, we now should exit, which we do by reporting
                    debug!(
                        "Shutdown time of {}s has been reached!",
                        duration.as_secs_f32()
                    );
                    break;
                }

                // Otherwise, we just wait the full duration as worst case
                // we'll have waited just about the time desired if right
                // after waiting starts the last connection is closed
                time::sleep(duration).await;
            }
        });

        Self { task, tracker }
    }
/// Spawns a new task that continues to monitor the time since a
/// connection on the server existed, reporting a shutdown to all listeners
/// once the timeout is exceeded
Spawns a new task that continues to monitor the time since a
connection on the server existed, reporting a shutdown to all listeners
once the timeout is exceeded
Spawns a new task that continues to monitor the time since a connection on the server existed reporting a shutdown to all listeners once the timeout is exceeded
pub fn initialize(duration: Duration) -> Self {
        let tracker = Arc::new(Mutex::new(ConnTracker::new()));
        let tracker_2 = Arc::clone(&tracker);
        let task = tokio::spawn(async move {
            loop {
                // Get the time since the last connection joined/left
                let (base_time, cnt) = tracker_2.lock().await.time_and_cnt();
                // If we have no connections left, we want to wait
                // until the remaining period has passed and then
                // verify that we still have no connections
                if cnt == 0 {
                    // Get the time we should wait based on when the last connection
                    // was dropped; this closes the gap in the case where we start
                    // sometime later than exactly duration since the last check
                    let next_time = base_time + duration;
                    let wait_duration = next_time
                        .checked_duration_since(Instant::now())
                        .unwrap_or_default()
                        + Duration::from_millis(1);
                    // Wait until we've reached our desired duration since the
                    // last connection was dropped
                    time::sleep(wait_duration).await;
                    // If we do have a connection at this point, don't exit
                    if !tracker_2.lock().await.has_reached_timeout(duration) {
                        continue;
                    }
                    // Otherwise, we now should exit, which we do by reporting
                    debug!(
                        "Shutdown time of {}s has been reached!",
                        duration.as_secs_f32()
                    );
                    break;
                }
                // Otherwise, we just wait the full duration as worst case
                // we'll have waited just about the time desired if right
                // after waiting starts the last connection is closed
                time::sleep(duration).await;
            }
        });
        Self { task, tracker }
    }
pub fn initialize ( duration : Duration ) -> Self { let tracker = Arc :: new ( Mutex :: new ( ConnTracker :: new ( ) ) ) ; let tracker_2 = Arc :: clone ( & tracker ) ; let task = tokio :: spawn ( async move { loop { // Get the time since the last connection joined/left let ( base_time , cnt ) = tracker_2 . lock ( ) . await . time_and_cnt ( ) ; // If we have no connections left, we want to wait // until the remaining period has passed and then // verify that we still have no connections if cnt == 0 { // Get the time we should wait based on when the last connection // was dropped; this closes the gap in the case where we start // sometime later than exactly duration since the last check let next_time = base_time + duration ; let wait_duration = next_time . checked_duration_since ( Instant :: now ( ) ) . unwrap_or_default ( ) + Duration :: from_millis ( 1 ) ; // Wait until we've reached our desired duration since the // last connection was dropped time :: sleep ( wait_duration ) . await ; // If we do have a connection at this point, don't exit if ! tracker_2 . lock ( ) . await . has_reached_timeout ( duration ) { continue ; } // Otherwise, we now should exit, which we do by reporting debug ! ( " " duration as_secs_f32 ( ) ) ; break ; } // Otherwise, we just wait the full duration as worst case // we'll have waited just about the time desired if right // after waiting starts the last connection is closed time :: sleep ( duration ) . await ; } } ) ; Self { task , tracker } }
Spawns a new task that continues to monitor the time since a
connection on the server existed, reporting a shutdown to all listeners
once the timeout is exceeded
Spawns a new task that continues to monitor the time since a connection on the server existed reporting a shutdown to all listeners once the timeout is exceeded
duration
Duration