ruby - Is there one Rack app instance per HTTP request? -
i'm building facebook app called lovers, using sinatra app on heroku. it's running on ruby 1.9.2 on heroku's bamboo-mri-1.9.2 stack.
it's modular sinatra app, , in lovers source code, i'm giving each instance of sinatra app (lovers::application
) instance of facebook::application
:
require 'sinatra/base' class lovers::application < sinatra::base attr_reader :facebook def initialize(app=nil) @facebook = facebook::application.new( lovers::conf.fb_app_id, lovers::conf.fb_app_secret, lovers::conf.fb_canvas_name) super(app) end # ... end
that way, can lovers.application.facebook
access facebook::application
instance anywhere within lovers
module, lovers::user
.
does make sense, or should have instances of lovers::application
(if there's ever more one) share same facebook::application
instance, i.e., lovers.facebook
. that's we're doing redis: lovers.redis
, makes sense me. guess i'm leaning toward changing latter, want make sure before change it. think?
finally, there 1 instance of lovers::application
per http request?
update:
i read on heroku dynos. apparently, each dyno (process) runs instance of lovers::application
. so, after reading sharing global variable among processes, think means if define class variable @@hit_count
in lovers::application
class, have different values depending on dyno receives request, assuming increment @@hit_count
every time home page requested, i.e.:
@@hit_count = 0 "/" @@hit_count += 1 end
"finally, there 1 instance of lovers::application per http request?"
there's 1 instance per process/dyno.
"it have different values depending on dyno receives request, assuming increment @@hit_count every time home page requested"
yes, if need global state have keep state outside of process/dyno. there many different ways this, , choose depend on details of app , traffic levels. if don't lot of traffic can simple keeping in database. can atomic increments in postgres or mysql hit_count. however, approach may become bottleneck if have lot of traffic.
Comments
Post a Comment