@@ -7,7 +7,7 @@ use std::fmt;
7
7
use std:: hash:: { Hash , Hasher } ;
8
8
use std:: num:: NonZeroUsize ;
9
9
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
10
- use std:: sync:: { Arc , Mutex , OnceLock } ;
10
+ use std:: sync:: { Arc , Mutex } ;
11
11
12
12
use lru:: LruCache ;
13
13
use nostr:: prelude:: IntoNostrSigner ;
@@ -16,9 +16,7 @@ use nostr_database::{IntoNostrDatabase, MemoryDatabase, NostrDatabase};
16
16
use tokio:: sync:: RwLock ;
17
17
18
18
use crate :: policy:: AdmitPolicy ;
19
- use crate :: transport:: websocket:: {
20
- DefaultWebsocketTransport , IntoWebSocketTransport , WebSocketTransport ,
21
- } ;
19
+ use crate :: transport:: websocket:: { DefaultWebsocketTransport , WebSocketTransport } ;
22
20
23
21
// LruCache pre-allocate, so keep this at a reasonable value.
24
22
// A good value may be <= 128k, considering that stored values are the 64-bit hashes of the event IDs.
@@ -27,8 +25,6 @@ const MAX_VERIFICATION_CACHE_SIZE: usize = 128_000;
27
25
#[ derive( Debug ) ]
28
26
pub enum SharedStateError {
29
27
SignerNotConfigured ,
30
- /// Admit policy already set
31
- AdmitPolicyAlreadySet ,
32
28
MutexPoisoned ,
33
29
}
34
30
@@ -38,29 +34,26 @@ impl fmt::Display for SharedStateError {
38
34
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39
35
match self {
40
36
Self :: SignerNotConfigured => write ! ( f, "signer not configured" ) ,
41
- Self :: AdmitPolicyAlreadySet => write ! ( f, "admission policy already set" ) ,
42
37
Self :: MutexPoisoned => write ! ( f, "mutex poisoned" ) ,
43
38
}
44
39
}
45
40
}
46
41
47
- // TODO: reduce atomic operations
48
42
#[ derive( Debug , Clone ) ]
49
43
pub struct SharedState {
50
44
pub ( crate ) database : Arc < dyn NostrDatabase > ,
51
45
pub ( crate ) transport : Arc < dyn WebSocketTransport > ,
52
46
signer : Arc < RwLock < Option < Arc < dyn NostrSigner > > > > ,
53
47
nip42_auto_authentication : Arc < AtomicBool > ,
54
48
verification_cache : Arc < Mutex < LruCache < u64 , ( ) > > > ,
55
- pub ( crate ) admit_policy : OnceLock < Arc < dyn AdmitPolicy > > ,
56
- // TODO: add a semaphore to limit number of concurrent websocket connections attempts?
49
+ pub ( crate ) admit_policy : Option < Arc < dyn AdmitPolicy > > ,
57
50
}
58
51
59
52
impl Default for SharedState {
60
53
fn default ( ) -> Self {
61
54
Self :: new (
62
55
MemoryDatabase :: new ( ) . into_nostr_database ( ) ,
63
- DefaultWebsocketTransport . into_transport ( ) ,
56
+ Arc :: new ( DefaultWebsocketTransport ) ,
64
57
None ,
65
58
None ,
66
59
true ,
@@ -86,33 +79,10 @@ impl SharedState {
86
79
signer : Arc :: new ( RwLock :: new ( signer) ) ,
87
80
nip42_auto_authentication : Arc :: new ( AtomicBool :: new ( nip42_auto_authentication) ) ,
88
81
verification_cache : Arc :: new ( Mutex :: new ( LruCache :: new ( max_verification_cache_size) ) ) ,
89
- admit_policy : match admit_policy {
90
- Some ( policy) => OnceLock :: from ( policy) ,
91
- None => OnceLock :: new ( ) ,
92
- } ,
82
+ admit_policy,
93
83
}
94
84
}
95
85
96
- /// Set a custom transport
97
- pub fn custom_transport < T > ( mut self , transport : T ) -> Self
98
- where
99
- T : IntoWebSocketTransport ,
100
- {
101
- self . transport = transport. into_transport ( ) ;
102
- self
103
- }
104
-
105
- /// Set an admission policy
106
- #[ inline]
107
- pub ( crate ) fn set_admit_policy < T > ( & self , policy : T ) -> Result < ( ) , SharedStateError >
108
- where
109
- T : AdmitPolicy + ' static ,
110
- {
111
- self . admit_policy
112
- . set ( Arc :: new ( policy) )
113
- . map_err ( |_| SharedStateError :: AdmitPolicyAlreadySet )
114
- }
115
-
116
86
/// Check if auto authentication to relays is enabled
117
87
#[ inline]
118
88
pub fn is_auto_authentication_enabled ( & self ) -> bool {
0 commit comments