Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async example doesn't work at all #16

Closed
autarchprinceps opened this issue Aug 7, 2020 · 5 comments
Closed

Async example doesn't work at all #16

autarchprinceps opened this issue Aug 7, 2020 · 5 comments

Comments

@autarchprinceps
Copy link

autarchprinceps commented Aug 7, 2020

I tried to apply the async backoff example to my code, but kept running into errors, so I just tried to compile the raw example, but it fails with exactly the same errors.
This code:

use backoff::{future::FutureOperation as _, ExponentialBackoff};

async fn fetch_url(url: &str) -> Result<String, reqwest::Error> {
  (|| async {
    println!("Fetching {}", url);
    Ok(reqwest::get(url).await?.text().await?)
  })
    .retry(ExponentialBackoff::default())
    .await
}

#[tokio::main]
async fn main() {
  match fetch_url("https://www.rust-lang.org").await {
    Ok(_) => println!("Successfully fetched"),
    Err(err) => panic!("Failed to fetch: {}", err),
  }
}

causes these errors:

error[E0432]: unresolved import `backoff::future`
 --> src/main.rs:1:15
  |
1 | use backoff::{future::FutureOperation as _, ExponentialBackoff};
  |               ^^^^^^ could not find `future` in `backoff`

error[E0599]: no method named `retry` found for closure `[closure@src/main.rs:4:3: 7:5 url:_]` in the current scope
 --> src/main.rs:8:6
  |
8 |     .retry(ExponentialBackoff::default())
  |      ^^^^^ method not found in `[closure@src/main.rs:4:3: 7:5 url:_]`
  |
  = note: `(|| async {
              println!("Fetching {}", url);
              Ok(reqwest::get(url).await?.text().await?)
            })` is a function, perhaps you wish to call it

with a cargo toml dependency including backoff = ">=0.2.1"

@tobymurray
Copy link

tobymurray commented Aug 9, 2020

Did you try adding the async-std feature? e.g. backoff = { version = "0.2.1", features = ["async-std"] }? Related to #14 I think.

I'm not sure how a user is supposed to discover the async-std feature even exists, or that it's needed - perhaps missing documentation? I found it by looking at the source after running into the same issue as you.

@autarchprinceps
Copy link
Author

The feature sync-std does fix the default sample. This should definitively be documented.
As for my real code, it still fails. From what this tells me, I assume this means it doesn't like the error type, but I'm not sure. The actual call I'm making, is to rusoto, which has it's own error types, that do work with Result and match, but apparently not with backoff.

299 |   match (|| async {
    |         ---------
    |         |
    |         doesn't satisfy `<_ as std::ops::FnOnce<()>>::Output = std::result::Result<_, backoff::error::Error<_>>`
    |         doesn't satisfy `_: backoff::retry::Operation<_, _>`
300 |     client.list_stacks(list_stacks_input.clone()).await
301 |   }).retry(ExponentialBackoff::default()).await {
    |      ^^^^^ method not found in `[closure@src/main.rs:299:9: 301:5 client:_, list_stacks_input:_]`

@sander2
Copy link

sander2 commented Oct 16, 2020

299 |   match (|| async {
    |         ---------
    |         |
    |         doesn't satisfy `<_ as std::ops::FnOnce<()>>::Output = std::result::Result<_, backoff::error::Error<_>>`
    |         doesn't satisfy `_: backoff::retry::Operation<_, _>`
300 |     client.list_stacks(list_stacks_input.clone()).await
301 |   }).retry(ExponentialBackoff::default()).await {
    |      ^^^^^ method not found in `[closure@src/main.rs:299:9: 301:5 client:_, list_stacks_input:_]`

I think you need to convert the Error type. Try Ok(client.list_stacks(list_stacks_input.clone()).await?), or use map_err(|e| e.into())

@ihrwein
Copy link
Owner

ihrwein commented Jan 16, 2021

@autarchprinceps I'm sorry for taking so long to reply to this issue. Did you manage to solve it eventually?

I agree with @sander2's suggestion above, .map_err(backoff::Error::Transient) might solve your issue.

You can find an example here which uses the latest master, reqwest anyhow: https://github.com/ihrwein/backoff-issue-22/blob/main/src/lib.rs

@ihrwein
Copy link
Owner

ihrwein commented Jan 31, 2021

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants