Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
warittornc committed Feb 27, 2024
1 parent 1f11bb4 commit 9ba2a43
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
4 changes: 2 additions & 2 deletions bothan-binance/examples/dummy_example.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use tracing_subscriber::fmt::init;

use bothan_binance::service::BinanceServiceBuilder;
use bothan_binance::BinanceServiceBuilder;
use bothan_core::service::Service;

#[tokio::main]
async fn main() {
init();

let service = BinanceServiceBuilder::new().build().await;
let service = BinanceServiceBuilder::default().build().await;
if let Ok(mut service) = service {
loop {
let data = service.get_price_data(&["btcusdt", "ethusdt"]).await;
Expand Down
48 changes: 48 additions & 0 deletions bothan-binance/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
use std::sync::Arc;

use tokio::sync::Mutex;

use crate::api::types::DEFAULT_URL;
use crate::error::Error;
use crate::types::DEFAULT_CHANNEL_SIZE;
use crate::{BinanceService, BinanceWebSocketConnector};

pub struct BinanceServiceBuilder {
url: String,
cmd_ch_size: usize,
rem_id_ch_size: usize,
}

impl BinanceServiceBuilder {
pub fn with_cmd_ch_size(mut self, size: usize) -> Self {
self.cmd_ch_size = size;
self
}

pub fn with_rem_id_ch_size(mut self, size: usize) -> Self {
self.rem_id_ch_size = size;
self
}

pub async fn build(self) -> Result<BinanceService, Error> {
let connector = BinanceWebSocketConnector::new(self.url);
let connection = connector.connect().await?;
let service = BinanceService::new(
Arc::new(connector),
Arc::new(Mutex::new(connection)),
self.cmd_ch_size,
self.rem_id_ch_size,
)
.await;
Ok(service)
}
}

impl Default for BinanceServiceBuilder {
fn default() -> Self {
Self {
url: DEFAULT_URL.to_string(),
cmd_ch_size: DEFAULT_CHANNEL_SIZE,
rem_id_ch_size: DEFAULT_CHANNEL_SIZE,
}
}
}
1 change: 1 addition & 0 deletions bothan-binance/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use api::websocket::{BinanceWebSocketConnection, BinanceWebSocketConnector};
pub use builder::BinanceServiceBuilder;
pub use service::BinanceService;

pub mod api;
Expand Down
48 changes: 4 additions & 44 deletions bothan-binance/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use tokio::select;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::sync::{Mutex, MutexGuard};
use tokio::sync::Mutex;
use tokio::time::timeout;
use tracing::{debug, error, info, warn};

Expand All @@ -11,57 +11,17 @@ use bothan_core::service::{Error as ServiceError, Service, ServiceResult};
use bothan_core::types::PriceData;

use crate::api::error::Error as BinanceError;
use crate::api::types::{BinanceResponse, Data, DEFAULT_URL};
use crate::api::types::{BinanceResponse, Data};
use crate::api::websocket::{BinanceWebSocketConnection, BinanceWebSocketConnector};
use crate::error::Error;
use crate::types::{Command, DEFAULT_CHANNEL_SIZE, DEFAULT_TIMEOUT};

pub struct BinanceServiceBuilder {
url: String,
cmd_ch_size: usize,
rem_id_ch_size: usize,
}

impl BinanceServiceBuilder {
pub fn new() -> Self {
Self {
url: DEFAULT_URL.to_string(),
cmd_ch_size: DEFAULT_CHANNEL_SIZE,
rem_id_ch_size: DEFAULT_CHANNEL_SIZE,
}
}

pub fn with_cmd_ch_size(mut self, size: usize) -> Self {
self.cmd_ch_size = size;
self
}

pub fn with_rem_id_ch_size(mut self, size: usize) -> Self {
self.rem_id_ch_size = size;
self
}

pub async fn build(self) -> Result<BinanceService, Error> {
let connector = BinanceWebSocketConnector::new(self.url);
let connection = connector.connect().await?;
let service = BinanceService::new(
Arc::new(connector),
Arc::new(Mutex::new(connection)),
self.cmd_ch_size,
self.rem_id_ch_size,
)
.await;
Ok(service)
}
}
use crate::types::{Command, DEFAULT_TIMEOUT};

pub struct BinanceService {
cache: Arc<Cache<PriceData>>,
cmd_tx: Arc<Sender<Command>>,
}

impl BinanceService {
async fn new(
pub(crate) async fn new(
connector: Arc<BinanceWebSocketConnector>,
connection: Arc<Mutex<BinanceWebSocketConnection>>,
cmd_ch_size: usize,
Expand Down

0 comments on commit 9ba2a43

Please sign in to comment.