9db748e8dd
* Improve SDK rustdoc and add ARCHITECTURE.md files - Rewrite lib.rs module docs with quick-start example and module overview - Add stream example and include_str! ARCHITECTURE.md to mixnet module - Add ARCHITECTURE.md for mixnet, client_pool, and stream modules - Add rustdoc to MixnetClientBuilder, MixnetClientSender, MixnetMessageSender - Add cancel safety and drop behavior annotations to async methods - Add TcpProxy deprecation notice pointing to stream module * Fix rustdoc errors and add stepwise comments to remaining examples Rustdoc fixes: - Add missing .unwrap() on connect_new example - Replace broken turbofish intra-doc link in MixnetClientBuilder - Fix NymProxyServer::new args in tcp_proxy example - Wrap BandwidthImporter example in scoped block to fix borrow-then-move - Change misleading "5-hop routing" to "multi-hop routing" - Fix copy-paste "forget me" in send_remember_me error message - Fix wrong cargo run command in stream_simple_read_write - Fix DecayWrapper description * Cut down doc comment length * Trimmed down SDK ARCHITECTURE files * Slim Rust SDK docs and rename opener to dialer - Merge tour page into SDK landing page, delete tour.mdx - Trim all three tutorials: cut boilerplate, duplicated code, and misplaced content - Make FFI page evergreen with Go and C++ snippets, link to repo examples - Rename "opener" to "dialer" in stream docs, source ARCHITECTURE.md, and rustdoc - Add reply-to-open arrow in stream mermaid diagram - Replace remaining Unicode dashes in mermaid flowchart * - elevate streams in rustdoc: examples on lib.rs, MixnetClient, open_stream, listener - add stream quick reference to mixnet ARCHITECTURE.md - add stream types to key types list in ARCHITECTURE.md - add docs.rs links for AsyncRead/AsyncWrite and stream submodule - tcp_proxy: replace bold deprecation with warning box * - replace individual example doc pages with GitHub-linked tables - add step-by-step inline comments to all SDK example source files - add doc comments to examples missing them (simple, surb_reply, builder, etc.) - expand mixnet tutorial with persistent identity and split_sender sections - add tcpproxy tutorial - rename "API Reference" to "TypeDoc Reference" in TS SDK sidebar - rename "Misc" to "Extras" in developer sidebar, move VPN CLI up - remove echo server from tools - update message-queue callout to reference actual modules - fix mixnet/examples redirect collision * Add missing mut to example code * Update ARCHITECTURE.md with LP Framing + stream examples with sequencing * Update doc comment in utils.rs * Standardise commenting style across Rust SDK examples * Fix inline doc examples and trim re-export boilerplate * Update sdk/rust/nym-sdk/examples/bandwidth.rs Co-authored-by: Simon Wicky <simon@nymtech.net> * Fix review comments --------- Co-authored-by: Simon Wicky <simon@nymtech.net>
115 lines
4.1 KiB
Rust
115 lines
4.1 KiB
Rust
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//! Pre-warmed pool of ephemeral Mixnet clients.
|
|
//!
|
|
//! The pool keeps a reserve of connected clients so that new connections
|
|
//! can be served instantly. When the pool is empty, a fallback ephemeral
|
|
//! client is created on-demand (with higher latency).
|
|
//!
|
|
//! Run with: cargo run --example client_pool -- ../../../envs/<NETWORK>.env
|
|
|
|
use anyhow::Result;
|
|
use nym_network_defaults::setup_env;
|
|
use nym_sdk::client_pool::ClientPool;
|
|
use nym_sdk::mixnet::{MixnetClientBuilder, NymNetworkDetails};
|
|
use tokio::signal::ctrl_c;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
nym_bin_common::logging::setup_tracing_logger();
|
|
setup_env(std::env::args().nth(1));
|
|
|
|
// Create a pool that maintains 2 clients in reserve.
|
|
let conn_pool = ClientPool::new(2);
|
|
|
|
// Start the pool's background loop in a spawned task.
|
|
// It will continuously create clients until the reserve is full.
|
|
let client_maker = conn_pool.clone();
|
|
tokio::spawn(async move {
|
|
client_maker.start().await?;
|
|
Ok::<(), anyhow::Error>(())
|
|
});
|
|
|
|
// Wait for the pool to fill up.
|
|
println!("\n\nWaiting a few seconds to fill pool\n\n");
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(15)).await;
|
|
|
|
// Grab clients from the pool in two concurrent tasks.
|
|
// If the pool is empty, fall back to creating an ephemeral client.
|
|
let pool_clone_one = conn_pool.clone();
|
|
let pool_clone_two = conn_pool.clone();
|
|
|
|
tokio::spawn(async move {
|
|
let client_one = match pool_clone_one.get_mixnet_client().await {
|
|
Some(client) => {
|
|
println!("Grabbed client {} from pool", client.nym_address());
|
|
client
|
|
}
|
|
None => {
|
|
println!("Not enough clients in pool, creating ephemeral client");
|
|
let net = NymNetworkDetails::new_from_env();
|
|
let client = MixnetClientBuilder::new_ephemeral()
|
|
.network_details(net)
|
|
.build()
|
|
.unwrap()
|
|
.connect_to_mixnet()
|
|
.await
|
|
.unwrap();
|
|
println!(
|
|
"Using {} for the moment, created outside of the connection pool",
|
|
client.nym_address()
|
|
);
|
|
client
|
|
}
|
|
};
|
|
let our_address = client_one.nym_address();
|
|
println!("\n\nClient 1: {our_address}\n\n");
|
|
client_one.disconnect().await;
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
|
|
Ok::<(), anyhow::Error>(())
|
|
});
|
|
|
|
tokio::spawn(async move {
|
|
let client_two = match pool_clone_two.get_mixnet_client().await {
|
|
Some(client) => {
|
|
println!("Grabbed client {} from pool", client.nym_address());
|
|
client
|
|
}
|
|
None => {
|
|
println!("Not enough clients in pool, creating ephemeral client");
|
|
let net = NymNetworkDetails::new_from_env();
|
|
let client = MixnetClientBuilder::new_ephemeral()
|
|
.network_details(net)
|
|
.build()
|
|
.unwrap()
|
|
.connect_to_mixnet()
|
|
.await
|
|
.unwrap();
|
|
println!(
|
|
"Using {} for the moment, created outside of the connection pool",
|
|
client.nym_address()
|
|
);
|
|
client
|
|
}
|
|
};
|
|
let our_address = *client_two.nym_address();
|
|
println!("\n\nClient 2: {our_address}\n\n");
|
|
client_two.disconnect().await;
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
|
|
Ok::<(), anyhow::Error>(())
|
|
});
|
|
|
|
// Wait for ctrl-c, then shut down the pool.
|
|
wait_for_ctrl_c(conn_pool).await?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn wait_for_ctrl_c(pool: ClientPool) -> Result<()> {
|
|
println!("\n\nPress CTRL_C to disconnect pool\n\n");
|
|
ctrl_c().await?;
|
|
println!("CTRL_C received. Killing client pool");
|
|
pool.disconnect_pool().await;
|
|
Ok(())
|
|
}
|