From 1c1949010eb32360dde663bd46a960e2dfdd44b6 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Mon, 11 Feb 2013 00:04:41 -0600 Subject: [PATCH] renamed connection timers to correspond to state names --- bin/websocket_gateway | 4 ++-- config/application.yml | 4 ++-- lib/jam_websockets/router.rb | 4 ++-- lib/jam_websockets/server.rb | 34 ++++++++++++++++++++-------------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/bin/websocket_gateway b/bin/websocket_gateway index 8260d500b..ead97d4ac 100755 --- a/bin/websocket_gateway +++ b/bin/websocket_gateway @@ -33,5 +33,5 @@ end ActiveRecord::Base.establish_connection(db_config) Server.new.run(:port => config["port"], :emwebsocket_debug => config["emwebsocket_debug"], - :max_stale_connection_time => config["max_stale_connection_time"], - :max_reconnect_time => config["max_reconnect_time"]) + :connect_time_stale => config["connect_time_stale"], + :connect_time_expire => config["connect_time_expire"]) diff --git a/config/application.yml b/config/application.yml index ef2c4e214..f0576f674 100644 --- a/config/application.yml +++ b/config/application.yml @@ -1,6 +1,6 @@ Defaults: &defaults - max_stale_connection_time: 30 - max_reconnect_time: 180 + connect_time_stale: 30 + connect_time_expire: 180 development: port: 6767 diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index b83e513e1..0309f4373 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -39,11 +39,11 @@ module JamWebsockets end - def start(max_stale_connection_time, options={:host => "localhost", :port => 5672}) + def start(connect_time_stale, options={:host => "localhost", :port => 5672}) @log.info "startup" - @heartbeat_interval = max_stale_connection_time / 2 + @heartbeat_interval = connect_time_stale / 2 begin @connection = AMQP.connect(:host => options[:host], :port => options[:port]) diff --git a/lib/jam_websockets/server.rb b/lib/jam_websockets/server.rb index be738bba1..158047619 100644 --- a/lib/jam_websockets/server.rb +++ b/lib/jam_websockets/server.rb @@ -14,13 +14,13 @@ module JamWebsockets host = "0.0.0.0" port = options[:port] - max_stale_connection_time = options[:max_stale_connection_time] - max_reconnect_time = options[:max_reconnect_time] + connect_time_stale = options[:connect_time_stale].to_i + connect_time_expire = options[:connect_time_expire].to_i - @log.info "starting server #{host}:#{port} with staleness_time=#{max_stale_connection_time}" + @log.info "starting server #{host}:#{port} with staleness_time=#{connect_time_stale}; reconnect time = #{connect_time_expire}" EventMachine.run do - @router.start(max_stale_connection_time) + @router.start(connect_time_stale) # if you don't do this, the app won't exit unless you kill -9 at_exit do @@ -28,8 +28,8 @@ module JamWebsockets @router.cleanup end - start_connection_flagger(max_stale_connection_time) - start_connection_cleaner(max_reconnect_time) + start_connection_expiration(connect_time_expire) + start_connection_flagger(connect_time_stale) start_websocket_listener(host, port, options[:emwebsocket_debug]) end @@ -42,34 +42,40 @@ module JamWebsockets end end - - def start_connection_cleaner(stale_max_time) + def start_connection_expiration(stale_max_time) # one cleanup on startup - cleanup_stale_connections(stale_max_time) + @log.info("*** start_connection_expiration: #{stale_max_time}") - EventMachine::PeriodicTimer.new(15) do - cleanup_stale_connections(stale_max_time) + expire_stale_connections(stale_max_time) + + EventMachine::PeriodicTimer.new(stale_max_time) do + @log.info("*** start_connection_expiration: TIMER #{stale_max_time}") + expire_stale_connections(stale_max_time) end end - def cleanup_stale_connections(stale_max_time) + def expire_stale_connections(stale_max_time) ConnectionManager.active_record_transaction do |connection_manager| - connection_manager.remove_stale_connections(stale_max_time) + connection_manager.expire_stale_connections(stale_max_time) end end def start_connection_flagger(flag_max_time) + @log.info("*** start_connection_flagger: #{flag_max_time}") + # one cleanup on startup flag_stale_connections(flag_max_time) - EventMachine::PeriodicTimer.new(15) do + EventMachine::PeriodicTimer.new(flag_max_time) do + @log.info("*** start_connection_flagger: TIMER #{flag_max_time}") flag_stale_connections(flag_max_time) end end def flag_stale_connections(flag_max_time) ConnectionManager.active_record_transaction do |connection_manager| + @log.info("*** flag_stale_connections: TIMER #{flag_max_time}") connection_manager.flag_stale_connections(flag_max_time) end end