actix / actix/actix-web

Using awc with `LocalPoolHandle` and `thread_local!`

Offen
#3,146 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
24.8k
Forks
1.9k
Ø Merge
23 Std. 10 Min.
Gemergte PRs (30 T.)
26

Beschreibung

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"`

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.