repo_factory: make clonable

Summary:
With `MegarepoApi` struct in play, there is a genuine need to have two repo
factories in a single process: this allows the structure to be self-sufficient
and instantiated without any references to `Mononoke` from `monooke_api`.

While this need could be solved by just wrapping a `RepoFactory` in an `Arc`,
it seems like most of it is already clonable, so let's just make it fully
clonable by fixing a few remaining places. (I prefer this over `Arc`, because
there's less refactoring of unrelated code). Given that there will likely be a
single digit of repo factories instantiated in a single process, the difference
between a single arc's clone (`Arc<RepoFactory>`) and ~10 arc clones (what I
did) is negligible.

Differential Revision: D28379860

fbshipit-source-id: fbddbdc913fedcd5846366344bc2f2c1ec4bd91e
This commit is contained in:
Kostia Balytskyi 2021-05-12 05:53:09 -07:00 committed by Facebook GitHub Bot
parent 501246bbd4
commit f93404fb67

View File

@ -69,14 +69,15 @@ use virtually_sharded_blobstore::VirtuallyShardedBlobstore;
pub use blobstore_factory::{BlobstoreOptions, ReadOnlyStorage};
#[derive(Clone)]
struct RepoFactoryCache<K: Clone + Eq + Hash, V: Clone> {
cache: Mutex<HashMap<K, Arc<AsyncOnceCell<V>>>>,
cache: Arc<Mutex<HashMap<K, Arc<AsyncOnceCell<V>>>>>,
}
impl<K: Clone + Eq + Hash, V: Clone> RepoFactoryCache<K, V> {
fn new() -> Self {
RepoFactoryCache {
cache: Mutex::new(HashMap::new()),
cache: Arc::new(Mutex::new(HashMap::new())),
}
}
@ -108,6 +109,7 @@ impl<K: Clone + Eq + Hash, V: Clone> RepoFactoryCache<K, V> {
pub trait RepoFactoryOverride<T> = Fn(T) -> T + Send + Sync + 'static;
#[derive(Clone)]
pub struct RepoFactory {
pub env: Arc<MononokeEnvironment>,
censored_scuba_params: CensoredScubaParams,
@ -115,7 +117,7 @@ pub struct RepoFactory {
blobstores: RepoFactoryCache<BlobConfig, Arc<dyn Blobstore>>,
redacted_blobs:
RepoFactoryCache<MetadataDatabaseConfig, Arc<HashMap<String, RedactedMetadata>>>,
blobstore_override: Option<Box<dyn RepoFactoryOverride<Arc<dyn Blobstore>>>>,
blobstore_override: Option<Arc<dyn RepoFactoryOverride<Arc<dyn Blobstore>>>>,
scrub_handler: Arc<dyn ScrubHandler>,
blobstore_component_sampler: Option<Arc<dyn ComponentSamplingHandler>>,
}
@ -141,7 +143,7 @@ impl RepoFactory {
&mut self,
blobstore_override: impl RepoFactoryOverride<Arc<dyn Blobstore>>,
) -> &mut Self {
self.blobstore_override = Some(Box::new(blobstore_override));
self.blobstore_override = Some(Arc::new(blobstore_override));
self
}