question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can one client connection be used for the lifetime of an app?

See original GitHub issue

Since as of version 3.0.0 per https://github.com/jbielick/faktory_worker_node/blob/master/CHANGELOG.md#300--2019-02-11, a connection pool is added to each client, is it advantageous/recommended to instantiate a single client connection to be used for the lifetime of an app?

Also, can you clarify what is meant by (taken from the changelog):

Client.connect() not necessary before using connection Client.connect() now connects and disconnects to test the connection details. A connection is created in the pool when it is needed.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jbielickcommented, Jul 10, 2019

Does this make sense? And I don’t need to close the client manually?

Absolutely! And as for closing: I would try and do so, but it’s not typically critical.

SIGINT or whatever signal you’re sending typically terminates those connections, but it can be helpful to await client.close() to shut down the connection pool gracefully. I wouldn’t worry too much about it unless you’re having issues.

Also—you may have omitted some small details for brevity, so forgive me if these are already known. faktory.connect() is async, so you’ll want to await that before starting your server. This ensures the connection is possible and ready before starting your app (helpful when it comes to health checks). I’d recommend adding the client to the request object in a middleware so it’s available to all HTTP handlers instead of relying on the closure having access to the client var. It would look something like this:

import faktory from 'faktory-worker';
import express from 'express';

const app = express();

router.get('/some-path', async (req, res, next) {
  // ... do some work
  await req.faktory.job(...).push();
  await req.faktory.job(...).push();
  next();
});

(async () => {
  const client = await faktory.connect();
  
  app.use(async (req, res, next) => {
    req.faktory = client;
    next();
  });  

  app.listen(...);
}).catch(console.error.bind(console));

But again, if you don’t have any issues you’re probably fine. Feel free to disregard.

0reactions
CaryLandholtcommented, Jul 10, 2019

Excellent! Yep, was going to use express middleware just like your code snippet. Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redis one connection per application lifetime - java
This connection would be used by various functions. Or the alternative is better: scoping each connection to the method, which would then ...
Read more >
Understanding and Handling Connection Lifetime Events ...
This article provides an overview of the SignalR connection, reconnection, and disconnection events that you can handle, and timeout and ...
Read more >
MySQL Connection Handling and Scaling
A long lived connection is a connection that is open “indefinitely”. For example one might have a Web server or an Application server...
Read more >
You're using HttpClient wrong and it is destabilizing your ...
> HttpClient is intended to be instantiated once and re-used throughout the life of an application. Especially in server applications, creating ...
Read more >
What's the connection lifetime in the pool? / When are idle ...
I noticed that the original driver has the Connection Lifetime option, the doc says: When a connection is returned to the pool, its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found