开发者

ruby rails loop causes server freeze

开发者 https://www.devze.com 2022-12-26 14:19 出处:网络
I am working on a Ruby on Rails project on Windows. I have Ruby 1.86 and Rails 2.35 installed. Everything is fine until I tried to implement a comet process. I have the following code written to respo

I am working on a Ruby on Rails project on Windows. I have Ruby 1.86 and Rails 2.35 installed. Everything is fine until I tried to implement a comet process. I have the following code written to respond to a long poll javascript request. But everytime this function is called, it will hang the whole rails server, no second request can get in, until the timeout. (I know there is juggernaut, but I like to implement one myself first :)

Is this due to my server setup? The project will be deployed on a linux server with Ngix and Passenger setup, will it suffer the same problem?

def comet_hook
    timeout(5) do
      while true do
        key = 'station_' + station_id.to_s + '_message_lastwrite'      
        if Rails.cache.exist?(key) 
          @c开发者_运维问答ache_time = DateTime.parse(Rails.cache.read(key))
          if @cache_time > hook_start
            @messages = @station.messages_posted_after(hook_start)
            hook_start = @cache_time
            break
          end 
        end
    end
    ...
end

Also with Rails memory store cache, I keep getting "cannot modify frozen object" error, so the above script only worked for me when I switched to File cache. :(


Your Windows setup probably consists of a single webrick or mongrel instance which can handle only one connection at a time.

Passeger and nginx setups can (and typically will), of course, accommodate multiple concurrent sessions via a cluster of webserver processes (be it a mongrel cluster behind nginx, or a cluster of Passenger Apache worker processes), if configured to do so (the servers configuration value for mongrel clusters, MaxClients for Apache/Passenger.)

Scale the number of concurrent client sessions and/or system memory based on expected traffic (e.g. if you expect 10 requests/second, where each request can take up to 5s or more to service, you will need to accommodate 50 or more client connections i.e. 50 or more Mongrel servers in the cluster or 50 or more Apache worker processes -- with non-trivial memory requirements.)

0

精彩评论

暂无评论...
验证码 换一张
取 消