feat: improved NOTICE messages for events and subscriptions

This commit is contained in:
Greg Heartsfield 2021-12-12 10:50:16 -06:00
parent 1589268eba
commit 531f6c4624
3 changed files with 29 additions and 12 deletions

View File

@ -1,7 +1,9 @@
//! Client connection state //! Client connection state
use crate::close::Close; use crate::close::Close;
use crate::error::Error;
use crate::error::Result; use crate::error::Result;
use crate::event::Event; use crate::event::Event;
use crate::subscription::Subscription; use crate::subscription::Subscription;
use log::*; use log::*;
use std::collections::HashMap; use std::collections::HashMap;
@ -33,7 +35,7 @@ impl ClientConn {
ClientConn { ClientConn {
client_id, client_id,
subscriptions: HashMap::new(), subscriptions: HashMap::new(),
max_subs: 128, max_subs: 32,
} }
} }
@ -65,7 +67,7 @@ impl ClientConn {
"ignoring sub request with excessive length: ({})", "ignoring sub request with excessive length: ({})",
sub_id_len sub_id_len
); );
return Ok(()); return Err(Error::SubIdMaxLengthError);
} }
// check if an existing subscription exists, and replace if so // check if an existing subscription exists, and replace if so
if self.subscriptions.contains_key(&k) { if self.subscriptions.contains_key(&k) {
@ -77,9 +79,7 @@ impl ClientConn {
// check if there is room for another subscription. // check if there is room for another subscription.
if self.subscriptions.len() >= self.max_subs { if self.subscriptions.len() >= self.max_subs {
// TODO: return error/notice for this return Err(Error::SubMaxExceededError);
info!("client has reached the maximum number of unique subscriptions");
return Ok(());
} }
// add subscription // add subscription
self.subscriptions.insert(k, s); self.subscriptions.insert(k, s);

View File

@ -21,6 +21,10 @@ pub enum Error {
CloseParseFailed, CloseParseFailed,
#[error("Event validation failed")] #[error("Event validation failed")]
EventInvalid, EventInvalid,
#[error("Subscription identifier max length exceeded")]
SubIdMaxLengthError,
#[error("Maximum concurrent subscription count reached")]
SubMaxExceededError,
// this should be used if the JSON is invalid // this should be used if the JSON is invalid
#[error("JSON parsing failed")] #[error("JSON parsing failed")]
JsonParseFailed(serde_json::Error), JsonParseFailed(serde_json::Error),

View File

@ -156,7 +156,10 @@ async fn nostr_server(
event_tx.send(e.clone()).await.ok(); event_tx.send(e.clone()).await.ok();
client_published_event_count += 1; client_published_event_count += 1;
}, },
Err(_) => {info!("client {} sent an invalid event", cid)} Err(_) => {
info!("client {} sent an invalid event", cid);
nostr_stream.send(NoticeRes("event was invalid".to_owned())).await.ok();
}
} }
}, },
Some(Ok(SubMsg(s))) => { Some(Ok(SubMsg(s))) => {
@ -166,12 +169,19 @@ async fn nostr_server(
// * making a channel to cancel to request later // * making a channel to cancel to request later
// * sending a request for a SQL query // * sending a request for a SQL query
let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>(); let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>();
match conn.subscribe(s.clone()) {
Ok(()) => {
running_queries.insert(s.id.to_owned(), abandon_query_tx); running_queries.insert(s.id.to_owned(), abandon_query_tx);
// register this connection
conn.subscribe(s.clone()).ok();
// start a database query // start a database query
db::db_query(s, query_tx.clone(), abandon_query_rx).await; db::db_query(s, query_tx.clone(), abandon_query_rx).await;
}, },
Err(e) => {
info!("Subscription error: {}", e);
nostr_stream.send(NoticeRes(format!("{}",e))).await.ok();
}
}
},
Some(Ok(CloseMsg(cc))) => { Some(Ok(CloseMsg(cc))) => {
// closing a request simply removes the subscription. // closing a request simply removes the subscription.
let parsed : Result<Close> = Result::<Close>::from(cc); let parsed : Result<Close> = Result::<Close>::from(cc);
@ -187,7 +197,10 @@ async fn nostr_server(
// the subscription // the subscription
conn.unsubscribe(c); conn.unsubscribe(c);
}, },
Err(_) => {info!("invalid command ignored");} Err(_) => {
info!("invalid command ignored");
}
} }
}, },
None => { None => {