Giter Club home page Giter Club logo

Comments (5)

algesten avatar algesten commented on August 30, 2024

Hi @John-Nagle!

ureq does use set_nonblocking() temporarily when it wants to check that a pooled connection to a server is still fresh. The way it does it is:

  1. set_nonblocking(true)
  2. Try read.
  3. set_nonblocking(false)

The idea is that in the happy path, the read should result in a EWOULDBLOCK indicating the connection is still open and can be used. A closed connection would give us another error, and if we manage to read any bytes, the server sent stuff we didn't expect (meaning it's best to close since otherwise we risk request smuggling etc).

I wonder if what you observe maybe has something to do with this?

from ureq.

John-Nagle avatar John-Nagle commented on August 30, 2024

Possibly. Ureq can usefully use nonblocking I/O at some points. But status EWOULDBLOCK should never make it out to the caller.

For now, I turned on a retry loop which, on any error, waits 1 second and tries the connection again, for 5 tries. That will probably work around intermittent errors.

from ureq.

jsha avatar jsha commented on August 30, 2024

from ureq.

John-Nagle avatar John-Nagle commented on August 30, 2024
pub fn fetch_capabilities(
    client: &HttpClient,
    cap_url: &str,
    keys: &[&str],
) -> Result<HashMap<String, String>, Error> {
    const RETRIES: usize = 0; // try this many times
                              
    //  Construct request in LLSD format
    let query = compose_query(keys).with_context(|| {
        format!(
            "Composing query for capabilities {:?} from {}",
            keys, cap_url
        )
    })?;
    //  Retry query. Especially on Open Simulator, tries can fail.
    //  Documentation says that firewall/CDN problems can cause transient 5xx errors.
    let mut try_count = 0;
    let reply = loop {
        log::info!("Capability query to {}: {}", cap_url, query);
        //  Send HTTP request and block.
        let http_result = client
            .agent
            .post(cap_url)
            ////.set("Content-Type", "text/xml") // "text/xml; charset=utf-8" is rejected by SL server
            .set("Content-Type", "application/llsd+xml") // "text/xml; charset=utf-8" is rejected by SL server
            .send_string(&query)
            .map_err(anyhow::Error::msg)
            .with_context(|| format!("Fetching capabilities {:?} from {}", keys, cap_url));
        //  Analyze result
        match http_result {
            Ok(reply) => break reply, // good
            Err(e) => {
                log::error!(
                    "Error reading capability {}, try {}: {:?}",
                    cap_url,
                    try_count,
                    e
                );
                try_count += 1;
                if try_count > RETRIES {
                    return Err(e);
                }
                std::thread::sleep(std::time::Duration::from_millis(1000)); // wait before retry
            }
        }
    };
   ...

This was running with RETRIES=0, so any failure was fatal. It's now running with RETRIES=5.

This is deep inside a metaverse client for Second Life/Open Simulator. The servers provide a URL which is queried for some internal data. This particular code is executed a few times per minute, maximum. As the player moves around, this is part of the system which attaches new regions. So it's not heavy traffic. The server side is 20 year old code, and may be misbehaving. The client code above has been unchanged for the last year.

from ureq.

algesten avatar algesten commented on August 30, 2024

Closing since we're moving to ureq 3.x. The behavior is changing.

from ureq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.