binance / binance-spot-connector-rust

Simple Rust connector to Binance API
MIT License
136 stars 43 forks source link

Enables binance_tokio_client to be used within async context and Tokio::Spawn runtime #3

Closed belakm closed 4 months ago

belakm commented 1 year ago

This PR enables binance_spot_connector with Hyper client to be used within async contexts and Tokio runtime.

Problem

Using hyper client within tokio::spawn throws an error.

tokio::spawn(async move {
  // ...
  //  Doing something with hyper client
  // ...
})

The problem lies with url::form_urlencoded::Serializer which is not Send and there for cannot be carried across an await.

Using it within Tokio context throws an error on line /src/hyper/client.rs:117:13:

let response = self
            .client
            .request(request)
            .await // <--------------------------- Throws here
            .map_err(|err| Error::Send(err))?;

// ...
    = help: the trait `Sync` is not implemented for `dyn for<'a> Fn(&'a s
note: future is not `Send` as this value is used across an await
   --> /binance_spot_connector_rust-1.0.1/src/hyper/client.rs:117:13
    |
68  |         let mut serializer = url::form_urlencoded::Serializer::n...
    |             -------------- has type `form_urlencoded::Serializer<'_, std::string::String>` which is not `Send`
...
117 |             .await
    |             ^^^^^^ await occurs here, with `mut serializer` maybe used later
...
122 |     }
    |     - `mut serializer` is later dropped here
note: required by a bound in `tokio::spawn`

Solution

The fix is to put the serializer in a block so it drops before it could cross an await and it works like a charm :)

belakm commented 1 year ago

@2pd @aisling-2 could someone take a peak?

TilenRupar commented 1 year ago

Same problem here.

raduom commented 10 months ago

Same problem..

stupid-boar commented 8 months ago

same problem @belakm

JaydenElliott commented 6 months ago

same problem