This repository has no description
0

Configure Feed

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

core / knot2 / crates / knot-xrpc / src / locks.rs
944 B 38 lines
1use std::collections::hash_map::DefaultHasher; 2use std::hash::{Hash, Hasher}; 3use std::sync::{Mutex, MutexGuard}; 4 5use knot_types::RepoDid; 6 7const REPO_SHARDS: usize = 64; 8 9pub struct CobLocks { 10 meta: Mutex<()>, 11 repos: Vec<Mutex<()>>, 12} 13 14impl Default for CobLocks { 15 fn default() -> Self { 16 Self { 17 meta: Mutex::new(()), 18 repos: (0..REPO_SHARDS).map(|_| Mutex::new(())).collect(), 19 } 20 } 21} 22 23impl CobLocks { 24 pub fn meta(&self) -> MutexGuard<'_, ()> { 25 self.meta 26 .lock() 27 .unwrap_or_else(|poisoned| poisoned.into_inner()) 28 } 29 30 pub fn repo(&self, repo: &RepoDid) -> MutexGuard<'_, ()> { 31 let mut hasher = DefaultHasher::new(); 32 repo.as_str().hash(&mut hasher); 33 let shard = (hasher.finish() as usize) % self.repos.len(); 34 self.repos[shard] 35 .lock() 36 .unwrap_or_else(|poisoned| poisoned.into_inner()) 37 } 38}