pub async fn record_sliding_window(
        &mut self,
        resource: &str,
        subject: &str,
        size: Duration,
    ) -> Result<u64, Box<dyn Error>> {
        let now = SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap();
        let size_secs = size.as_secs();

        let current_window = (now.as_secs() / size_secs) * size_secs;
        let previous_window = (now.as_secs() / size_secs) * size_secs - size_secs;
        let current_key = format!("{}:{}:{}:{}", KEY_PREFIX, resource, subject, current_window);
        let previous_key = format!(
            "{}:{}:{}:{}",
            KEY_PREFIX, resource, subject, previous_window
        );

        let (previous_count, current_count): (Option<u64>, Option<u64>) = redis::pipe()
            .atomic()
            .get(&previous_key)
            .incr(&current_key, 1)
            .expire(&current_key, (size_secs * 2) as usize)
            .ignore()
            .query_async(&mut self.conn)
            .await?;
        Ok(Self::sliding_window_count(
            previous_count,
            current_count,
            now,
            size,
        ))
    }
/// Records an access to `resource` by `subject` with sliding window algorithm.
/// Size of time window equals the `size` in seconds.
Records an access to `resource` by `subject` with sliding window algorithm.
Size of time window equals the `size` in seconds.
Records an access to ` resource ` by ` subject ` with sliding window algorithm . Size of time window equals the ` size ` in seconds .
pub async fn record_sliding_window(
        &mut self,
        resource: &str,
        subject: &str,
        size: Duration,
    ) -> Result<u64, Box<dyn Error>> {
        let now = SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap();
        let size_secs = size.as_secs();
        let current_window = (now.as_secs() / size_secs) * size_secs;
        let previous_window = (now.as_secs() / size_secs) * size_secs - size_secs;
        let current_key = format!("{}:{}:{}:{}", KEY_PREFIX, resource, subject, current_window);
        let previous_key = format!(
            "{}:{}:{}:{}",
            KEY_PREFIX, resource, subject, previous_window
        );
        let (previous_count, current_count): (Option<u64>, Option<u64>) = redis::pipe()
            .atomic()
            .get(&previous_key)
            .incr(&current_key, 1)
            .expire(&current_key, (size_secs * 2) as usize)
            .ignore()
            .query_async(&mut self.conn)
            .await?;
        Ok(Self::sliding_window_count(
            previous_count,
            current_count,
            now,
            size,
        ))
    }
pub async fn record_sliding_window ( & mut self , resource : & str , subject : & str , size : Duration , ) -> Result < u64 , Box < dyn Error > > { let now = SystemTime :: now ( ) . duration_since ( time :: UNIX_EPOCH ) . unwrap ( ) ; let size_secs = size . as_secs ( ) ; let current_window = ( now . as_secs ( ) / size_secs ) * size_secs ; let previous_window = ( now . as_secs ( ) / size_secs ) * size_secs - size_secs ; let current_key = format ! ( " " KEY_PREFIX resource subject current_window ) ; let previous_key = format ! ( " " KEY_PREFIX resource subject previous_window ) ; let ( previous_count , current_count ) : ( Option < u64 > , Option < u64 > ) = redis :: pipe ( ) . atomic ( ) . get ( & previous_key ) . incr ( & current_key , 1 ) . expire ( & current_key , ( size_secs * 2 ) as usize ) . ignore ( ) . query_async ( & mut self . conn ) . await ? ; Ok ( Self :: sliding_window_count ( previous_count , current_count , now , size , ) ) }
Records an access to `resource` by `subject` with sliding window algorithm.
Records an access to ` resource ` by ` subject ` with sliding window algorithm .
&mut self
resource
&str
subject
&str
size
Duration
&mut self
resource
&str
subject
&str
size
Duration