httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Lewis <...@joe-lewis.com>
Subject Re: Question about global data
Date Mon, 09 Feb 2009 18:32:56 GMT
Sorin Manolache wrote:
> On Mon, Feb 9, 2009 at 19:04, Thomas Moyer <tmmoyer@cse.psu.edu> wrote:
>   
>> Is it possible to have some global data that is accessible to all request
>> threads.  Something like a socket is what I am interested in. For example,
>> if I have a socket that opens a connection to another system, I would like
>> to share this one open socket across all threads.  If this isn't possible,
>> what are other options for sharing something like a socket between multiple
>> request threads.
>>     
>
> You simply declare a global variable in the source file.
>
> You open the connection either in post_config or in child_init hook.
> post_config is called when apache starts and each time the
> configuration is reloaded, and it is called before apache forks its
> children. post_config is called with the effective UID of the user who
> starts apache (typically root), while child_init is called with the
> effective UID defined in the User configuration directive (a
> non-privileged user, typically www-data or nobody). child_init is run
> once in each apache child upon child creation.
>
> So, in the first case, you would have one file descriptor shared by
> all processes, while in the second case you would have N open
> connections to your backend, where N is the number of apache
> processes.
>
> In any case, all threads in a process share the socket.
>
> S
>
>   

Just remember to control who writes with semaphores or mutexes - failure 
to do so will cause corrupt data, bad responses, etc.  (BTW, I'd code 
this in a way that you could easily change to a proxy-type connection, 
where you connect to a local proxy, which controls which process/thread 
gets the single network socket, just in case you find that the file 
descriptors don't work in the way you think they do.)

Joe

Mime
View raw message