Skip to content

Commit

Permalink
to static hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Jul 12, 2024
1 parent ef71163 commit 79a9af1
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/cli/runtime/http.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::thread::ThreadId;
use std::time::Duration;

use anyhow::Result;
use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions};
use hyper::body::Bytes;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use opentelemetry::metrics::Counter;
use opentelemetry::trace::SpanKind;
Expand Down Expand Up @@ -67,8 +70,9 @@ fn get_response_status(response: &reqwest_middleware::Result<reqwest::Response>)
KeyValue::new(HTTP_RESPONSE_STATUS_CODE, status_code as i64)
}

thread_local! {
static HTTP_CLIENT: RefCell<Option<ClientWithMiddleware>> = Default::default();
lazy_static! {
static ref HTTP_CLIENTS: RwLock<HashMap<ThreadId, Arc<ClientWithMiddleware>>> =
Default::default();
}

#[derive(Clone)]
Expand All @@ -81,15 +85,29 @@ impl HttpClient {
Self { upstream }
}

pub fn get_client(&self) -> ClientWithMiddleware {
HTTP_CLIENT.with_borrow_mut(|rc| {
pub fn get_client(&self) -> Arc<ClientWithMiddleware> {
let clients = HTTP_CLIENTS.read().unwrap();
let thread_id = std::thread::current().id();
let client = clients.get(&thread_id);
if let Some(client) = client {
client.clone()
} else {
drop(clients);
let mut clients = HTTP_CLIENTS.write().unwrap();
let client = Self::build_client(&self.upstream);
let client = Arc::new(client);
clients.insert(thread_id, client.clone());
drop(clients);
client
}
/*HTTP_CLIENT.with_borrow_mut(|rc| {
if rc.is_none() {
*rc = Some(Self::build_client(&self.upstream));
rc.clone().unwrap()
} else {
rc.take().unwrap()
}
})
})*/
}

fn build_client(upstream: &Upstream) -> ClientWithMiddleware {
Expand Down Expand Up @@ -156,10 +174,10 @@ impl HttpIO for NativeHttp {
err,
fields(
otel.name = "upstream_request",
otel.kind = ?SpanKind::Client,
url.full = %request.url(),
http.request.method = %request.method(),
network.protocol.version = ?request.version()
otel.kind = ? SpanKind::Client,
url.full = % request.url(),
http.request.method = % request.method(),
network.protocol.version = ? request.version()
)
)]
async fn execute(&self, mut request: reqwest::Request) -> Result<Response<Bytes>> {
Expand Down

1 comment on commit 79a9af1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.38ms 3.59ms 138.55ms 75.55%
Req/Sec 3.44k 130.95 4.02k 86.17%

410760 requests in 30.01s, 2.06GB read

Requests/sec: 13686.73

Transfer/sec: 70.25MB

Please sign in to comment.