mirror of
https://github.com/mikedilger/chorus.git
synced 2026-06-02 07:02:09 +00:00
Timeout idle connections with no subscriptions after 5 seconds
This commit is contained in:
parent
5311314fce
commit
7f978618e4
17
src/main.rs
17
src/main.rs
@ -36,6 +36,7 @@ use std::time::Duration;
|
||||
use textnonce::TextNonce;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
use tokio::time::Instant;
|
||||
use tungstenite::protocol::WebSocketConfig;
|
||||
use tungstenite::Message;
|
||||
|
||||
@ -340,9 +341,25 @@ impl WebSocketService {
|
||||
let reply = NostrReply::Auth(self.challenge.clone());
|
||||
self.websocket.send(Message::text(reply.as_json())).await?;
|
||||
|
||||
let mut last_message_at = Instant::now();
|
||||
|
||||
loop {
|
||||
let interval = tokio::time::interval(Duration::from_secs(5));
|
||||
tokio::pin!(interval);
|
||||
|
||||
tokio::select! {
|
||||
instant = interval.tick() => {
|
||||
// Drop them if they have no subscriptions
|
||||
if self.subscriptions.is_empty() {
|
||||
// And they are idle for 5 seconds with no subscriptions
|
||||
if last_message_at + Duration::from_secs(5) < instant {
|
||||
self.websocket.send(Message::Close(None)).await?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
message_option = self.websocket.next() => {
|
||||
last_message_at = Instant::now();
|
||||
match message_option {
|
||||
Some(message) => {
|
||||
let message = message?;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user