Skip to content

Commit

Permalink
add drop context log
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Feb 5, 2025
1 parent 125012c commit 5f2707a
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 89 deletions.
5 changes: 2 additions & 3 deletions examples/rust/subscribe_candlesticks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_env_filter(EnvFilter::from_default_env())
.init();

let config =
Arc::new(Config::from_env()?.push_candlestick_mode(PushCandlestickMode::Confirmed));
let config = Arc::new(Config::from_env()?);
let (ctx, mut receiver) = QuoteContext::try_new(config).await?;
println!("member id: {}", ctx.member_id());
ctx.subscribe_candlesticks("600000.SH", Period::FiveMinute)
ctx.subscribe_candlesticks("700.HK", Period::OneMinute)
.await?;

while let Some(event) = receiver.recv().await {
Expand Down
9 changes: 6 additions & 3 deletions rust/crates/httpclient/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use serde::Deserialize;
use crate::{HttpClientConfig, HttpClientError, HttpClientResult, Json, RequestBuilder};

/// LongPort HTTP client
#[derive(Clone)]
pub struct HttpClient {
pub(crate) http_cli: Client,
pub(crate) config: Arc<HttpClientConfig>,
Expand Down Expand Up @@ -54,8 +53,12 @@ impl HttpClient {

/// Create a new request builder
#[inline]
pub fn request(&self, method: Method, path: impl Into<String>) -> RequestBuilder<(), (), ()> {
RequestBuilder::new(self.clone(), method, path)
pub fn request(
&self,
method: Method,
path: impl Into<String>,
) -> RequestBuilder<'_, (), (), ()> {
RequestBuilder::new(self, method, path)
}

/// Get the socket OTP(One Time Password)
Expand Down
18 changes: 9 additions & 9 deletions rust/crates/httpclient/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ struct OpenApiResponse {
}

/// A request builder
pub struct RequestBuilder<T, Q, R> {
client: HttpClient,
pub struct RequestBuilder<'a, T, Q, R> {
client: &'a HttpClient,
method: Method,
path: String,
headers: HeaderMap,
Expand All @@ -128,8 +128,8 @@ pub struct RequestBuilder<T, Q, R> {
mark_resp: PhantomData<R>,
}

impl RequestBuilder<(), (), ()> {
pub(crate) fn new(client: HttpClient, method: Method, path: impl Into<String>) -> Self {
impl<'a> RequestBuilder<'a, (), (), ()> {
pub(crate) fn new(client: &'a HttpClient, method: Method, path: impl Into<String>) -> Self {
Self {
client,
method,
Expand All @@ -142,10 +142,10 @@ impl RequestBuilder<(), (), ()> {
}
}

impl<T, Q, R> RequestBuilder<T, Q, R> {
impl<'a, T, Q, R> RequestBuilder<'a, T, Q, R> {
/// Set the request body
#[must_use]
pub fn body<T2>(self, body: T2) -> RequestBuilder<T2, Q, R>
pub fn body<T2>(self, body: T2) -> RequestBuilder<'a, T2, Q, R>
where
T2: ToPayload,
{
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<T, Q, R> RequestBuilder<T, Q, R> {

/// Set the query string
#[must_use]
pub fn query_params<Q2>(self, params: Q2) -> RequestBuilder<T, Q2, R>
pub fn query_params<Q2>(self, params: Q2) -> RequestBuilder<'a, T, Q2, R>
where
Q2: Serialize + Send + Sync,
{
Expand All @@ -194,7 +194,7 @@ impl<T, Q, R> RequestBuilder<T, Q, R> {

/// Set the response body type
#[must_use]
pub fn response<R2>(self) -> RequestBuilder<T, Q, R2>
pub fn response<R2>(self) -> RequestBuilder<'a, T, Q, R2>
where
R2: FromPayload,
{
Expand All @@ -210,7 +210,7 @@ impl<T, Q, R> RequestBuilder<T, Q, R> {
}
}

impl<T, Q, R> RequestBuilder<T, Q, R>
impl<T, Q, R> RequestBuilder<'_, T, Q, R>
where
T: ToPayload,
Q: Serialize + Send,
Expand Down
1 change: 0 additions & 1 deletion rust/crates/wsclient/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ impl WsSession {
}

/// LongPort Websocket client
#[derive(Clone)]
pub struct WsClient {
command_tx: mpsc::UnboundedSender<Command>,
rate_limit: Arc<HashMap<u8, RateLimiter>>,
Expand Down
11 changes: 11 additions & 0 deletions rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ impl Error {
}
}

/// Returns the OpenAPI error code
pub fn openapi_error_code(&self) -> Option<i64> {
match self {
Error::HttpClient(HttpClientError::OpenApi { code, .. }) => Some(*code as i64),
Error::WsClient(WsClientError::ResponseError { detail, .. }) => {
detail.as_ref().map(|detail| detail.code as i64)
}
_ => None,
}
}

/// Consumes this error and returns a simple error
pub fn into_simple_error(self) -> SimpleError {
match self {
Expand Down
9 changes: 3 additions & 6 deletions rust/src/quote/cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::HashMap,
hash::Hash,
sync::Arc,
time::{Duration, Instant},
};

Expand All @@ -18,9 +17,8 @@ struct Inner<K, V> {
values: HashMap<K, Item<V>>,
}

#[derive(Clone)]
pub(crate) struct CacheWithKey<K, V> {
inner: Arc<Mutex<Inner<K, V>>>,
inner: Mutex<Inner<K, V>>,
}

impl<K, V> CacheWithKey<K, V>
Expand All @@ -30,10 +28,10 @@ where
{
pub(crate) fn new(timeout: Duration) -> Self {
CacheWithKey {
inner: Arc::new(Mutex::new(Inner {
inner: Mutex::new(Inner {
timeout,
values: HashMap::new(),
})),
}),
}
}

Expand Down Expand Up @@ -61,7 +59,6 @@ where
}
}

#[derive(Clone)]
pub(crate) struct Cache<V> {
inner: CacheWithKey<(), V>,
}
Expand Down
Loading

0 comments on commit 5f2707a

Please sign in to comment.