[Mongrel] mongrel thread safety and global variables
gethemant at gmail.com
Fri Sep 1 08:06:10 EDT 2006
In environment.rb file, I initialize a connection to some data vending
servers, through TCP sockets.
The connection object is global and hence the code:
$generic_connection = ConnectionClass.instance (singleton class)
$generic_connection.connect_me( this call will make the connection)
The above approach is to make sure that, only one connection is made to the
data vending servers.ConnectionClass is a library Class that, I have written
to handle connections and to ensure exception handling and all that stuff.
Now, in our rails code, whenever we need some data from these data vending
servers, we use this global variable to get the data.
$generic_connection.get_data('<our internal protocol>')
Since, we switched to mongrel, we are facing a strange issue. Our protocol
code, specify what kind of data we want from data vending servers.So, lets
say we want, x informatio, then we specify xxx as protocol code and get the
data. protocol code "yyy" would give completely different kind of data.
Now..sometimes what happens is, an Ajax call requests xxx protocol code and
in the response we get data of protocol code yyy. Data vending servers are
threaded again, and for each connection it starts a new thread and each
request is served on a new thread.
Data vending servers are again mostly written in ruby.
Now, I am wondering where is the problem?
As i know, mongrel serves requests on new threads, so what happens to this
global variable that is shared among the threads.Is there a possiblity that,
problem is here?
I mean, lets say a thread is using this global variable for fetching data
for protocol code xxx and same time another thread requests for protocol
code yyy. My connection class, which is basically a proxy to these data
vending servers, may not be thread safe.If this is the problem, then can
anybody suggest better approach of making single connection to data vending
servers, without using shared global variables.
It could be a problem with our data vending servers also.But I am not so,
sure about that.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Mongrel-users