Amir
Amir

Reputation: 6176

Handing over an established TCP connection from one process to another

I am writing a simple web server with C++ that handles long-lived connections. However, I need to reload my web server from time to time. I wonder if there is a way that I can hand over the established connections from one process to another process to be able to retain my established connections after reload.

Would that be enough to only pass file descriptors? what would happen to connection states? Any similar open source project that does the same thing?

Any thoughts or ideas?

Thanks,

Upvotes: 1

Views: 829

Answers (1)

yzt
yzt

Reputation: 9103

I really have no idea whether this is possible, but I think not. If you fork() then the child will "inherit" the descriptors, but I don't know whether they behave like the should (though I suspect that they do.) And with forking, you can't run new code (can you?) Simple descriptor numbers are process-specific, so just passing them to a new, unrelated process won't work either, and they will be closed when your process terminates anyway.

One solution (in the absence of a simpler one,) is to break your server into two processes:

  1. Front-end: A very simple process that just accepts the connections, keep them open and forwards any data it receives to the second process, and vice versa.
  2. Server: The real web server, that does all the logic and processing, but does not communicate with the clients directly.

The first and second processes communicate via a simple protocol. One feature of this protocol must that it does support the second process being terminated and relaunched.

Now, you can reload the actual server process without losing the client connections (since they are handled by the front-end process.) And since this front-end is extremely simple and probably has very few configurations and bugs, you rarely need to reload it at all. (I'm assuming that you need to reload your server process because it runs into bugs that need to be fixed or you need to change configurations and stuff.)

Another important and helpful feature that this system can have is to be able to transition between server processes "gradually". That is, you already have a front-end and a server running, but you decide to reload the server. You launch another server process that connects to the front-end (while the old server is still running and connected,) and the front-end process forwards all the new client connections to the new server process (or even all the new requests coming from the existing client connections.) And when the old server finishes processing all the requests that it has under processing, it gracefully and cleanly exits.

As I said, this is a solution you might to try only if nothing easier and simpler is found.

Upvotes: 2

Related Questions