A better Rust ATProto crate

resolver cache tunables

Orual e3b48af3 cfedbbd0

Changed files
+92 -10
crates
jacquard-identity
src
+92 -10
crates/jacquard-identity/src/lib.rs
··· 110 110 ))] 111 111 use std::sync::Arc; 112 112 113 + /// Configuration for resolver caching 114 + #[cfg(feature = "cache")] 115 + #[derive(Clone, Debug)] 116 + pub struct CacheConfig { 117 + /// Maximum capacity for handle→DID cache 118 + pub handle_to_did_capacity: u64, 119 + /// TTL for handle→DID cache 120 + pub handle_to_did_ttl: Duration, 121 + /// Maximum capacity for DID→document cache 122 + pub did_to_doc_capacity: u64, 123 + /// TTL for DID→document cache 124 + pub did_to_doc_ttl: Duration, 125 + /// Maximum capacity for authority→DID cache 126 + pub authority_to_did_capacity: u64, 127 + /// TTL for authority→DID cache 128 + pub authority_to_did_ttl: Duration, 129 + /// Maximum capacity for NSID→schema cache 130 + pub nsid_to_schema_capacity: u64, 131 + /// TTL for NSID→schema cache 132 + pub nsid_to_schema_ttl: Duration, 133 + } 134 + 135 + #[cfg(feature = "cache")] 136 + impl Default for CacheConfig { 137 + fn default() -> Self { 138 + Self { 139 + handle_to_did_capacity: 2000, 140 + handle_to_did_ttl: Duration::from_secs(24 * 3600), 141 + did_to_doc_capacity: 1000, 142 + did_to_doc_ttl: Duration::from_secs(72 * 3600), 143 + authority_to_did_capacity: 1000, 144 + authority_to_did_ttl: Duration::from_secs(168 * 3600), 145 + nsid_to_schema_capacity: 1000, 146 + nsid_to_schema_ttl: Duration::from_secs(168 * 3600), 147 + } 148 + } 149 + } 150 + 151 + #[cfg(feature = "cache")] 152 + impl CacheConfig { 153 + /// Set handle→DID cache parameters 154 + pub fn with_handle_cache(mut self, capacity: u64, ttl: Duration) -> Self { 155 + self.handle_to_did_capacity = capacity; 156 + self.handle_to_did_ttl = ttl; 157 + self 158 + } 159 + 160 + /// Set DID→document cache parameters 161 + pub fn with_did_doc_cache(mut self, capacity: u64, ttl: Duration) -> Self { 162 + self.did_to_doc_capacity = capacity; 163 + self.did_to_doc_ttl = ttl; 164 + self 165 + } 166 + 167 + /// Set authority→DID cache parameters 168 + pub fn with_authority_cache(mut self, capacity: u64, ttl: Duration) -> Self { 169 + self.authority_to_did_capacity = capacity; 170 + self.authority_to_did_ttl = ttl; 171 + self 172 + } 173 + 174 + /// Set NSID→schema cache parameters 175 + pub fn with_schema_cache(mut self, capacity: u64, ttl: Duration) -> Self { 176 + self.nsid_to_schema_capacity = capacity; 177 + self.nsid_to_schema_ttl = ttl; 178 + self 179 + } 180 + } 181 + 113 182 /// Cache layer for resolver operations 114 183 #[cfg(feature = "cache")] 115 184 #[derive(Clone)] ··· 122 191 123 192 #[cfg(feature = "cache")] 124 193 impl ResolverCaches { 125 - fn new() -> Self { 194 + fn new(config: &CacheConfig) -> Self { 126 195 Self { 127 196 handle_to_did: Cache::builder() 128 - .max_capacity(2000) 129 - .time_to_live(Duration::from_secs(24 * 3600)) 197 + .max_capacity(config.handle_to_did_capacity) 198 + .time_to_live(config.handle_to_did_ttl) 130 199 .build(), 131 200 did_to_doc: Cache::builder() 132 - .max_capacity(1000) 133 - .time_to_live(Duration::from_secs(72 * 3600)) 201 + .max_capacity(config.did_to_doc_capacity) 202 + .time_to_live(config.did_to_doc_ttl) 134 203 .build(), 135 204 authority_to_did: Cache::builder() 136 - .max_capacity(200) 137 - .time_to_live(Duration::from_secs(168 * 3600)) 205 + .max_capacity(config.authority_to_did_capacity) 206 + .time_to_live(config.authority_to_did_ttl) 138 207 .build(), 139 208 nsid_to_schema: Cache::builder() 140 - .max_capacity(500) 141 - .time_to_live(Duration::from_secs(168 * 3600)) 209 + .max_capacity(config.nsid_to_schema_capacity) 210 + .time_to_live(config.nsid_to_schema_ttl) 142 211 .build(), 143 212 } 213 + } 214 + } 215 + 216 + impl Default for ResolverCaches { 217 + fn default() -> Self { 218 + Self::new(&CacheConfig::default()) 144 219 } 145 220 } 146 221 ··· 222 297 #[cfg(feature = "cache")] 223 298 /// Enable caching with default configuration 224 299 pub fn with_cache(mut self) -> Self { 225 - self.caches = Some(ResolverCaches::new()); 300 + self.caches = Some(ResolverCaches::default()); 301 + self 302 + } 303 + 304 + #[cfg(feature = "cache")] 305 + /// Enable caching with custom configuration 306 + pub fn with_cache_config(mut self, config: CacheConfig) -> Self { 307 + self.caches = Some(ResolverCaches::new(&config)); 226 308 self 227 309 } 228 310