Skip to content

Commit f8f8b03

Browse files
committed
graph: Use caching IPFS clients
Get the settings for CachingClient from the environment
1 parent e29f1df commit f8f8b03

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

graph/src/ipfs/cache.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use lru_time_cache::LruCache;
1111
use object_store::{local::LocalFileSystem, path::Path, ObjectStore};
1212
use slog::{warn, Logger};
1313

14-
use crate::prelude::CheapClone;
14+
use crate::{env::ENV_VARS, prelude::CheapClone};
1515

1616
use super::{ContentPath, IpfsClient, IpfsRequest, IpfsResponse, IpfsResult, RetryPolicy};
1717

@@ -119,14 +119,14 @@ pub struct CachingClient {
119119
}
120120

121121
impl CachingClient {
122-
#[allow(dead_code)]
123-
pub fn new(
124-
client: Arc<dyn IpfsClient>,
125-
max_entry_size: usize,
126-
cache_capacity: usize,
127-
cache_path: Option<PathBuf>,
128-
) -> Self {
129-
let cache = Cache::new(cache_capacity, max_entry_size, cache_path);
122+
pub fn new(client: Arc<dyn IpfsClient>) -> Self {
123+
let env = &ENV_VARS.mappings;
124+
125+
let cache = Cache::new(
126+
env.max_ipfs_cache_size as usize,
127+
env.max_ipfs_cache_file_size,
128+
env.ipfs_cache_location.clone(),
129+
);
130130
CachingClient { client, cache }
131131
}
132132

graph/src/ipfs/gateway_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct IpfsGatewayClient {
3434
impl IpfsGatewayClient {
3535
/// Creates a new [IpfsGatewayClient] with the specified server address.
3636
/// Verifies that the server is responding to IPFS gateway requests.
37-
pub async fn new(server_address: impl AsRef<str>, logger: &Logger) -> IpfsResult<Self> {
37+
pub(crate) async fn new(server_address: impl AsRef<str>, logger: &Logger) -> IpfsResult<Self> {
3838
let client = Self::new_unchecked(server_address, logger)?;
3939

4040
client

graph/src/ipfs/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use anyhow::anyhow;
4+
use cache::CachingClient;
45
use futures03::future::BoxFuture;
56
use futures03::stream::FuturesUnordered;
67
use futures03::stream::StreamExt;
@@ -40,6 +41,8 @@ pub type IpfsResult<T> = Result<T, IpfsError>;
4041
/// If multiple IPFS server addresses are specified, an IPFS client pool is created internally
4142
/// and for each IPFS request, the fastest client that can provide the content is
4243
/// automatically selected and the response is streamed from that client.
44+
///
45+
/// All clients are set up to cache results
4346
pub async fn new_ipfs_client<I, S>(
4447
server_addresses: I,
4548
logger: &Logger,
@@ -59,7 +62,8 @@ where
5962
SafeDisplay(server_address)
6063
);
6164

62-
clients.push(use_first_valid_api(server_address, logger).await?);
65+
let client = CachingClient::new(use_first_valid_api(server_address, logger).await?);
66+
clients.push(Arc::new(client));
6367
}
6468

6569
match clients.len() {

0 commit comments

Comments
 (0)