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

SystemD socket activation support #480

Open
carlhoerberg opened this issue Jul 2, 2017 · 14 comments
Open

SystemD socket activation support #480

carlhoerberg opened this issue Jul 2, 2017 · 14 comments
Labels
Component: mosquitto-broker Type: Enhancement A new feature for a minor or major release.

Comments

@carlhoerberg
Copy link

We would like to add socket activation support, that is, systemd opens a TCP socket and listens for connections, once the first connection is made the mosquitto server is started and the server inherites the FD/socket and starts accepting the connections.

https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html#

We're willing to do the PR if you think it's a good idea to incorporate.

@karlp
Copy link
Contributor

karlp commented Jul 3, 2017

Is there anyway of doing this without making systemd a dependency? Also, what's the use case scenarion where you want this? I think the socket activation is really neat for short lived services, but for a message broker I simply don't understand why you would want the extra hassle?

@carlhoerberg
Copy link
Author

Yes, no need for a dependency, we can just look for the environment variables at start up, LISTEN_PID (compare with current process pid), LISTEN_FDS (how many more sockets to listen for), LISTEN_FDNAMES (to map each socket to a protocol, eg tls, websocket)

It lets us start listening for packets without starting the server. At CloudMQTT we have thousands of Mosquitto servers running on each box, but we could save a lot of resources and startup time if we only started the mosquitto process for the users that are actually using their servers.

@ralight
Copy link
Contributor

ralight commented Jul 3, 2017

Dependencies aren't the worst thing in the world as long as they're optional.

I can see that this would be a useful feature for you, but I'm struggling to see that it would be something that was more widely useful. As Karl points out, long running processes are a key point of MQTT so this is trying to optimise for a degenerate case.

Maybe it would be worth canvassing the mailing list to see what the wider opinion is.

@carlhoerberg
Copy link
Author

carlhoerberg commented Jul 3, 2017 via email

@ralight
Copy link
Contributor

ralight commented Jul 4, 2017

Let's give it a try then and see what it looks like - if you could base your work on the develop branch that would be best.

@ralight ralight added Type: Enhancement A new feature for a minor or major release. Component: mosquitto-broker labels Jul 18, 2017
@bwduncan
Copy link

bwduncan commented Sep 28, 2017

Just wanted to add my +1 here. My use case is exactly one of dependencies: I have many services which are relying on mosquitto being started, which at the moment are failing.

Obviously I could explicitly add a dependency to start those services after mosquitto has been started, but there is still a race since it takes some time for mosquitto to start listening. Also, I have to specify the dependency for every service using mosquitto, whereas socket activation means the dependency is implicit and dependent services will simply block until mosquitto is ready. This hopefully makes the system much more resilient, which is important when it's deployed!

@pat1
Copy link

pat1 commented Mar 10, 2018

I need something different but some link with this issue is possible.
I have a lot of environmental things that have to publish data with a local queue for data sent/not sent.
When one things publish, it dequeue the message. The message have to go in a DB.
At boot, to do not have lost message, the sequence have to be:

  • start mosquitto
  • start consumer (subscribe)
  • start producer (publish)

now I can have lost messages because things can publish before consumer can subscribe.
The only solution find for now is to use the firewall to enable local consumer before network producer.

@peterhoeg
Copy link

@carlhoerberg any progress? I would very much like to echo @pat1's sentiments that socket activation vastly simplifies dependency handling.

@carlhoerberg
Copy link
Author

carlhoerberg commented Mar 12, 2018 via email

@spuun
Copy link

spuun commented Mar 12, 2018

I'll give it some attention again. I need to test a few things. You find it here: https://github.com/CloudMQTT/mosquitto/tree/systemd

@karlp
Copy link
Contributor

karlp commented Mar 12, 2018

@bwduncan if your services rely on mosquitto being up that's fine, making notifications or events for mosquitto being up sounds perfectly reasonable. But this original PR was about making mosquitto not start until someone tried to connect to it, which is completely different altogether. @peterhoeg is that what you actually want as well?

@peterhoeg
Copy link

It's really the same thing. With socket activation, the socket will be available right from the get-go so that all services depending on mosquitto will not fail if the mosquitto service isn't up.

@chris2511
Copy link

The other good thing with "socket activation" is the avoidance of race conditions. With the sockets hold by the service manager, clients can connect at any time, even if the server crashed or got killed unexpectedly.

I created PR #2346 for https://github.com/eclipse/mosquitto/tree/develop
as well as patches for the current releases 2.0.12: 4c7cd33 and 2.0.14: 4c7cd33

@qubeck
Copy link

qubeck commented Dec 26, 2021

Great that eventually something is happening on that topic again. In the past I created PR #1522 to target the generic use case to accept connections passed via UDS... @chris2511 thx, for #2346. I think this partially has the potential to render my PR obsolete (since it's also not in sync with the current codebase anymore).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: mosquitto-broker Type: Enhancement A new feature for a minor or major release.
Projects
None yet
Development

No branches or pull requests

9 participants