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

Question: redirects and timeouts? #271

Closed
persona0591 opened this issue Jan 12, 2020 · 3 comments
Closed

Question: redirects and timeouts? #271

persona0591 opened this issue Jan 12, 2020 · 3 comments
Labels
support Questions, discussions, and general support

Comments

@persona0591
Copy link

Support plan

  • which support plan is this issue covered by? Community
  • is this issue currently blocking your project? yes
  • is this issue affecting a production system? yes

Context

  • node version: 12
  • module version: 17.0.0
  • environment (e.g. node, browser, native): Node
  • used with: standalone
  • any other relevant information: -

How can we help?

Hi all!

I use Wreck - with great joy - as my HTTP client for connecting to a third-party API. This API, however, uses 302 redirects and can also be quite slow. I've managed to configure Wreck to follow redirects. I also use a timeout in case the API takes too much time and I want to stop the request.

However, I don't fully understand how Wreck's redirects and timeouts work together. I want my request to fail if 5 redirects have occurred, or if 15 seconds have passed. I'm now confronted with a situation in which the initial request and 2 additional redirects occur in about 15 seconds. Wreck then rightfully throws a 504 error ("Client request timeout"). But after this, 3 more requests take place, probably because I've configured Wreck to make 5 redirects. I'd like the request as a whole to fail as soon as a timeout occurs, disregarding any remaining redirects. Is this possible?

My test code:

'use strict';

const Http = require('http');
const Wreck = require('@hapi/wreck');

const run = async () => {

    const handler = (request, response) => {

        response.writeHead(302, { 'Location': '/' });
        setTimeout(() => response.end(), 5000); // Simulate slow response
    };

    const getServer = function (handler) {

        const server = Http.createServer(handler);

        return new Promise((resolve) => {

            server.listen(0, () => resolve(server));
        });
    };

    const server = await getServer(handler);

    const client = Wreck.defaults({
        timeout: 15000,
        redirects: 5,
        beforeRedirect: (redirectMethod, statusCode, location, resHeaders, redirectOptions, next) => {

            console.log('beforeRedirect: ' + location);
            return next();
        }
    });

    await client.request('head', 'http://localhost:' + server.address().port);
};

(async () => {

    try {
        await run();
    }
    catch (err) {
        console.log(err);
    }
})();

This code logs the following steps:

  1. beforeRedirect: http://localhost:{somePort}/
  2. beforeRedirect: http://localhost:{somePort}/
  3. Error: Client request timeout
  4. beforeRedirect: http://localhost:{somePort}/
  5. beforeRedirect: http://localhost:{somePort}/
  6. beforeRedirect: http://localhost:{somePort}/

However, I would like the code to stop after step 3.

Help is much appreciated! :)

@persona0591 persona0591 added the support Questions, discussions, and general support label Jan 12, 2020
@kanongil
Copy link
Contributor

This sounds like a bug in wreck. It should not perform additional retry requests after a timeout has been reported.

@persona0591
Copy link
Author

Thanks Gil - much appreciated!

@lloydbenson
Copy link
Contributor

thx @kanongil !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

3 participants