-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmulti_thread.rs
More file actions
36 lines (30 loc) · 910 Bytes
/
multi_thread.rs
File metadata and controls
36 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use feignhttp::{FeignClientBuilder, feign};
use std::sync::Arc;
#[feign(
url = "https://httpbin.org/headers",
headers = "Authorization: Bearer {token}"
)]
pub trait UserClient {
#[get]
async fn get_headers(&self, #[param] token: &str) -> feignhttp::Result<String>;
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let tokens = vec!["token_a", "token_b", "token_c"];
let client = Arc::new(UserClient::builder().build()?);
let handles: Vec<_> = tokens
.into_iter()
.map(|token| {
let client = client.clone();
async move {
let r = client.get_headers(token).await;
println!("token: {}, result: {}", token, r.unwrap());
}
})
.map(|fut| tokio::spawn(fut))
.collect();
for handle in handles {
handle.await.unwrap();
}
Ok(())
}