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

Make calculateDelay promisable #1266

Merged
merged 10 commits into from May 16, 2020
Merged

Make calculateDelay promisable #1266

merged 10 commits into from May 16, 2020

Conversation

Giotino
Copy link
Collaborator

@Giotino Giotino commented May 14, 2020

Checklist

  • I have read the documentation.
  • I have included some tests.
  • If it's a new feature, I have included documentation updates.

Fixes #1262

@Giotino Giotino marked this pull request as draft May 14, 2020 17:05
@szmarczak
Copy link
Collaborator

Excellent! This just needs tests :)

Also Node.js 14.2.0 is failing somehow:

  Uncaught exception in test/retry.ts
  RequestError: Cannot call end after a stream was destroyed
  › PromisableRequest._destroy (dist/source/core/index.js:96:618)
  › PromisableRequest.destroy (internal/streams/destroy.js:36:8)
  › errorOrDestroy (internal/streams/destroy.js:146:12)
  › dist/source/core/index.js:94:579
  › endRequest (dist/source/core/index.js:94:221)
  › PromisableRequest._final (dist/source/core/index.js:94:706)

@Giotino
Copy link
Collaborator Author

Giotino commented May 14, 2020

That issue is less trivial than it seems.
It's a problem with the request destruction.

https://github.com/sindresorhus/got/blob/master/source/as-promise/index.ts#L151,L224

request.once('error', async (error: RequestError) => {
  // Some slow logic
  request.destroy();
});

After the error event, the request fire the response event, but this is prevented by request.destroy().
The problem in my PR is that being the error listener async, the emitter sees it as immediately completed and goes on with the response event because request.destroy() has not been called yet.

While the previous commit make some sense (a86d7b0) that thing should never happen, hence it's not a fix.

@Giotino
Copy link
Collaborator Author

Giotino commented May 14, 2020

The only thing that came to my mind was restoring the order of the two events (error and response) in the async environment. I used a sync barrier (ae24c2f).

Note: my code needs a lot of cleaup and rewriting.

@Giotino
Copy link
Collaborator Author

Giotino commented May 14, 2020

BTW, this commit is passing on my Travic CI (and also on my PC).
I think the last commit of sindresorhus/got (2e349d1) broke everything.
https://travis-ci.org/github/Giotino/got/builds/687143936

@Giotino Giotino marked this pull request as ready for review May 15, 2020 08:10
@Giotino Giotino requested a review from szmarczak May 15, 2020 09:18
@Giotino Giotino changed the title Made calculateDelay promisable Make calculateDelay promisable May 15, 2020
@szmarczak szmarczak requested review from szmarczak and sindresorhus and removed request for sindresorhus and szmarczak May 15, 2020 19:38
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@szmarczak
Copy link
Collaborator

@sindresorhus Is this looking good? I'd like to release a patch version before any API depracations.

@szmarczak szmarczak merged commit 3745efc into sindresorhus:master May 16, 2020
@szmarczak
Copy link
Collaborator

Nice one! 🙌

@Giotino Giotino deleted the issue-1262 branch May 16, 2020 15:40
szmarczak pushed a commit to jaulz/got that referenced this pull request Jul 6, 2020
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

Successfully merging this pull request may close these issues.

Support for async calculateDelay
3 participants