Skip to content

Commit 9122158

Browse files
Devdutt Shenoinikhilsinhaparseable
andauthored
refactor: single reqwest::Client for HTTP (#1071)
Co-authored-by: Nikhil Sinha <[email protected]>
1 parent b46be32 commit 9122158

File tree

9 files changed

+79
-46
lines changed

9 files changed

+79
-46
lines changed

Cargo.lock

Lines changed: 27 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ relative-path = { version = "1.7", features = ["serde"] }
7373
reqwest = { version = "0.11.27", default-features = false, features = [
7474
"rustls-tls",
7575
"json",
76+
"gzip",
77+
"brotli",
7678
] } # cannot update cause rustls is not latest `see rustls`
7779
rustls = "0.22.4" # cannot update to 0.23 actix has not caught up yet
7880
rustls-pemfile = "2.1.2"

src/analytics.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::handlers::http::cluster::utils::check_liveness;
2222
use crate::handlers::http::{base_path_without_preceding_slash, cluster};
2323
use crate::handlers::STREAM_NAME_HEADER_KEY;
2424
use crate::option::{Mode, CONFIG};
25-
use crate::storage;
2625
use crate::{metadata, stats};
26+
use crate::{storage, HTTP_CLIENT};
2727

2828
use crate::stats::Stats;
2929
use actix_web::{web, HttpRequest, Responder};
@@ -132,9 +132,7 @@ impl Report {
132132
}
133133

134134
pub async fn send(&self) {
135-
let client = reqwest::Client::new();
136-
137-
let _ = client
135+
let _ = HTTP_CLIENT
138136
.post(ANALYTICS_SERVER_URL)
139137
.header(STREAM_NAME_HEADER_KEY, "serverusageevent")
140138
.json(&self)
@@ -240,7 +238,7 @@ async fn fetch_ingestors_metrics(
240238
))
241239
.expect("Should be a valid URL");
242240

243-
let resp = reqwest::Client::new()
241+
let resp = HTTP_CLIENT
244242
.get(uri)
245243
.header(header::AUTHORIZATION, im.token.clone())
246244
.header(header::CONTENT_TYPE, "application/json")

src/audit.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ use std::{
2121
fmt::{Debug, Display},
2222
};
2323

24-
use crate::{about::current, storage::StorageMetadata};
24+
use crate::{about::current, storage::StorageMetadata, HTTP_CLIENT};
2525

2626
use super::option::CONFIG;
2727
use chrono::{DateTime, Utc};
2828
use once_cell::sync::Lazy;
29-
use reqwest::Client;
3029
use serde::Serialize;
3130
use serde_json::{json, Value};
3231
use tracing::error;
@@ -38,7 +37,6 @@ static AUDIT_LOGGER: Lazy<Option<AuditLogger>> = Lazy::new(AuditLogger::new);
3837

3938
// AuditLogger handles sending audit logs to a remote logging system
4039
pub struct AuditLogger {
41-
client: Client,
4240
log_endpoint: Url,
4341
}
4442

@@ -62,16 +60,12 @@ impl AuditLogger {
6260
}
6361
};
6462

65-
Some(AuditLogger {
66-
client: reqwest::Client::new(),
67-
log_endpoint,
68-
})
63+
Some(AuditLogger { log_endpoint })
6964
}
7065

7166
// Sends the audit log to the configured endpoint with proper authentication
7267
async fn send_log(&self, json: Value) {
73-
let mut req = self
74-
.client
68+
let mut req = HTTP_CLIENT
7569
.post(self.log_endpoint.as_str())
7670
.json(&json)
7771
.header("x-p-stream", "audit_log");

src/handlers/http/cluster/mod.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::stats::Stats;
3333
use crate::storage::object_storage::ingestor_metadata_path;
3434
use crate::storage::{ObjectStorageError, STREAM_ROOT_DIRECTORY};
3535
use crate::storage::{ObjectStoreFormat, PARSEABLE_ROOT_DIRECTORY};
36+
use crate::HTTP_CLIENT;
3637
use actix_web::http::header::{self, HeaderMap};
3738
use actix_web::{HttpRequest, Responder};
3839
use bytes::Bytes;
@@ -76,8 +77,6 @@ pub async fn sync_streams_with_ingestors(
7677
StreamError::Anyhow(err)
7778
})?;
7879

79-
let client = reqwest::Client::new();
80-
8180
for ingestor in ingestor_infos {
8281
if !utils::check_liveness(&ingestor.domain_name).await {
8382
warn!("Ingestor {} is not live", ingestor.domain_name);
@@ -89,7 +88,7 @@ pub async fn sync_streams_with_ingestors(
8988
base_path_without_preceding_slash(),
9089
stream_name
9190
);
92-
let res = client
91+
let res = HTTP_CLIENT
9392
.put(url)
9493
.headers(reqwest_headers.clone())
9594
.header(header::AUTHORIZATION, &ingestor.token)
@@ -126,7 +125,6 @@ pub async fn sync_users_with_roles_with_ingestors(
126125
RBACError::Anyhow(err)
127126
})?;
128127

129-
let client = reqwest::Client::new();
130128
let role = to_vec(&role.clone()).map_err(|err| {
131129
error!("Fatal: failed to serialize role: {:?}", err);
132130
RBACError::SerdeError(err)
@@ -143,7 +141,7 @@ pub async fn sync_users_with_roles_with_ingestors(
143141
username
144142
);
145143

146-
let res = client
144+
let res = HTTP_CLIENT
147145
.put(url)
148146
.header(header::AUTHORIZATION, &ingestor.token)
149147
.header(header::CONTENT_TYPE, "application/json")
@@ -177,7 +175,6 @@ pub async fn sync_user_deletion_with_ingestors(username: &String) -> Result<(),
177175
RBACError::Anyhow(err)
178176
})?;
179177

180-
let client = reqwest::Client::new();
181178
for ingestor in ingestor_infos.iter() {
182179
if !utils::check_liveness(&ingestor.domain_name).await {
183180
warn!("Ingestor {} is not live", ingestor.domain_name);
@@ -190,7 +187,7 @@ pub async fn sync_user_deletion_with_ingestors(username: &String) -> Result<(),
190187
username
191188
);
192189

193-
let res = client
190+
let res = HTTP_CLIENT
194191
.delete(url)
195192
.header(header::AUTHORIZATION, &ingestor.token)
196193
.send()
@@ -231,7 +228,6 @@ pub async fn sync_user_creation_with_ingestors(
231228
user.roles.clone_from(role);
232229
}
233230
let username = user.username();
234-
let client = reqwest::Client::new();
235231

236232
let user = to_vec(&user).map_err(|err| {
237233
error!("Fatal: failed to serialize user: {:?}", err);
@@ -250,7 +246,7 @@ pub async fn sync_user_creation_with_ingestors(
250246
username
251247
);
252248

253-
let res = client
249+
let res = HTTP_CLIENT
254250
.post(url)
255251
.header(header::AUTHORIZATION, &ingestor.token)
256252
.header(header::CONTENT_TYPE, "application/json")
@@ -283,7 +279,6 @@ pub async fn sync_password_reset_with_ingestors(username: &String) -> Result<(),
283279
error!("Fatal: failed to get ingestor info: {:?}", err);
284280
RBACError::Anyhow(err)
285281
})?;
286-
let client = reqwest::Client::new();
287282

288283
for ingestor in ingestor_infos.iter() {
289284
if !utils::check_liveness(&ingestor.domain_name).await {
@@ -297,7 +292,7 @@ pub async fn sync_password_reset_with_ingestors(username: &String) -> Result<(),
297292
username
298293
);
299294

300-
let res = client
295+
let res = HTTP_CLIENT
301296
.post(url)
302297
.header(header::AUTHORIZATION, &ingestor.token)
303298
.header(header::CONTENT_TYPE, "application/json")
@@ -338,7 +333,6 @@ pub async fn sync_role_update_with_ingestors(
338333
RoleError::SerdeError(err)
339334
})?;
340335
let roles = Bytes::from(roles);
341-
let client = reqwest::Client::new();
342336

343337
for ingestor in ingestor_infos.iter() {
344338
if !utils::check_liveness(&ingestor.domain_name).await {
@@ -352,7 +346,7 @@ pub async fn sync_role_update_with_ingestors(
352346
name
353347
);
354348

355-
let res = client
349+
let res = HTTP_CLIENT
356350
.put(url)
357351
.header(header::AUTHORIZATION, &ingestor.token)
358352
.header(header::CONTENT_TYPE, "application/json")
@@ -401,7 +395,7 @@ pub async fn fetch_daily_stats_from_ingestors(
401395
StreamError::Anyhow(anyhow::anyhow!("Invalid URL in Ingestor Metadata: {}", err))
402396
})?;
403397

404-
let res = reqwest::Client::new()
398+
let res = HTTP_CLIENT
405399
.get(uri)
406400
.header(header::AUTHORIZATION, &ingestor.token)
407401
.header(header::CONTENT_TYPE, "application/json")
@@ -512,8 +506,7 @@ pub async fn send_stream_delete_request(
512506
if !utils::check_liveness(&ingestor.domain_name).await {
513507
return Ok(());
514508
}
515-
let client = reqwest::Client::new();
516-
let resp = client
509+
let resp = HTTP_CLIENT
517510
.delete(url)
518511
.header(header::CONTENT_TYPE, "application/json")
519512
.header(header::AUTHORIZATION, ingestor.token)
@@ -551,8 +544,7 @@ pub async fn send_retention_cleanup_request(
551544
if !utils::check_liveness(&ingestor.domain_name).await {
552545
return Ok(first_event_at);
553546
}
554-
let client = reqwest::Client::new();
555-
let resp = client
547+
let resp = HTTP_CLIENT
556548
.post(url)
557549
.header(header::CONTENT_TYPE, "application/json")
558550
.header(header::AUTHORIZATION, ingestor.token)
@@ -603,7 +595,7 @@ pub async fn get_cluster_info() -> Result<impl Responder, StreamError> {
603595
))
604596
.expect("should always be a valid url");
605597

606-
let resp = reqwest::Client::new()
598+
let resp = HTTP_CLIENT
607599
.get(uri)
608600
.header(header::AUTHORIZATION, ingestor.token.clone())
609601
.header(header::CONTENT_TYPE, "application/json")
@@ -752,7 +744,7 @@ async fn fetch_cluster_metrics() -> Result<Vec<Metrics>, PostError> {
752744
PostError::Invalid(anyhow::anyhow!("Invalid URL in Ingestor Metadata: {}", err))
753745
})?;
754746

755-
let res = reqwest::Client::new()
747+
let res = HTTP_CLIENT
756748
.get(uri)
757749
.header(header::AUTHORIZATION, &ingestor.token)
758750
.header(header::CONTENT_TYPE, "application/json")

src/handlers/http/cluster/utils.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
*
1717
*/
1818

19-
use crate::handlers::http::{
20-
base_path_without_preceding_slash, logstream::error::StreamError, modal::IngestorMetadata,
19+
use crate::{
20+
handlers::http::{
21+
base_path_without_preceding_slash, logstream::error::StreamError, modal::IngestorMetadata,
22+
},
23+
HTTP_CLIENT,
2124
};
2225
use actix_web::http::header;
2326
use chrono::{DateTime, Utc};
@@ -235,13 +238,13 @@ pub async fn check_liveness(domain_name: &str) -> bool {
235238
}
236239
};
237240

238-
let reqw = reqwest::Client::new()
241+
let req = HTTP_CLIENT
239242
.get(uri)
240243
.header(header::CONTENT_TYPE, "application/json")
241244
.send()
242245
.await;
243246

244-
reqw.is_ok()
247+
req.is_ok()
245248
}
246249

247250
/// send a request to the ingestor to fetch its stats
@@ -255,8 +258,7 @@ pub async fn send_stats_request(
255258
return Ok(None);
256259
}
257260

258-
let client = reqwest::Client::new();
259-
let res = client
261+
let res = HTTP_CLIENT
260262
.get(url)
261263
.header(header::CONTENT_TYPE, "application/json")
262264
.header(header::AUTHORIZATION, ingestor.token)

src/handlers/http/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use http::StatusCode;
2323
use itertools::Itertools;
2424
use serde_json::Value;
2525

26-
use crate::{option::CONFIG, storage::STREAM_ROOT_DIRECTORY};
26+
use crate::{option::CONFIG, storage::STREAM_ROOT_DIRECTORY, HTTP_CLIENT};
2727

2828
use self::{cluster::get_ingestor_info, query::Query};
2929

@@ -109,7 +109,7 @@ pub async fn send_query_request_to_ingestor(query: &Query) -> anyhow::Result<Vec
109109
base_path_without_preceding_slash(),
110110
"query"
111111
);
112-
let reqw = reqwest::Client::new()
112+
let reqw = HTTP_CLIENT
113113
.post(uri)
114114
.json(query)
115115
.header(http::header::AUTHORIZATION, im.token.clone())

0 commit comments

Comments
 (0)