You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Thomas Moyer <tm...@cse.psu.edu> on 2009/02/09 19:04:22 UTC

Question about global data

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.

~tom

- --
Thomas Moyer
Graduate Student
CSE Department
Penn State University
tmmoyer@cse.psu.edu
http://www.cse.psu.edu/~tmmoyer

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkmQcCYACgkQa7Yypxfw9TeuaACeLGfgZlgWU9PDzBIQj8+wquyl
rVYAoKTe5rG5v32omIxjjam2viLS/B19
=oduz
-----END PGP SIGNATURE-----

Re: Question about global data

Posted by Joe Lewis <jo...@joe-lewis.com>.
Sorin Manolache wrote:
> On Mon, Feb 9, 2009 at 19:04, Thomas Moyer <tm...@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

Re: Question about global data

Posted by Sorin Manolache <so...@gmail.com>.
On Mon, Feb 9, 2009 at 19:04, Thomas Moyer <tm...@cse.psu.edu> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> 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

-- 
A: Because it reverses the logical flow of conversation.
Q: Why is top-posting frowned upon?
A: Top-posting.
Q: What is the most annoying thing in e-mail?