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

forever-agent blocks with maxSocket=1 and POST message #23

Open
yherve opened this issue Jan 29, 2015 · 2 comments
Open

forever-agent blocks with maxSocket=1 and POST message #23

yherve opened this issue Jan 29, 2015 · 2 comments

Comments

@yherve
Copy link

yherve commented Jan 29, 2015

Hi
For a specific test, we need to make sure that two subsequent requests use the same connection.
So, I use forever-agent with request module and it works great if the two requests are GET.

Now if the second request is a POST, the second request is never sent and it blocks forever.

After some investigations, I see that in the pb is in 'addRequest':
in the case of POST, the code does not enter the logic to reuse socket, but calls parent method addRequestNoreuse.

The reason is the test on 'req.useChunkedEncodingByDefault', which is set to true in case of a POST.

      if (this.freeSockets[name] && this.freeSockets[name].length > 0 
          && !req.useChunkedEncodingByDefault) {

If I remove this test, it works fine.

So, of course I could patch the forever-agent to do what I want, but I was wondering

  • if there is a clean solution to this pb (apparently, we cannot set the value useChunkedEncodingByDefault ourselves on the request, it is set automatically if POST is used)
  • why does the agent needs to check useChunkedEncodingByDefault ?

Thanks

Yann

@mgorczyca
Copy link
Contributor

I am experiencing a similar issue, where the check for req.useChunkedEncodingByDefault is preventing socket reuse for a persistent connection that I need for a series of POST requests.

I'm currently getting past the problem by checking req.chunkedEncoding instead. Here is the change to index.js:

@@ -48,7 +48,7 @@
 ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
 ForeverAgent.prototype.addRequest = function(req, host, port) {
   var name = host + ':' + port
-  if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
+  if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.chunkedEncoding) {
     var idleSocket = this.freeSockets[name].pop()
     idleSocket.removeListener('error', idleSocket._onIdleError)
     delete idleSocket._onIdleError

@mgorczyca
Copy link
Contributor

Pull request #29 Enable socket reuse, is the proposal to resolve this issue.

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

2 participants