Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High Client Stale Connection Performance Issues #96

Open
CJBuchel opened this issue Sep 7, 2023 · 0 comments · May be fixed by #159
Open

High Client Stale Connection Performance Issues #96

CJBuchel opened this issue Sep 7, 2023 · 0 comments · May be fixed by #159
Assignees
Labels
Server Host/Server side wontfix This will not be worked on

Comments

@CJBuchel
Copy link
Owner

CJBuchel commented Sep 7, 2023

The rust server checks all clients currently connected to determine if there are any stale connections or not.
May cause performance drops, but it's unlikely. Putting it here for future me.

The specific code I reference is

// Client timestamp update fairing
pub struct ClientTimestampUpdate;
#[rocket::async_trait]
impl Fairing for ClientTimestampUpdate {
  fn info(&self) -> Info {
    Info {
      name: "Update client timestamp",
      kind: Kind::Request
    }
  }

  async fn on_request(&self, request: &mut Request<'_>, _: &mut Data<'_>) {
    let path_segments: Vec<String> = request.uri().path().split('/').map(|s| s.to_string()).collect();
    // Assuming that UUID is always the last segment
    if let Some(uuid) = path_segments.last() {
      if Uuid::parse_str(uuid).is_ok() { // check if it is a valid uuid
        if let Some(state) = request.rocket().state::<TmsClients>() {
          with_clients_write(&state, |client_map| {
            if let Some(client) = client_map.get_mut(uuid) {
              client.last_timestamp = std::time::SystemTime::now();
            }
    
            // Check all clients for stale connections
            let current_time = std::time::SystemTime::now();
            let mut stale_clients: Vec<String> = Vec::new();
            for (id, client) in client_map.iter() {
              if let Ok(duration) = current_time.duration_since(client.last_timestamp) {
                if duration.as_secs() > (5 * 60) { // 5 mins
                  stale_clients.push(id.to_string());
                }
              }
            }
    
            // remove oldest stale clients if there are more than 100
            let num_allowed_stale_clients = 100;
            if stale_clients.len() > num_allowed_stale_clients {
              stale_clients.sort_by(|a, b| {
                client_map[a].last_timestamp.cmp(&client_map[b].last_timestamp)
              });
    
              for i in 0..(stale_clients.len() - num_allowed_stale_clients) {
                let id = stale_clients[i].clone();
                client_map.remove(&id);
              }
            }
          }).unwrap();
  
        }
      }
    }
  }
}
@CJBuchel CJBuchel added wontfix This will not be worked on Server Host/Server side labels Sep 7, 2023
@CJBuchel CJBuchel self-assigned this Sep 7, 2023
@CJBuchel CJBuchel changed the title High client Stale Connection Performance issues High Client Stale Connection Performance Issues Sep 7, 2023
@CJBuchel CJBuchel linked a pull request Jul 19, 2024 that will close this issue
@CJBuchel CJBuchel linked a pull request Jul 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Server Host/Server side wontfix This will not be worked on
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

1 participant