Skip to content

Commit 146c333

Browse files
committed
refactor(session): remove thread and add timeout to Session.send
1 parent 90abfac commit 146c333

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/session.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub struct Session {
8989

9090
impl Session {
9191
/// Create a new Session
92-
/// # Example
92+
/// # Example
9393
/// ```ignore
9494
/// let config = PyroscopeConfig::new("https://localhost:8080", "my-app");
9595
/// let report = vec![1, 2, 3];
@@ -131,39 +131,38 @@ impl Session {
131131
pub fn send(self) -> Result<()> {
132132
log::info!("Session - Sending Session");
133133

134-
let _handle: JoinHandle<Result<()>> = thread::spawn(move || {
135-
if self.report.is_empty() {
136-
return Ok(());
137-
}
138-
139-
let client = reqwest::blocking::Client::new();
140-
// TODO: handle the error of this request
141-
142-
// Clone URL
143-
let url = self.config.url.clone();
144-
145-
// Merge application name with Tags
146-
let application_name = merge_tags_with_app_name(
147-
self.config.application_name.clone(),
148-
self.config.tags.clone(),
149-
)?;
150-
151-
client
152-
.post(format!("{}/ingest", url))
153-
.header("Content-Type", "binary/octet-stream")
154-
.query(&[
155-
("name", application_name.as_str()),
156-
("from", &format!("{}", self.from)),
157-
("until", &format!("{}", self.until)),
158-
("format", "folded"),
159-
("sampleRate", &format!("{}", self.config.sample_rate)),
160-
("spyName", "pprof-rs"),
161-
])
162-
.body(self.report)
163-
.send()?;
164-
134+
// Check if the report is empty
135+
if self.report.is_empty() {
165136
return Ok(());
166-
});
137+
}
138+
139+
// Create a new client
140+
let client = reqwest::blocking::Client::new();
141+
142+
// Clone URL
143+
let url = self.config.url.clone();
144+
145+
// Merge application name with Tags
146+
let application_name = merge_tags_with_app_name(
147+
self.config.application_name.clone(),
148+
self.config.tags.clone(),
149+
)?;
150+
151+
// Create and send the request
152+
client
153+
.post(format!("{}/ingest", url))
154+
.header("Content-Type", "binary/octet-stream")
155+
.query(&[
156+
("name", application_name.as_str()),
157+
("from", &format!("{}", self.from)),
158+
("until", &format!("{}", self.until)),
159+
("format", "folded"),
160+
("sampleRate", &format!("{}", self.config.sample_rate)),
161+
("spyName", "pyroscope-rs"),
162+
])
163+
.body(self.report)
164+
.timeout(std::time::Duration::from_secs(10))
165+
.send()?;
167166

168167
Ok(())
169168
}

0 commit comments

Comments
 (0)