Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions nostr-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

-->

## Unreleased

### Changed

- local_relay: default connection limit set to 512 (https://github.com/nostrdevkit/nostr/pull/1462)

## v0.45.2 - 2026/08/19

### Fixed
Expand Down
9 changes: 5 additions & 4 deletions nostr-sdk/src/local_relay/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use nostr_database::prelude::*;
use super::local::LocalRelay;

pub(super) const DEFAULT_MAX_PENDING_HANDSHAKES: usize = 128;
pub(super) const DEFAULT_CONNECTIONS_LIMIT: usize = 512;
pub(super) const DEFAULT_MAX_FILTERS_PER_REQ: usize = 20;
pub(super) const DEFAULT_MAX_EVENT_SIZE: usize = 64 * 1024;
pub(super) const DEFAULT_MAX_QUERY_RESULTS: usize = 500;
Expand Down Expand Up @@ -285,7 +286,7 @@ pub struct LocalRelayBuilder {
/// NIP42 options
pub(crate) nip42: Option<LocalRelayBuilderNip42>,
/// Max connections allowed
pub(crate) max_connections: Option<usize>,
pub(crate) max_connections: usize,
/// Max WebSocket message size
pub(crate) max_websocket_message_size: usize,
/// Max event size in bytes
Expand Down Expand Up @@ -345,7 +346,7 @@ impl Default for LocalRelayBuilder {
auth_events_per_minute: DEFAULT_AUTH_EVENTS_PER_MINUTE,
messages_per_minute: DEFAULT_MESSAGES_PER_MINUTE,
nip42: None,
max_connections: None,
max_connections: DEFAULT_CONNECTIONS_LIMIT,
max_websocket_message_size: DEFAULT_MAX_WEBSOCKET_MESSAGE_SIZE,
max_event_size: DEFAULT_MAX_EVENT_SIZE,
websocket_handshake_timeout: DEFAULT_WEBSOCKET_HANDSHAKE_TIMEOUT,
Expand Down Expand Up @@ -448,10 +449,10 @@ impl LocalRelayBuilder {
self
}

/// Set number of max connections allowed. By default, connections are unlimited.
/// Limits the maximum number of concurrent connections. Defaults to 512.
#[inline]
pub fn max_connections(mut self, max: usize) -> Self {
self.max_connections = Some(max);
self.max_connections = max.min(tokio::sync::Semaphore::MAX_PERMITS);
self
}

Expand Down
7 changes: 3 additions & 4 deletions nostr-sdk/src/local_relay/local/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ impl InnerLocalRelay {
auth_events_per_minute: builder.auth_events_per_minute,
messages_per_minute: builder.messages_per_minute,
pending_handshakes_limit: Arc::new(Semaphore::new(DEFAULT_MAX_PENDING_HANDSHAKES)),
connections_limit: Arc::new(Semaphore::new(
builder.max_connections.unwrap_or(Semaphore::MAX_PERMITS),
)),
connections_limit: Arc::new(Semaphore::new(builder.max_connections)),
max_websocket_message_size: builder.max_websocket_message_size,
max_event_size: builder.max_event_size,
websocket_handshake_timeout: builder.websocket_handshake_timeout,
Expand Down Expand Up @@ -1655,6 +1653,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::local_relay::DEFAULT_CONNECTIONS_LIMIT;

#[derive(Debug)]
struct RejectWrites;
Expand Down Expand Up @@ -1714,7 +1713,7 @@ mod tests {
assert_eq!(relay.pending_handshakes_limit.available_permits(), 128);
assert_eq!(
relay.connections_limit.available_permits(),
Semaphore::MAX_PERMITS
DEFAULT_CONNECTIONS_LIMIT
);
assert_eq!(relay.queries_per_minute, 1_200);
assert_eq!(relay.auth_events_per_minute, 30);
Expand Down
Loading