don't
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(identity): allow `MemcacheResolver` wrap any other resolver

Signed-off-by: tjh <x@tjh.dev>

tjh e6152019 c582ec79

+41 -29
+12 -8
crates/identity/src/lib.rs
··· 1 1 mod document; 2 - mod resolvers; 2 + pub mod resolvers; 3 3 4 4 use core::fmt; 5 5 use std::sync::Arc; ··· 17 17 pub type HttpClient = reqwest::Client; 18 18 pub type HttpError = reqwest::Error; 19 19 20 - trait ResolveIdentity: fmt::Debug + Sync { 20 + pub trait ResolveIdentity: fmt::Debug + Sync { 21 21 /// Resolve a handle or a DID to a DID and DID document. 22 22 /// 23 23 fn resolve<'s: 'a, 'a>( ··· 259 259 .plc_directory(self.plc_directory) 260 260 .build_with(http), 261 261 ), 262 - ResolverBackend::Memcache => Arc::new( 263 - MemcacheResolver::builder() 264 - .capacity(self.cache_capacity) 265 - .ttl(self.cache_ttl) 262 + ResolverBackend::Memcache => { 263 + let resolver = DirectResolver::builder() 266 264 .plc_directory(self.plc_directory) 267 - .build_with(http), 268 - ), 265 + .build_with(http); 266 + Arc::new( 267 + MemcacheResolver::builder() 268 + .capacity(self.cache_capacity) 269 + .ttl(self.cache_ttl) 270 + .build_with(resolver), 271 + ) 272 + } 269 273 }; 270 274 271 275 Resolver { inner }
+29 -21
crates/identity/src/resolvers/memcache.rs
··· 1 - use std::{borrow::Cow, sync::Arc, time::Duration}; 1 + use std::{sync::Arc, time::Duration}; 2 2 3 3 use atproto::did::OwnedDid; 4 4 use futures_util::{FutureExt as _, TryFutureExt as _, future::BoxFuture}; 5 - use hickory_resolver::name_server::TokioConnectionProvider; 6 5 use moka::future::{Cache, CacheBuilder}; 7 6 8 - use crate::{DEFAULT_PLC_DIRECTORY, Did, DidDocument, HttpClient, ResolveError, ResolveIdentity}; 9 - 10 - use super::direct::DirectResolver; 7 + use crate::{Did, DidDocument, ResolveError, ResolveIdentity}; 11 8 12 9 const DEFAULT_DID_CACHE_CAP: u64 = 1024; 13 10 const DEFAULT_DOC_CACHE_CAP: u64 = 1024; ··· 12 15 const DEFAULT_DID_CACHE_TTL: Duration = Duration::from_secs(600); 13 16 const DEFAULT_DOC_CACHE_TTL: Duration = Duration::from_secs(600); 14 17 18 + /// An indentity resolver with an in-memory cache. 15 19 #[derive(Debug)] 16 20 pub struct MemcacheResolver { 17 21 did_cache: Cache<Box<str>, OwnedDid>, 18 22 doc_cache: Cache<OwnedDid, DidDocument>, 19 - inner: DirectResolver<'static, TokioConnectionProvider>, 23 + inner: Box<dyn ResolveIdentity + Send + 'static>, 20 24 } 21 25 22 26 impl MemcacheResolver { 27 + /// Create a memory-caching resolver wrapping another resolver. 28 + pub fn wrap<R>(resolver: R) -> Self 29 + where 30 + R: ResolveIdentity + Send + 'static, 31 + { 32 + let did_cache = CacheBuilder::new(DEFAULT_DID_CACHE_CAP) 33 + .time_to_live(DEFAULT_DID_CACHE_TTL) 34 + .build(); 35 + 36 + let doc_cache = CacheBuilder::new(DEFAULT_DOC_CACHE_CAP) 37 + .time_to_live(DEFAULT_DOC_CACHE_TTL) 38 + .build(); 39 + 40 + Self { 41 + did_cache, 42 + doc_cache, 43 + inner: Box::new(resolver), 44 + } 45 + } 46 + 23 47 pub fn builder() -> MemcacheResolverBuilder { 24 48 MemcacheResolverBuilder { 25 - plc_directory: Cow::Borrowed(DEFAULT_PLC_DIRECTORY), 26 49 did_cache_capacity: DEFAULT_DID_CACHE_CAP, 27 50 did_cache_ttl: DEFAULT_DID_CACHE_TTL, 28 51 doc_cache_capacity: DEFAULT_DOC_CACHE_CAP, ··· 87 70 } 88 71 89 72 pub struct MemcacheResolverBuilder { 90 - plc_directory: Cow<'static, str>, 91 - 92 73 did_cache_capacity: u64, 93 74 did_cache_ttl: Duration, 94 75 ··· 125 110 self 126 111 } 127 112 128 - /// Set the PLC directory to use. Must be a well-formed URL. 129 - /// 130 - /// Defaults to "https://plc.directory". 131 - pub fn plc_directory(mut self, plc_directory: impl Into<Cow<'static, str>>) -> Self { 132 - self.plc_directory = plc_directory.into(); 133 - self 134 - } 135 - 136 - pub fn build_with(self, http: HttpClient) -> MemcacheResolver { 113 + pub fn build_with<R>(self, resolver: R) -> MemcacheResolver 114 + where 115 + R: ResolveIdentity + Send + 'static, 116 + { 137 117 let did_cache = CacheBuilder::new(self.did_cache_capacity) 138 118 .time_to_live(self.did_cache_ttl) 139 119 .build(); ··· 140 130 MemcacheResolver { 141 131 did_cache, 142 132 doc_cache, 143 - inner: DirectResolver::builder() 144 - .plc_directory(self.plc_directory) 145 - .build_with(http), 133 + inner: Box::new(resolver), 146 134 } 147 135 } 148 136 }