Skip to content

Commit

Permalink
Merge pull request #44 from ainestal/fix_block_or_continue
Browse files Browse the repository at this point in the history
Fix block or continue
  • Loading branch information
hugues31 authored Aug 15, 2017
2 parents 5b803a4 + 0c55c4b commit 97ae693
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
23 changes: 18 additions & 5 deletions src/bitstamp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub fn get_pair_enum(pair: &str) -> Option<&Pair> {
}

pub fn block_or_continue(last_request: i64) {
let threshold = 1000; // 600 requests per 10 mins = 1 request per second
let delay = helpers::get_unix_timestamp_ms() - last_request;
if delay < threshold {
let duration_ms = Duration::from_millis(delay as u64);
thread::sleep(duration_ms);
let threshold: u64 = 1000; // 600 requests per 10 mins = 1 request per second
let offset: u64 = helpers::get_unix_timestamp_ms() as u64 - last_request as u64;
if offset < threshold {
let wait_ms = Duration::from_millis(threshold - offset);
thread::sleep(wait_ms);
}
}

Expand Down Expand Up @@ -160,3 +160,16 @@ pub fn get_currency_string(currency: Currency) -> Option<String> {
_ => None,
}
}

#[cfg(test)]
mod utils_tests {
use super::*;

#[test]
fn should_block_when_enabled() {
let start = helpers::get_unix_timestamp_ms();
block_or_continue(start);
let end = helpers::get_unix_timestamp_ms();
assert!(end - start >= 1000)
}
}
12 changes: 6 additions & 6 deletions src/kraken/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ impl KrakenApi {
self.otp = Some(otp);
}

fn block_or_continue(&self) {
let threshold = 2000; // 1 request/2sec
let delay = helpers::get_unix_timestamp_ms() - self.last_request;
if delay < threshold {
let duration_ms = Duration::from_millis(delay as u64);
thread::sleep(duration_ms);
pub fn block_or_continue(&self) {
let threshold: u64 = 2000; // 1 request/2sec
let offset: u64 = helpers::get_unix_timestamp_ms() as u64 - self.last_request as u64;
if offset < threshold {
let wait_ms = Duration::from_millis(threshold - offset);
thread::sleep(wait_ms);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/poloniex/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ impl PoloniexApi {
}

fn block_or_continue(&self) {
let threshold = 167; // 6 requests/sec = 1/6*1000
let delay = helpers::get_unix_timestamp_ms() - self.last_request;
if delay < threshold {
let duration_ms = Duration::from_millis(delay as u64);
thread::sleep(duration_ms);
let threshold: u64 = 167; // 6 requests/sec = 1/6*1000
let offset: u64 = helpers::get_unix_timestamp_ms() as u64 - self.last_request as u64;
if offset < threshold {
let wait_ms = Duration::from_millis(threshold - offset);
thread::sleep(wait_ms);
}
}

Expand Down

0 comments on commit 97ae693

Please sign in to comment.