Skip to content

Commit

Permalink
feat(shuttle): add options to enable and configure GC (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored Dec 12, 2024
1 parent 1399e17 commit 5513ff2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
22 changes: 20 additions & 2 deletions extensions/warp-ipfs/shuttle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

use std::path::PathBuf;

use base64::{
alphabet::STANDARD,
engine::{general_purpose::PAD, GeneralPurpose},
Engine,
};
use clap::Parser;
use rust_ipfs::{Keypair, Multiaddr};
use std::path::PathBuf;
use std::time::Duration;

use zeroize::Zeroizing;

Expand Down Expand Up @@ -70,6 +70,20 @@ struct Opt {
/// TLS Private Key when websocket is used
#[clap(long)]
ws_tls_private_key: Option<PathBuf>,

/// Enable GC to cleanup any unpinned or orphaned blocks
#[clap(long)]
enable_gc: bool,

/// Run GC at start
/// Note: its recommended not to use this if GC is enabled.
#[clap(long)]
run_gc_once: bool,

/// GC Duration in seconds on how often GC should run
/// Note: NOOP if `enable_gc` is false
#[clap(long)]
gc_duration: Option<u16>,
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -162,6 +176,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
false,
&opts.listen_addr,
&opts.external_addr,
opts.enable_gc,
opts.run_gc_once,
opts.gc_duration.map(u64::from).map(Duration::from_secs),
None,
true,
)
.await?;
Expand Down
27 changes: 22 additions & 5 deletions extensions/warp-ipfs/src/shuttle/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl ShuttleServer {
memory_transport: bool,
listen_addrs: &[Multiaddr],
external_addrs: &[Multiaddr],
enable_gc: bool,
run_gc_once: bool,
gc_duration: Option<Duration>,
gc_trigger: Option<GCTrigger>,
ext: bool,
) -> anyhow::Result<Self> {
let executor = LocalExecutor;
Expand Down Expand Up @@ -123,11 +127,6 @@ impl ShuttleServer {
websocket_pem: wss_certs_and_key,
..Default::default()
})
// TODO: Either enable GC or do manual GC during little to no activity unless we reach a specific threshold
.with_gc(GCConfig {
duration: Duration::from_secs(60),
trigger: GCTrigger::None,
})
.set_temp_pin_duration(Duration::from_secs(60 * 30))
.with_request_response(vec![
RequestResponseConfig {
Expand All @@ -144,6 +143,13 @@ impl ShuttleServer {
},
]);

if enable_gc {
let duration = gc_duration.unwrap_or(Duration::from_secs(60 * 24));
let trigger = gc_trigger.unwrap_or_default();
// TODO: maybe do manual GC during little to no activity unless we reach a specific threshold?
uninitialized = uninitialized.with_gc(GCConfig { duration, trigger })
}

if enable_relay_server {
// Relay is unbound or with higher limits so we can avoid having the connection resetting
uninitialized = uninitialized.with_relay_server(RelayConfig::unbounded());
Expand All @@ -170,6 +176,17 @@ impl ShuttleServer {

let ipfs = uninitialized.start().await?;

if run_gc_once {
match ipfs.gc().await {
Ok(blocks) => {
tracing::info!(blocks_removed = blocks.len(), "cleaned up unpinned blocks")
}
Err(e) => {
tracing::warn!(error = %e, "unable to run GC")
}
}
}

for addr in addrs {
ipfs.add_listening_address(addr).await?;
}
Expand Down

0 comments on commit 5513ff2

Please sign in to comment.