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

If agent fails, try again without #1654

Closed
Tracked by #1684
joergd opened this issue Mar 5, 2021 · 6 comments
Closed
Tracked by #1684

If agent fails, try again without #1654

joergd opened this issue Mar 5, 2021 · 6 comments
Labels
enhancement This change will extend Got features ✭ help wanted ✭

Comments

@joergd
Copy link

joergd commented Mar 5, 2021

If I start up a stream request with an agent (proxy), and that request fails (if say the proxy is unavailable), is there an easy way to fallback to making the call without the agent?

@szmarczak
Copy link
Collaborator

It should be possible to remove the agent option in the beforeRetry hooks.

@szmarczak
Copy link
Collaborator

szmarczak commented Mar 5, 2021

Let's keep this issue open. This needs a test.

@szmarczak szmarczak added enhancement This change will extend Got features ✭ help wanted ✭ labels Mar 5, 2021
@joergd
Copy link
Author

joergd commented Mar 5, 2021

I can't use that hook, as I am streaming ...

@szmarczak
Copy link
Collaborator

In that case - yes, it's possible: https://github.com/sindresorhus/got#retry

@joergd
Copy link
Author

joergd commented Mar 5, 2021

Yeah I looked at that - but I couldn't get that to work. Just to confirm with that sample code, the writeStream is the stream that represents the actual response that I can stream further along in my pipeline?

@szmarczak szmarczak mentioned this issue Mar 21, 2021
71 tasks
@szmarczak
Copy link
Collaborator

This is still possible with Got v11.8.2:

const https = require('https');
const got = require('got');

class MyAgent extends https.Agent {
    createConnection(port, options, callback) {
        console.log(`Connecting with MyAgent`);
        return https.Agent.prototype.createConnection.call(this, port, options, callback);
    }
}

const fn = retryCount => {
    const stream = got.stream('https://httpbin.org/status/408', {
        agent: {
            https: retryCount === 0 ? new MyAgent() : undefined
        }
    });
    
    stream.retryCount = retryCount;

    stream.on('retry', (retryCount, error) => {
        console.log('Creating new retry:', retryCount);
        fn(retryCount);
    }).on('error', error => {
        console.log('Retry count:', error.request.retryCount);
    }).resume().on('end', () => {
        console.log('`end` event');
    });
};

fn(0);

@szmarczak szmarczak mentioned this issue Apr 11, 2021
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features ✭ help wanted ✭
Projects
None yet
Development

No branches or pull requests

2 participants