Using awc with `LocalPoolHandle` and `thread_local!`
- Lingua principale
- Rust
- Stelle
- 24.8k
- Fork
- 1.9k
- Merge medio
- 23h 10m
- PR unite (30g)
- 26
Descrizione
Your issue may already be reported! Please search on the [Actix Web issue tracker](https://github.com/actix/actix-web/issues) before creating one.
## Expected Behavior
Not really sure whether I am using `awc::Client` in correct way but this seems like the most straightforward way to follow the "create at most one client per thread" advice from document.
## Current Behavior
```
thread '' panicked at 'called `Result::unwrap()` on an `Err` value: Connect(Io(Os { code: 99, kind: AddrNotAvailable, message: "Cannot assign requested address" }))', src/main.rs:20:69
```
## Possible Solution
## Steps to Reproduce (for bugs)
```rust
use std::{
env::args,
thread::{available_parallelism, sleep},
time::Duration,
};
use awc::Client;
use tokio_util::task::LocalPoolHandle;
fn main() {
let url = args().nth(1).unwrap();
// available parallelism = 64 in test env
// it is possible to reproduce the crashing with less worker threads e.g. 4
let pool = LocalPoolHandle::new(available_parallelism().unwrap().into());
for _ in 0..100 {
let url = url.clone();
pool.spawn_pinned(move || async move {
loop {
thread_local! {
static CLIENT: Client = Client::new();
}
CLIENT.with(|client| client.get(&url)).send().await.unwrap();
}
});
}
sleep(Duration::from_secs(10));
}
```
## Context
I was trying to build a p2p network with actix-web, each peer is made with a HTTP server and one or more clients.
The HTTP requests that sent to other peers are initiated from multiple worker threads (tasks) of a `LocalPoolHandle`. Naturally each task would create a `Client` and send its request. It is possible to pass all requests to a centralized client through channels but that adds complexity and may degrade performance.
## Your Environment
- Rust Version (I.e, output of `rustc -V`): rustc 1.72.1 (d5c2e9c34 2023-09-13)
- Actix Web Version: `awc = "3.2.0"`
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.