Throttle the server a bit

This commit is contained in:
Gender Shrapnel 2020-06-01 19:30:56 +02:00
parent 3ee735b7f2
commit b0bb187f75

View File

@ -1,11 +1,14 @@
use std::{sync::Arc, time::Duration};
use std::sync::Arc;
use futures::{SinkExt, StreamExt}; use futures::{SinkExt, StreamExt};
use serde_json::json; use serde_json::json;
use tera::{Context, Tera}; use tera::{Context, Tera};
use tokio::sync::watch; use tokio::{sync::watch, time::throttle};
use warp::{Filter, reject::Reject, Rejection, ws::{Message, WebSocket}}; use warp::{
reject::Reject,
ws::{Message, WebSocket},
Filter, Rejection,
};
use crate::state::State; use crate::state::State;
@ -54,23 +57,23 @@ struct WithTemplate {
} }
async fn watch_state(ws: WebSocket, mut state_channel: watch::Receiver<State>) { async fn watch_state(ws: WebSocket, mut state_channel: watch::Receiver<State>) {
let (mut tx, mut rx) = ws.split(); let (mut tx, rx) = ws.split();
let mut rx = throttle(Duration::from_millis(1000 / 30), rx);
while let Some(_) = rx.next().await { while let Some(_) = rx.next().await {
let state = state_channel.recv().await; let state = state_channel.recv().await;
let result = match state { let result = match state {
Some(state) => tx.send(Message::text(json!(state).to_string())).await, Some(state) => tx.send(Message::text(json!(state).to_string())).await,
None => break None => break,
}; };
if let Err(_) = result { if let Err(_) = result {
break; break;
} }
}; }
} }
#[derive(Debug)] #[derive(Debug)]
struct SimulationUnavailable; struct SimulationUnavailable;
@ -79,7 +82,7 @@ impl Reject for SimulationUnavailable {}
async fn get_state(mut channel: watch::Receiver<State>) -> Result<State, Rejection> { async fn get_state(mut channel: watch::Receiver<State>) -> Result<State, Rejection> {
match channel.recv().await { match channel.recv().await {
Some(state) => Ok(state), Some(state) => Ok(state),
None => Err(warp::reject::custom(SimulationUnavailable)) None => Err(warp::reject::custom(SimulationUnavailable)),
} }
} }