Skip to content

Commit 8ca0102

Browse files
fix: do not create http clients every 10s (#239)
Co-authored-by: koalp <[email protected]>
1 parent 08e0fb0 commit 8ca0102

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/session.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ impl SessionManager {
5151
// Create a thread for the SessionManager
5252
let handle = Some(thread::spawn(move || {
5353
log::trace!(target: LOG_TAG, "Started");
54+
let client = reqwest::blocking::Client::new();
5455
while let Ok(signal) = rx.recv() {
5556
match signal {
5657
SessionSignal::Session(session) => {
5758
// Send the session
5859
// Matching is done here (instead of ?) to avoid breaking
5960
// the SessionManager thread if the server is not available.
60-
match session.send() {
61+
match session.send_with_client(&client) {
6162
Ok(_) => log::trace!("SessionManager - Session sent"),
6263
Err(e) => log::error!("SessionManager - Failed to send session: {}", e),
6364
}
@@ -125,16 +126,13 @@ impl Session {
125126
})
126127
}
127128

128-
/// Send the session to the server and consumes the session object.
129-
/// # Example
130-
/// ```ignore
131-
/// let config = PyroscopeConfig::new("https://localhost:8080", "my-app");
132-
/// let report = vec![1, 2, 3];
133-
/// let until = 154065120;
134-
/// let session = Session::new(until, config, report)?;
135-
/// session.send()?;
136-
/// ```
129+
/// deprecated
137130
pub fn send(self) -> Result<()> {
131+
let client = reqwest::blocking::Client::new();
132+
self.send_with_client(&client)
133+
}
134+
135+
pub fn send_with_client(self, client: &reqwest::blocking::Client) -> Result<()> {
138136
// Check if the report is empty
139137
if self.reports.is_empty() {
140138
return Ok(());
@@ -145,7 +143,7 @@ impl Session {
145143
let reports = self.compress_reports(reports);
146144

147145
for report in reports {
148-
self.upload(report)?;
146+
self.upload(report, client)?;
149147
}
150148

151149
Ok(())
@@ -194,16 +192,13 @@ impl Session {
194192
.collect()
195193
}
196194

197-
fn upload(&self, report: EncodedReport) -> Result<()> {
195+
fn upload(&self, report: EncodedReport, client: &reqwest::blocking::Client) -> Result<()> {
198196
log::info!(target: LOG_TAG, "Sending Session: {} - {}", self.from, self.until);
199197

200198
if report.data.is_empty() {
201199
return Ok(());
202200
}
203201

204-
//todo do not create a new client for every request
205-
let client = reqwest::blocking::Client::new();
206-
207202
let application_name = merge_tags_with_app_name(
208203
self.config.application_name.clone(),
209204
report.metadata.tags.clone().into_iter().collect(),

0 commit comments

Comments
 (0)