GRPC C++: Pre-fork worker model

161 views
Skip to first unread message

stefan pantos

unread,
Dec 4, 2023, 11:02:19 AM12/4/23
to grpc.io
Hi,

Is it possible to create a GRPC server in C++ using a pre-fork worker model and can someone point me to an example. I see there are some C prefork related functions but I cannot find an example of using this and cannot quite see how it would work either. I did try myself using AddInsecureChannelFromFd but I didn't have much success not sure if it is me simply making a coding mistake or complete miss understanding of the use case.

Incase it isn't clear what I want to do I'll outline the idea in more detail.
A parent process creates, binds and listens on the incoming port.
Then the child processes are forked, they then do an accept on the now shared file descriptor. Only one of the children will accept a connection and will end up handing that connection but any other connections coming will be handled by one of the other child processes.

This is a method outlined in UNIX Network Programming, and I think is a method used in Apache Httpd.

The reason I want to use this method is that I have a code base which is not thread safe and it would take a long time and a lot of trial and error to make it thread safe and perform well enough. We have used this method for other protocols with good effect but none of them are as good as GRPC in my opinion.

Thanks for any help you can give me in advance.
Stefan Pantos

Eugene Ostroukhov

unread,
Jul 12, 2024, 1:12:22 PM7/12/24
to grpc.io
gRPC is not designed for this scenario. gRPC closes all file descriptors before forking so the connections would not be available in the client. This is explicit expectation and is covered with this test case - https://github.com/grpc/grpc/blob/master/test/cpp/end2end/client_fork_test.cc

Richard Belleville

unread,
Jul 12, 2024, 1:53:14 PM7/12/24
to grpc.io
Instead of using a parent process / child process model, you could consider cutting out the parent process entirely and using SO_REUSEPORT to handle multiplexing the single address / port combo to multiple worker processes. There's an example in Python here that illustrates this. This should extrapolate pretty straightforwardly to C++.

Stefan Pantos

unread,
Jul 12, 2024, 3:01:45 PM7/12/24
to Richard Belleville, grpc.io
Thanks Richard. I think I saw this but for some reason I didn’t realise how it was work.


Sent from my iPhone

On 12 Jul 2024, at 18:53, 'Richard Belleville' via grpc.io <grp...@googlegroups.com> wrote:

Instead of using a parent process / child process model, you could consider cutting out the parent process entirely and using SO_REUSEPORT to handle multiplexing the single address / port combo to multiple worker processes. There's an example in Python here that illustrates this. This should extrapolate pretty straightforwardly to C++.
--
You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/oSwXlQLQLRM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/86cd9f65-8791-492c-802d-0493705d82c2n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages