Rails (internals) architectural question

Hi

Another newbe here… I was wondering if someone could please help me
understand the controllers/view inner working in Production

  1. If xxx_controller.rb is only loaded once does it mean there is only
    one
    instance of Controller serving all requests?
  2. If only one instance of controller is there then how are controllers
    instance variables protected from data corruption when serving multiple
    requests? Since these instance variables render data in views.
  3. If I store a SOAP::RPC::Driver object (pointing to a web service with
    only one function) in a global variable Do I run the risk of corrupting
    data
    when the web service response arrives?

thanks in advance

-daya

linux user wrote:

Hi

Another newbe here… I was wondering if someone could please help me
understand the controllers/view inner working in Production

  1. If xxx_controller.rb is only loaded once does it mean there is only
    one
    instance of Controller serving all requests?
  2. If only one instance of controller is there then how are controllers
    instance variables protected from data corruption when serving multiple
    requests? Since these instance variables render data in views.
  3. If I store a SOAP::RPC::Driver object (pointing to a web service with
    only one function) in a global variable Do I run the risk of corrupting
    data
    when the web service response arrives?

thanks in advance

-daya

  1. The controller you create all descend from ApplicationController,
    which itself descends from ActionController::Base. So the class
    BlogController is a subclass if the rails vanilla controller. On each
    request a new instance of the requested controller is created to server
    the request and then destroyed when the request is served.

  2. Since each request is served by a feshly created instance of a
    specific controller the instance variables never overlap or get corrupt
    since it’s 1 instance for 1 request.

  3. Each ruby process can only process one request at a time. So it
    would probably be fine since the process that is executing this will
    wait for it’s completion to process anything else and each process has
    it’s own global variables. However, to be sure you really dont want to
    use global variables for this.

On Aug 8, 2006, at 9:56 AM, linux user wrote:

  1. If xxx_controller.rb is only loaded once does it mean there is
    only one instance of Controller serving all requests?

A controller is instantiated per request.

  1. If only one instance of controller is there then how are
    controllers instance variables protected from data corruption when
    serving multiple requests? Since these instance variables render
    data in views.
  2. If I store a SOAP::RPC::Driver object (pointing to a web service
    with only one function) in a global variable Do I run the risk of
    corrupting data when the web service response arrives?

Requests are handled one at a time, so you don’t have to worry about
concurrent requests stepping on your global var. Though feel free to
worry about the global var itself :slight_smile:

jeremy

On 8/8/06, Jeremy K. [email protected] wrote:

On Aug 8, 2006, at 9:56 AM, linux user wrote:

  1. If xxx_controller.rb is only loaded once does it mean there is
    only one instance of Controller serving all requests?

A controller is instantiated per request.

Is this true even in Production mode ?? If yes, this will degrade
performance of rails compared other web frameworks?.

On Aug 8, 2006, at 2:06 PM, linux user wrote:

On 8/8/06, Jeremy K. [email protected] wrote: On Aug 8,
2006, at 9:56 AM, linux user wrote:

  1. If xxx_controller.rb is only loaded once does it mean there is
    only one instance of Controller serving all requests?

A controller is instantiated per request.

Is this true even in Production mode ?? If yes, this will degrade
performance of rails compared other web frameworks?.

I think misunderstood you - seems you’re referring to ‘reloading
Rails’ as in reloading the entire app framework. That does not occur
in production mode nor does it affect the issue you had in the
original email regarding global variables.

jeremy

On 8/8/06, linux user [email protected] wrote:

Is this true even in Production mode ?? If yes, this will
degrade performance of rails compared other web frameworks?.

Yes. No.

Joe

On 8/8/06, Jeremy K. [email protected] wrote:

performance of rails compared other web frameworks?.

I think misunderstood you - seems you’re referring to ‘reloading
Rails’ as in reloading the entire app framework. That does not occur
in production mode nor does it affect the issue you had in the
original email regarding global variables.

jeremy

I understand that reloading happens in Development to reload any source
code
changes and it doesn’t happen in production. And I am not talking about
reloading entire framework (the CGI approach) but My question is that if
a
new instance of the controller is created for each request, that in
itself
would be a performance overhead, when compared to a multi-thread model
of
Java servlets in which there is only one instance of the servlet which
serves all requests.

Are you aware of any performance comparisons between Java Servlets and
Rails Controllers?? would be interesting to see how Rails
fares in comparison to Java Servlets.

I agree it will not have effect on the global variables.

-daya

Is this true even in Production mode ?? If yes, this will
degrade performance of rails compared other web frameworks?.

Yes, this is true in production mode. One difference is that in dev
mode (well, based on configuration), the classes are (or may be, if
they have changed) reloaded for each request, whereas in production
mode they are cached, so you don’t incur the overhead of processing
the class definition every time.

And yes, this does incur a very minor performance overhead. I believe
this to be negligible - and for quite a few years now I have been
using similar “Instantiate things every time” mechanisms for Java web
development, with no problems. Normally, you’ll have much bigger
performance fish to fry.

Max

On Aug 9, 2006, at 6:28 AM, linux user wrote:

I understand that reloading happens in Development to reload any
source code changes and it doesn’t happen in production. And I am
not talking about reloading entire framework (the CGI approach) but
My question is that if a new instance of the controller is created
for each request, that in itself would be a performance overhead,

Yes, it is. I just couldn’t believe that was your concern :slight_smile:

when compared to a multi-thread model of Java servlets in which
there is only one instance of the servlet which serves all requests.

Please look into Mongrel or WEBrick handlers if you wish to pursue
the servlet model.

Are you aware of any performance comparisons between Java Servlets
and Rails Controllers?? would be interesting to see how Rails fares
in comparison to Java Servlets.

I haven’t, sorry.

jeremy

On 8/10/06, Jeremy K. [email protected] wrote:

On Aug 9, 2006, at 6:28 AM, linux user wrote:

I understand that reloading happens in Development to reload any
source code changes and it doesn’t happen in production. And I am
not talking about reloading entire framework (the CGI approach) but
My question is that if a new instance of the controller is created
for each request, that in itself would be a performance overhead,

Yes, it is. I just couldn’t believe that was your concern :slight_smile:

The only reason this was a concern because questions like this will be
raised when I make my case of using RoR instead of Java/Struts to the
architectural review board of the company I work for.

when compared to a multi-thread model of Java servlets in which

there is only one instance of the servlet which serves all requests.

Please look into Mongrel or WEBrick handlers if you wish to pursue
the servlet model.

On 8/10/06, linux user [email protected] wrote:

Yes, it is. I just couldn’t believe that was your concern :slight_smile:

The only reason this was a concern because questions like this will be
raised when I make my case of using RoR instead of Java/Struts to the
architectural review board of the company I work for.

It’s one Ruby object.

When I go:
x = 5
in Ruby, that also creates one Ruby object.

Joe