Improve negentropy error handling by using NEG-ERR when we can

This commit is contained in:
Mike Dilger 2025-01-21 09:41:35 +13:00
parent 4f8fcb7c65
commit e9fd738213
No known key found for this signature in database
GPG Key ID: 47581A78D4329BA4
2 changed files with 21 additions and 6 deletions

View File

@ -227,6 +227,7 @@ async fn websocket_thread(peer: HashedPeer, websocket: HyperWebsocket, origin: S
user: None,
error_punishment: 0.0,
replied: false,
negentropy_sub: None,
};
// Increment connection count
@ -362,6 +363,7 @@ struct WebSocketService {
pub user: Option<Pubkey>,
pub error_punishment: f32,
pub replied: bool,
pub negentropy_sub: Option<String>,
}
impl WebSocketService {
@ -544,8 +546,13 @@ impl WebSocketService {
}
}
if !self.replied {
let reply = NostrReply::Notice(format!("error: {}", e.inner));
self.send(Message::text(reply.as_json())).await?;
if let Some(subid) = &self.negentropy_sub {
let reply = NostrReply::NegErr(subid, format!("error: {e}"));
self.send(Message::text(reply.as_json())).await?;
} else {
let reply = NostrReply::Notice(format!("error: {}", e.inner));
self.send(Message::text(reply.as_json())).await?;
}
}
if self.error_punishment >= 1.0 {
let reply = NostrReply::Notice("Closing due to error(s)".into());

View File

@ -468,8 +468,11 @@ impl WebSocketService {
subid
};
if ! GLOBALS.config.read().enable_negentropy {
let reply = NostrReply::NegErr(&subid, "blocked: Negentropy sync is disabled".to_owned());
self.negentropy_sub = Some(subid.clone());
if !GLOBALS.config.read().enable_negentropy {
let reply =
NostrReply::NegErr(&subid, "blocked: Negentropy sync is disabled".to_owned());
self.send(Message::text(reply.as_json())).await?;
return Ok(());
}
@ -600,8 +603,11 @@ impl WebSocketService {
subid
};
if ! GLOBALS.config.read().enable_negentropy {
let reply = NostrReply::NegErr(&subid, "blocked: Negentropy sync is disabled".to_owned());
self.negentropy_sub = Some(subid.clone());
if !GLOBALS.config.read().enable_negentropy {
let reply =
NostrReply::NegErr(&subid, "blocked: Negentropy sync is disabled".to_owned());
self.send(Message::text(reply.as_json())).await?;
return Ok(());
}
@ -677,6 +683,8 @@ impl WebSocketService {
subid
};
self.negentropy_sub = Some(subid.clone());
// Close the subscription
self.neg_subscriptions.remove(&subid);