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

New undici pool is created on each request #206

Closed
2 tasks done
artur-ma opened this issue Oct 25, 2021 · 5 comments · Fixed by #207
Closed
2 tasks done

New undici pool is created on each request #206

artur-ma opened this issue Oct 25, 2021 · 5 comments · Fixed by #207
Labels

Comments

@artur-ma
Copy link
Contributor

artur-ma commented Oct 25, 2021

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.22.1

Plugin version

6.4.0

Node.js version

16.11

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.3

Description

proxy will create a new connection pool on each request, which will produce unlimited number of connections to single host and high resource usage (also performance decrease)

https://github.com/fastify/fastify-reply-from/blob/master/lib/request.js#L133

Steps to Reproduce

create proxy:

const app = require('fastify')({
  logger: true
});

app.register(require('fastify-reply-from'), {
  base: 'http://localhost:3010/',
  undici: {
    connections: 10,
  },
})

app.get('/alive', (request, reply) => {
  reply.from('/alive')
})

app.listen(3000, (err) => {
  if (err) {
    throw err
  }
})

create target

const fastify = require('fastify')({
  logger: true,
  keepAliveTimeout: 30000,
})

fastify.get('/alive', (request, reply) => {
  reply.send({ hello: 'world' })
})

// Run the server!
fastify.listen(3010, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

run autocannon -r 20 http://localhost:3000/alive -f

then check number of file descriptors (open sockets) both on target and proxy
lsof -a -p <PID> | wc -l

Expected Behavior

Since number of connections is limited to 10 per origin (host), expected limited number of file descriptors(open sockets), but the number continue to grow to thousands

fastify-reply-from should use build-in request function implemented in undici
it uses Agent singleton with a single pool per host

p.s with plugin 5.x it works as expected

@mcollina
Copy link
Member

This is a bad bug I should have spotted. Could you send a PR? It'd probably take less time as I'm swamped.

@artur-ma
Copy link
Contributor Author

Thanks for the fast fix! Soryy for not sending the PR, I fell to sleep :D

@climba03003
Copy link
Member

climba03003 commented Oct 26, 2021

Thanks for the fast fix! Soryy for not sending the PR, I fell to sleep :D

Can you double check is the solution works on your end?

@artur-ma
Copy link
Contributor Author

artur-ma commented Oct 26, 2021

@climba03003 yes, it behaves as expected, today we will deploy it, then I will finally confirm it
Thanks!

@artur-ma
Copy link
Contributor Author

looks way better

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants