-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tcp conn tracker * make default decay const * first pass connpool * err handling conpool start * added notes for next features * first version working * first pass spin out client_pool * cancel token * logging change * bump default decay time * bugfix: make sure to apply gateway score filtering when choosing initial node * add duplicate packets received to troubleshooting * client_pool.rs mod * client pool example * clippy * client pool example done * added disconnect to client pool * update mod file * add cancel token disconnect fn * comments * comments * add clone * added disconnect thread * update example files tcpproxy * client pool docs * remove comments for future ffi push + lower default pool size from 4 to 2 * comment on ffi * update command help * clone impl * remove clone * fix clippy * fix clippy again * fix test * tweaked text grammar * updated comment in example * future is now * cherry * cherry * fix borked rebase * fix fmt * wasm fix --------- Co-authored-by: Jędrzej Stuczyński <[email protected]>
- Loading branch information
1 parent
0a47d5d
commit e454d71
Showing
35 changed files
with
923 additions
and
230 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Client Pool | ||
|
||
We have a configurable-size Client Pool for processes that require multiple clients in quick succession (this is used by default by the [`TcpProxyClient`](./tcpproxy) for instance) | ||
|
||
This will be useful for developers looking to build connection logic, or just are using raw SDK clients in a sitatuation where there are multiple connections with a lot of churn. | ||
|
||
> You can find this code [here](https://github.com/nymtech/nym/blob/develop/sdk/rust/nym-sdk/examples/client_pool.rs) |
4 changes: 4 additions & 0 deletions
4
documentation/docs/pages/developers/rust/client-pool/_meta.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"architecture": "Architecture", | ||
"example": "Example" | ||
} |
19 changes: 19 additions & 0 deletions
19
documentation/docs/pages/developers/rust/client-pool/architecture.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Client Pool Architecture | ||
|
||
## Motivations | ||
In situations where multiple connections are expected, and the number of connections can vary greatly, the Client Pool reduces time spent waiting for the creation of a Mixnet Client blocking your code sending traffic through the Mixnet. Instead, a configurable number of Clients can be generated and run in the background which can be very quickly grabbed, used, and disconnected. | ||
|
||
The Pool can be simply run as a background process for the runtime of your program. | ||
|
||
## Clients & Lifetimes | ||
The Client Pool creates **ephemeral Mixnet Clients** which are used and then disconnected. Using the [`TcpProxy`](../tcpproxy) as an example, Clients are used for the lifetime of a single incoming TCP connection; after the TCP connection is closed, the Mixnet client is disconnected. | ||
|
||
Clients are popped from the pool when in use, and another Client is created to take its place. If connections are coming in faster than Clients are replenished, you can instead generate an ephemeral Client on the fly, or wait; this is up to the developer to decide. You can see an example of this logic in the example on the next page. | ||
|
||
## Runtime Loop | ||
Aside from a few helper / getter functions and a graceful `disconnect_pool()`, the Client Pool is mostly made up of a very simple loop around some conditional logic making up `start()`: | ||
- if the number of Clients in the pool is `< client_pool_reserve_number` (set on `new()`) then create more, | ||
- if the number of Clients in the pool `== client_pool_reserve_number` (set on `new()`) then `sleep`, | ||
- if `client_pool_reserve_number == 0` just `sleep`. | ||
|
||
`disconnect_pool()` will cause this loop to `break` via cancellation token. |
100 changes: 100 additions & 0 deletions
100
documentation/docs/pages/developers/rust/client-pool/example.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Client Pool Example | ||
|
||
> You can find this code [here](https://github.com/nymtech/nym/blob/develop/sdk/rust/nym-sdk/examples/client_pool.rs) | ||
```rust | ||
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; | ||
|
||
// This client pool is used internally by the TcpProxyClient but can also be used by the Mixnet module, in case you're quickly swapping clients in and out but won't want to use the TcpProxy module. | ||
// | ||
// Run with: cargo run --example client_pool -- ../../../envs/<NETWORK>.env | ||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
nym_bin_common::logging::setup_logging(); | ||
setup_env(std::env::args().nth(1)); | ||
|
||
let conn_pool = ClientPool::new(2); // Start the Client Pool with 2 Clients always being kept in reserve | ||
let client_maker = conn_pool.clone(); | ||
tokio::spawn(async move { | ||
client_maker.start().await?; | ||
Ok::<(), anyhow::Error>(()) | ||
}); | ||
|
||
println!("\n\nWaiting a few seconds to fill pool\n\n"); | ||
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; | ||
|
||
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()? | ||
.connect_to_mixnet() | ||
.await?; | ||
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; // Emulate doing something | ||
return 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()? | ||
.connect_to_mixnet() | ||
.await?; | ||
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; // Emulate doing something | ||
return Ok::<(), anyhow::Error>(()); | ||
}); | ||
|
||
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(()) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"architecture": "Architecture", | ||
"examples": "Examples" | ||
"examples": "Examples", | ||
"troubleshooting": "Troubleshooting" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.