aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Edgecumbe <git@esotericnonsense.com>2019-10-27 19:24:42 +0100
committerDaniel Edgecumbe <git@esotericnonsense.com>2019-10-27 19:25:12 +0100
commit31e4578f52f9cc6d12d4c371973d2868ace52116 (patch)
tree223b501e3982801b2031621dcf769186aef3037c
parent4e6795a34cf2e00d45bb75a62363cc1f05165e7f (diff)
Wrap the BFClient in an Arc; embryonic keep-alive thread (not yet
working) This allows for the client to be cloned and passed to multiple threads whilst maintaining automatic login semantics, etc. The keep-alive thread is there in principle along with an automatic destructor however the logic itself is yet to be implemented.
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/client.rs44
3 files changed, 40 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 87c5a39..28c85e0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -61,7 +61,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "botfair"
-version = "0.2.1"
+version = "0.2.99"
dependencies = [
"chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index d3b60e5..ce8eac0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "botfair"
-version = "0.2.1"
+version = "0.2.99"
description = "rust bindings for Betfair's SportsAPING"
keywords = ["betfair", "jsonrpc", "aping", "sports", "betting"]
repository = "https://git.esotericnonsense.com/pub/botfair.git"
diff --git a/src/client.rs b/src/client.rs
index 4b1dff3..398b01d 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -19,7 +19,9 @@ use crate::result::{Error, Result};
use reqwest::{Client, Identity};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
-use std::sync::{Arc, RwLock};
+use std::sync::{mpsc, Arc, RwLock};
+use std::thread;
+use std::time::Duration;
#[derive(Debug, Serialize)]
struct LoginRequestForm {
@@ -75,16 +77,26 @@ impl BFCredentials {
/// Betfair APING.
pub struct BFClient {
client: reqwest::Client,
- session_token: Arc<RwLock<Option<String>>>,
+ destructor: mpsc::SyncSender<()>,
+ session_token: RwLock<Option<String>>,
creds: BFCredentials,
proxy_uri: Option<String>,
}
+impl Drop for BFClient {
+ fn drop(&mut self) {
+ info!("dropping BFClient!");
+ self.destructor
+ .send(())
+ .expect("unable to signal keepalive thread");
+ }
+}
+
impl BFClient {
pub fn new(
creds: BFCredentials,
proxy_uri: Option<String>,
- ) -> Result<Self> {
+ ) -> Result<Arc<Self>> {
let client: reqwest::Client = match &proxy_uri {
Some(uri) => {
let proxy = reqwest::Proxy::all(uri)?;
@@ -92,12 +104,30 @@ impl BFClient {
}
None => reqwest::Client::new(),
};
- Ok(BFClient {
+
+ let session_token = RwLock::new(None);
+ let (destructor, rx) = mpsc::sync_channel(0); // rendezvous channel
+ thread::spawn(move || {
+ info!("New keepalive thread spawned for BFClient");
+ loop {
+ match rx.recv_timeout(Duration::from_millis(1000)) {
+ Ok(_) => {
+ warn!("got destructor signal, thread finishing");
+ break;
+ }
+ Err(_) => {
+ info!("thread still running");
+ }
+ }
+ }
+ });
+ Ok(Arc::new(BFClient {
client,
- session_token: Arc::new(RwLock::new(None)),
+ destructor,
+ session_token,
creds,
proxy_uri,
- })
+ }))
}
// TODO keepalive
@@ -147,6 +177,8 @@ impl BFClient {
const JSONRPC_URI: &str =
"https://api.betfair.com/exchange/betting/json-rpc/v1";
+ trace!("Performing a query to the JSON-RPC api");
+
Ok(self
.client
.post(JSONRPC_URI)