Skip to content

Commit 8af14ea

Browse files
mixalturekmitsuhiko
authored andcommitted
feat: Added ability to share http clients with Sentry through transport config. (#131)
1 parent 7c86638 commit 8af14ea

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/transport.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ macro_rules! implement_http_transport {
127127
$(#[$attr:meta])*
128128
pub struct $typename:ident;
129129
fn spawn($($argname:ident: $argty:ty,)*) $body:block
130+
fn http_client($hc_options:ident: &ClientOptions, $hc_client:ident: Option<$hc_client_ty:ty>) -> $hc_ret:ty $hc_body:block
130131
) => {
131132
$(#[$attr])*
132133
pub struct $typename {
@@ -139,20 +140,34 @@ macro_rules! implement_http_transport {
139140

140141
impl $typename {
141142
/// Creates a new transport.
142-
pub fn new(options: &ClientOptions) -> $typename {
143+
pub fn new(options: &ClientOptions) -> Self {
144+
Self::new_internal(options, None)
145+
}
146+
147+
/// Creates a new transport that uses the passed HTTP client.
148+
pub fn with_client(options: &ClientOptions, $hc_client: $hc_client_ty) -> Self {
149+
Self::new_internal(options, Some($hc_client))
150+
}
151+
152+
/// Creates a new transport that uses the passed HTTP client or builds a new one.
153+
fn new_internal(options: &ClientOptions, $hc_client: Option<$hc_client_ty>) -> Self {
143154
fn spawn($($argname: $argty,)*) -> JoinHandle<()> { $body }
144155

156+
fn http_client($hc_options: &ClientOptions, $hc_client: Option<$hc_client_ty>) -> $hc_ret { $hc_body }
157+
145158
let (sender, receiver) = sync_channel(30);
146159
let shutdown_signal = Arc::new(Condvar::new());
147160
let shutdown_immediately = Arc::new(AtomicBool::new(false));
148161
#[allow(clippy::mutex_atomic)]
149162
let queue_size = Arc::new(Mutex::new(0));
163+
let http_client = http_client(options, $hc_client);
150164
let _handle = Some(spawn(
151165
options,
152166
receiver,
153167
shutdown_signal.clone(),
154168
shutdown_immediately.clone(),
155169
queue_size.clone(),
170+
http_client,
156171
));
157172
$typename {
158173
sender: Mutex::new(sender),
@@ -216,19 +231,10 @@ implement_http_transport! {
216231
signal: Arc<Condvar>,
217232
shutdown_immediately: Arc<AtomicBool>,
218233
queue_size: Arc<Mutex<usize>>,
234+
http_client: Client,
219235
) {
220236
let dsn = options.dsn.clone().unwrap();
221237
let user_agent = options.user_agent.to_string();
222-
let http_proxy = options.http_proxy.as_ref().map(|x| x.to_string());
223-
let https_proxy = options.https_proxy.as_ref().map(|x| x.to_string());
224-
let mut client = Client::builder();
225-
if let Some(url) = http_proxy {
226-
client = client.proxy(Proxy::http(&url).unwrap());
227-
};
228-
if let Some(url) = https_proxy {
229-
client = client.proxy(Proxy::https(&url).unwrap());
230-
};
231-
let client = client.build().unwrap();
232238

233239
let mut disabled = None::<SystemTime>;
234240

@@ -260,7 +266,7 @@ implement_http_transport! {
260266
}
261267
}
262268

263-
match client
269+
match http_client
264270
.post(url.as_str())
265271
.json(&event)
266272
.header("X-Sentry-Auth", dsn.to_auth(Some(&user_agent)).to_string())
@@ -291,6 +297,21 @@ implement_http_transport! {
291297
}
292298
}).unwrap()
293299
}
300+
301+
fn http_client(options: &ClientOptions, client: Option<Client>) -> Client {
302+
client.unwrap_or_else(|| {
303+
let http_proxy = options.http_proxy.as_ref().map(|x| x.to_string());
304+
let https_proxy = options.https_proxy.as_ref().map(|x| x.to_string());
305+
let mut client = Client::builder();
306+
if let Some(url) = http_proxy {
307+
client = client.proxy(Proxy::http(&url).unwrap());
308+
};
309+
if let Some(url) = https_proxy {
310+
client = client.proxy(Proxy::https(&url).unwrap());
311+
};
312+
client.build().unwrap()
313+
})
314+
}
294315
}
295316

296317
#[cfg(feature = "with_curl_transport")]
@@ -306,14 +327,15 @@ implement_http_transport! {
306327
signal: Arc<Condvar>,
307328
shutdown_immediately: Arc<AtomicBool>,
308329
queue_size: Arc<Mutex<usize>>,
330+
http_client: curl::easy::Easy,
309331
) {
310332
let dsn = options.dsn.clone().unwrap();
311333
let user_agent = options.user_agent.to_string();
312334
let http_proxy = options.http_proxy.as_ref().map(|x| x.to_string());
313335
let https_proxy = options.https_proxy.as_ref().map(|x| x.to_string());
314336

315337
let mut disabled = None::<SystemTime>;
316-
let mut handle = curl::easy::Easy::new();
338+
let mut handle = http_client;
317339

318340
thread::spawn(move || {
319341
sentry_debug!("spawning curl transport");
@@ -417,6 +439,10 @@ implement_http_transport! {
417439
}
418440
})
419441
}
442+
443+
fn http_client(_options: &ClientOptions, client: Option<curl::easy::Easy>) -> curl::easy::Easy {
444+
client.unwrap_or_else(curl::easy::Easy::new)
445+
}
420446
}
421447

422448
#[cfg(feature = "with_reqwest_transport")]

0 commit comments

Comments
 (0)