From 520225aedfae2352414201c6260fb251d08d5940 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Tue, 23 Oct 2012 06:37:25 -0500 Subject: [PATCH] * adding support for new heartbeat_interval field on LoginAck --- lib/jam_ruby/connection_manager.rb | 51 +++++++++++++++--------------- lib/jam_ruby/message_factory.rb | 4 +-- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lib/jam_ruby/connection_manager.rb b/lib/jam_ruby/connection_manager.rb index bb8100c45..31f438fb2 100644 --- a/lib/jam_ruby/connection_manager.rb +++ b/lib/jam_ruby/connection_manager.rb @@ -39,6 +39,8 @@ module JamRuby end end + @log.debug("deleting #{stale_clients.length} stale connections") + stale_clients.each do |client_id| delete_connection(client_id) end @@ -62,7 +64,6 @@ module JamRuby @mq_router.publish_to_friends(friend_ids, friend_update, user_id) end end - #end end @@ -111,7 +112,6 @@ module JamRuby unless music_session_id.nil? conn.exec("DELETE FROM music_sessions id = $1 AND 0 = (SELECT count(music_session_id) FROM connections where music_session_id = $1)", [music_session_id]).clear end - # end end def destroy_if_empty(conn, music_session_id) @@ -225,34 +225,35 @@ module JamRuby end end end - end - # Creates a connection manager, and associates the connection created by active_record with ourselves - def self.active_record_transaction(&block) - - connection_manager = ConnectionManager.new - ActiveRecord::Base.connection_pool.with_connection do |connection| - # create a transaction, and pass the current connection to ConnectionManager. - # this lets the entire operation work with the same transaction, - # across Rails ActiveRecord and the pg-gem based code in ConnectionManager. - connection_manager.pg_conn = connection.instance_variable_get("@connection") - - block.call(connection_manager) + def lock_connections(conn) + conn.exec("LOCK connections IN EXCLUSIVE MODE").clear end - end - def lock_connections(conn) - conn.exec("LOCK connections IN EXCLUSIVE MODE").clear - end + def gather_friends(conn, user_id) + friend_ids = [] + conn.exec("SELECT f1.friend_id as friend_id FROM friendships f1 WHERE f1.user_id = $1 AND f1.friend_id IN (SELECT f2.user_id FROM friendships f2 WHERE f2.friend_id = $1)", [user_id]) do |friend_results| + friend_results.each do |friend_result| + friend_ids.push(friend_result['friend_id']) + end + end + return friend_ids + end - def gather_friends(conn, user_id) - friend_ids = [] - conn.exec("SELECT f1.friend_id as friend_id FROM friendships f1 WHERE f1.user_id = $1 AND f1.friend_id IN (SELECT f2.user_id FROM friendships f2 WHERE f2.friend_id = $1)", [user_id]) do |friend_results| - friend_results.each do |friend_result| - friend_ids.push(friend_result['friend_id']) + # Creates a connection manager, and associates the connection created by active_record with ourselves + def self.active_record_transaction(&block) + + connection_manager = ConnectionManager.new + ActiveRecord::Base.connection_pool.with_connection do |connection| + # create a transaction, and pass the current connection to ConnectionManager. + # this lets the entire operation work with the same transaction, + # across Rails ActiveRecord and the pg-gem based code in ConnectionManager. + connection_manager.pg_conn = connection.instance_variable_get("@connection") + + connection.transaction do + block.call(connection_manager) + end end end - return friend_ids end -# end end \ No newline at end of file diff --git a/lib/jam_ruby/message_factory.rb b/lib/jam_ruby/message_factory.rb index 630a9f03a..7f5cd30fe 100644 --- a/lib/jam_ruby/message_factory.rb +++ b/lib/jam_ruby/message_factory.rb @@ -37,8 +37,8 @@ end # create a login ack (login was successful) - def login_ack(public_ip, client_id, token) - login_ack = Jampb::LoginAck.new(:public_ip => public_ip, :client_id => client_id, :token => token) + def login_ack(public_ip, client_id, token, heartbeat_interval) + login_ack = Jampb::LoginAck.new(:public_ip => public_ip, :client_id => client_id, :token => token, :heartbeat_interval => heartbeat_interval) return Jampb::ClientMessage.new(:type => ClientMessage::Type::LOGIN_ACK, :route_to => CLIENT_TARGET, :login_ack => login_ack) end