|
@@ -5,7 +5,10 @@ use nostr_rs_types::{
|
|
types::Addr,
|
|
types::Addr,
|
|
Request, Response,
|
|
Request, Response,
|
|
};
|
|
};
|
|
-use std::collections::HashMap;
|
|
|
|
|
|
+use std::{
|
|
|
|
+ collections::HashMap,
|
|
|
|
+ sync::atomic::{AtomicUsize, Ordering},
|
|
|
|
+};
|
|
use tokio::{
|
|
use tokio::{
|
|
net::TcpStream,
|
|
net::TcpStream,
|
|
sync::{
|
|
sync::{
|
|
@@ -17,6 +20,16 @@ use tokio::{
|
|
#[allow(unused_imports)]
|
|
#[allow(unused_imports)]
|
|
use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
|
use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
|
|
|
|
|
|
|
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
|
|
|
+pub struct ConnectionId(pub(crate) usize);
|
|
|
|
+
|
|
|
|
+impl Default for ConnectionId {
|
|
|
|
+ fn default() -> Self {
|
|
|
|
+ static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
|
|
|
|
+ Self(NEXT_ID.fetch_add(1, Ordering::SeqCst))
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
#[derive(Debug)]
|
|
#[derive(Debug)]
|
|
/// Relayer connection
|
|
/// Relayer connection
|
|
///
|
|
///
|
|
@@ -24,9 +37,9 @@ use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
|
/// upcoming messages form the client
|
|
/// upcoming messages form the client
|
|
pub struct Connection {
|
|
pub struct Connection {
|
|
#[allow(unused)]
|
|
#[allow(unused)]
|
|
- pub(crate) conn_id: u128,
|
|
|
|
|
|
+ pub(crate) conn_id: ConnectionId,
|
|
sender: Sender<Response>,
|
|
sender: Sender<Response>,
|
|
- subscriptions: RwLock<HashMap<String, u128>>,
|
|
|
|
|
|
+ subscriptions: RwLock<HashMap<String, ConnectionId>>,
|
|
handler: Option<JoinHandle<()>>,
|
|
handler: Option<JoinHandle<()>>,
|
|
}
|
|
}
|
|
|
|
|
|
@@ -46,7 +59,7 @@ impl Connection {
|
|
let (sender, receiver) = channel(MAX_SUBSCRIPTIONS_BUFFER);
|
|
let (sender, receiver) = channel(MAX_SUBSCRIPTIONS_BUFFER);
|
|
(
|
|
(
|
|
Self {
|
|
Self {
|
|
- conn_id: 0,
|
|
|
|
|
|
+ conn_id: ConnectionId::default(),
|
|
sender,
|
|
sender,
|
|
subscriptions: RwLock::new(HashMap::new()),
|
|
subscriptions: RwLock::new(HashMap::new()),
|
|
handler: None,
|
|
handler: None,
|
|
@@ -156,9 +169,11 @@ impl Connection {
|
|
}
|
|
}
|
|
|
|
|
|
/// Create a subscription for this connection
|
|
/// Create a subscription for this connection
|
|
- pub async fn create_subscription(&self, id: String) -> (u128, Sender<Response>) {
|
|
|
|
|
|
+ pub async fn create_subscription(&self, id: String) -> (ConnectionId, Sender<Response>) {
|
|
let mut subscriptions = self.subscriptions.write().await;
|
|
let mut subscriptions = self.subscriptions.write().await;
|
|
- let internal_id = subscriptions.entry(id).or_insert_with(get_id);
|
|
|
|
|
|
+ let internal_id = subscriptions
|
|
|
|
+ .entry(id)
|
|
|
|
+ .or_insert_with(ConnectionId::default);
|
|
(*internal_id, self.sender.clone())
|
|
(*internal_id, self.sender.clone())
|
|
}
|
|
}
|
|
}
|
|
}
|