Skip to content

Commit 23bbebc

Browse files
mitsuhikojan-auer
authored andcommitted
feat: Upgrade to reqwest 0.9 (#89)
* feat: Upgrade to reqwest 0.9 * ref: Simplified retry-after handling * build: Upgrade dependencies * ci: Ignore the lint and format tasks * fix: Support floats for Retry-After and simplify logic
1 parent e03d61a commit 23bbebc

File tree

5 files changed

+40
-36
lines changed

5 files changed

+40
-36
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ matrix:
2929
env: SUITE=cargotestall
3030
- env: SUITE=travis-push-docs
3131

32+
allow_failures:
33+
- env: SUITE=format-check
34+
- env: SUITE=lint
35+
3236
notifications:
3337
webhooks:
3438
urls:

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ failure = { version = "0.1.2", optional = true }
3636
log = { version = "0.4.5", optional = true, features = ["std"] }
3737
sentry-types = "0.8.1"
3838
env_logger = { version = "0.5.13", optional = true }
39-
reqwest = { version = "0.8.8", optional = true }
39+
reqwest = { version = "0.9.2", optional = true }
4040
lazy_static = "1.1.0"
4141
regex = { version = "1.0.5", optional = true }
4242
error-chain = { version = "0.12.0", optional = true }
43-
im = { version = "12.0.0", optional = true }
43+
im = { version = "12.1.0", optional = true }
4444
libc = { version = "0.2.43", optional = true }
4545
hostname = { version = "0.1.5", optional = true }
4646
findshlibs = { version = "0.4.0", optional = true }
4747
rand = "0.5.5"
48+
httpdate = "0.3.2"
4849

4950
[target."cfg(not(windows))".dependencies]
5051
uname = { version = "0.1.1", optional = true }
@@ -55,7 +56,7 @@ rustc_version = { version = "0.2.3", optional = true }
5556
[dev-dependencies]
5657
failure_derive = "0.1.2"
5758
pretty_env_logger = "0.2.4"
58-
actix-web = { version = "0.7.7", default-features = false }
59+
actix-web = { version = "0.7.8", default-features = false }
5960

6061
[[example]]
6162
name = "error-chain-demo"

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ doc:
1515

1616
check-all-features:
1717
@echo 'ALL FEATURES'
18-
@RUSTFLAGS=-Dwarnings cargo check --all-features --all --examples
18+
@RUSTFLAGS=-Dwarnings cargo check --all-features --all
1919
.PHONY: check-all-features
2020

2121
check-no-default-features:
@@ -71,7 +71,7 @@ cargotest:
7171

7272
cargotestall:
7373
@echo 'TESTSUITE'
74-
@cargo test --all-features --all --examples
74+
@cargo test --all-features --all
7575
.PHONY: cargotest
7676

7777
test: checkall cargotestall

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ extern crate im;
119119
#[macro_use]
120120
extern crate lazy_static;
121121
#[cfg(feature = "with_client_implementation")]
122+
extern crate httpdate;
123+
#[cfg(feature = "with_client_implementation")]
122124
extern crate reqwest;
123125
extern crate sentry_types;
124126
#[cfg(feature = "with_client_implementation")]

src/transport.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use std::sync::atomic::{AtomicBool, Ordering};
22
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
33
use std::sync::{Arc, Condvar, Mutex};
44
use std::thread::{self, JoinHandle};
5-
use std::time::{Duration, Instant, SystemTime};
5+
use std::time::{Duration, SystemTime};
66

7-
use reqwest::header::{Headers, RetryAfter};
8-
use reqwest::{Client, Proxy, StatusCode};
7+
use httpdate::parse_http_date;
8+
use reqwest::header::RETRY_AFTER;
9+
use reqwest::{Client, Proxy};
910

1011
use api::protocol::Event;
1112
use client::ClientOptions;
@@ -109,6 +110,16 @@ pub struct HttpTransport {
109110
_handle: Option<JoinHandle<()>>,
110111
}
111112

113+
fn parse_retry_after(s: &str) -> Option<SystemTime> {
114+
if let Ok(value) = s.parse::<f64>() {
115+
Some(SystemTime::now() + Duration::from_secs(value.ceil() as u64))
116+
} else if let Ok(value) = parse_http_date(s) {
117+
Some(value)
118+
} else {
119+
None
120+
}
121+
}
122+
112123
fn spawn_http_sender(
113124
client: Client,
114125
receiver: Receiver<Option<Event<'static>>>,
@@ -118,7 +129,7 @@ fn spawn_http_sender(
118129
queue_size: Arc<Mutex<usize>>,
119130
user_agent: String,
120131
) -> JoinHandle<()> {
121-
let mut disabled: Option<(Instant, RetryAfter)> = None;
132+
let mut disabled = SystemTime::now();
122133

123134
thread::spawn(move || {
124135
let url = dsn.store_api_url().to_string();
@@ -133,39 +144,25 @@ fn spawn_http_sender(
133144
}
134145

135146
// while we are disabled due to rate limits, skip
136-
match disabled {
137-
Some((disabled_at, RetryAfter::Delay(disabled_for))) => {
138-
if disabled_at.elapsed() > disabled_for {
139-
disabled = None;
140-
} else {
141-
continue;
142-
}
143-
}
144-
Some((_, RetryAfter::DateTime(wait_until))) => {
145-
if SystemTime::from(wait_until) > SystemTime::now() {
146-
disabled = None;
147-
} else {
148-
continue;
149-
}
150-
}
151-
None => {}
147+
if disabled > SystemTime::now() {
148+
continue;
152149
}
153150

154-
let auth = dsn.to_auth(Some(&user_agent));
155-
let mut headers = Headers::new();
156-
headers.set_raw("X-Sentry-Auth", auth.to_string());
157-
158151
if let Ok(resp) = client
159152
.post(url.as_str())
160153
.json(&event)
161-
.headers(headers)
154+
.header("X-Sentry-Auth", dsn.to_auth(Some(&user_agent)).to_string())
162155
.send()
163156
{
164-
if resp.status() == StatusCode::TooManyRequests {
165-
disabled = resp
157+
if resp.status() == 429 {
158+
if let Some(retry_after) = resp
166159
.headers()
167-
.get::<RetryAfter>()
168-
.map(|x| (Instant::now(), *x));
160+
.get(RETRY_AFTER)
161+
.and_then(|x| x.to_str().ok())
162+
.and_then(parse_retry_after)
163+
{
164+
disabled = retry_after;
165+
}
169166
}
170167
}
171168

@@ -193,10 +190,10 @@ impl HttpTransport {
193190
let queue_size = Arc::new(Mutex::new(0));
194191
let mut client = Client::builder();
195192
if let Some(url) = http_proxy {
196-
client.proxy(Proxy::http(&url).unwrap());
193+
client = client.proxy(Proxy::http(&url).unwrap());
197194
};
198195
if let Some(url) = https_proxy {
199-
client.proxy(Proxy::https(&url).unwrap());
196+
client = client.proxy(Proxy::https(&url).unwrap());
200197
};
201198
let _handle = Some(spawn_http_sender(
202199
client.build().unwrap(),

0 commit comments

Comments
 (0)