JsCommunity / http-server-plus

Augmented http.Server, HTTP/HTTPS and multiple ports on the same instance
4 stars 0 forks source link

Support systemd socket activation #6

Closed chewi closed 8 years ago

chewi commented 8 years ago

This library supports passing a file descriptor but it would be really handy if it could check LISTEN_FDS and LISTEN_PID environment variables to support systemd's socket activation. Applications using the library could do this but it would be more useful if the library could do it for you. Maybe accept a listen_fds option or do this when socket is true instead of a string?

This would sidestep #5 as systemd would be responsible for setting the socket permissions.

julien-f commented 8 years ago

That's not a bad idea :)

julien-f commented 8 years ago

Also, AFAIK, systemd's file descriptors always start from 3 so the user should be able to set it itself.

https://github.com/julien-f/simple-front-http-server/blob/master/sample.config.json#L15-L22

julien-f commented 8 years ago

I agree it would be nice to handle this automatically but how does the library knows which config is associated with each FD if there are multiple ones?

chewi commented 8 years ago

The config is an array so I guess you'd need to put them in the same order as they are in the unit file. From man systemd.socket:

Sockets configured in one unit are passed in the order of configuration, but no ordering between socket units is specified.

julien-f commented 8 years ago

The config is not an array, the user as to call listen() for each different “listen method.”

And I don't like relying on an implicit order, I think it would be best to add a specific method for systemd's sockets which would be provided a socket index:

server.listen({
  systemdSocket: 0 // LISTEND_FDS + 0
})
chewi commented 8 years ago

Ah sorry, it's an array in xo-server and I thought it passed that in as-is but it iterates over it. Your suggestion sounds good.

chewi commented 8 years ago

I had hoped to work around this in the short term by just passing fd: 3 directly but I can't get it to work at all. I always get Web server could not listen on http://<fd:3> and then it crashes. If I listen on TCP so it doesn't crash, I can see that the inode number for fd 3 under /proc matches the inode number shown by the ss command. Oh well.

julien-f commented 8 years ago

Hmmm, that's weird, I'm going to implement it very quickly to allow you to try it.

chewi commented 8 years ago

I just adjusted xo-server so that I could see the real error message. It's listen ENOTSOCK.

chewi commented 8 years ago

Figured it out! It's because I was starting it with npm start, if I do node bin/xo-server, it works. I still get a Cannot read property 'address' of null warning but it works anyway.

julien-f commented 8 years ago

Ok, good for you :+1:

julien-f commented 8 years ago

Released in v0.8.0.

(Will be available in the next version of Xen Orchestra).

chewi commented 8 years ago

Awesome, thanks very much.

julien-f commented 8 years ago

I just hope it'll work ^^