binance / binance-spot-connector-rust

MIT License
109 stars 39 forks source link

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

Closed belakm closed 1 week ago

belakm commented 11 months 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 11 months ago

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

TilenRupar commented 11 months ago

Same problem here.

raduom commented 6 months ago

Same problem..

stupid-boar commented 4 months ago

same problem @belakm

JaydenElliott commented 2 months ago

same problem