Class: Faye::Redis::ClientRegistry
- Inherits:
-
Object
- Object
- Faye::Redis::ClientRegistry
- Defined in:
- lib/faye/redis/client_registry.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#all(&callback) ⇒ Object
Get all active clients.
-
#cleanup_expired(&callback) ⇒ Object
Clean up expired clients.
-
#create(client_id, &callback) ⇒ Object
Create a new client.
-
#destroy(client_id, &callback) ⇒ Object
Destroy a client.
-
#exists?(client_id, &callback) ⇒ Boolean
Check if a client exists.
-
#get(client_id, &callback) ⇒ Object
Get client data.
-
#initialize(connection, options = {}) ⇒ ClientRegistry
constructor
A new instance of ClientRegistry.
-
#ping(client_id) ⇒ Object
Ping a client to keep it alive.
Constructor Details
#initialize(connection, options = {}) ⇒ ClientRegistry
Returns a new instance of ClientRegistry.
8 9 10 11 |
# File 'lib/faye/redis/client_registry.rb', line 8 def initialize(connection, = {}) @connection = connection @options = end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/faye/redis/client_registry.rb', line 6 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/faye/redis/client_registry.rb', line 6 def @options end |
Instance Method Details
#all(&callback) ⇒ Object
Get all active clients
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/faye/redis/client_registry.rb', line 99 def all(&callback) client_ids = @connection.with_redis do |redis| redis.smembers(clients_index_key) end EventMachine.next_tick { callback.call(client_ids) } if callback client_ids rescue => e log_error("Failed to get all clients: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#cleanup_expired(&callback) ⇒ Object
Clean up expired clients
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/faye/redis/client_registry.rb', line 113 def cleanup_expired(&callback) all do |client_ids| # Check existence in batch using pipelined commands results = @connection.with_redis do |redis| redis.pipelined do |pipeline| client_ids.each do |client_id| pipeline.exists?(client_key(client_id)) end end end # Collect expired client IDs expired_clients = [] client_ids.each_with_index do |client_id, index| result = results[index] # Redis 5.x returns boolean, older versions return integer exists = result.is_a?(Integer) ? result > 0 : result expired_clients << client_id unless exists end # Batch delete expired clients if expired_clients.any? @connection.with_redis do |redis| redis.pipelined do |pipeline| expired_clients.each do |client_id| pipeline.del(client_key(client_id)) pipeline.srem?(clients_index_key, client_id) end end end end EventMachine.next_tick { callback.call(expired_clients.size) } if callback end rescue => e log_error("Failed to cleanup expired clients: #{e.}") EventMachine.next_tick { callback.call(0) } if callback end |
#create(client_id, &callback) ⇒ Object
Create a new client
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/faye/redis/client_registry.rb', line 14 def create(client_id, &callback) = Time.now.to_i client_data = { client_id: client_id, created_at: , last_ping: , server_id: server_id } @connection.with_redis do |redis| redis.multi do |multi| multi.hset(client_key(client_id), client_data.transform_keys(&:to_s)) multi.sadd?(clients_index_key, client_id) multi.expire(client_key(client_id), client_timeout) end end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to create client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#destroy(client_id, &callback) ⇒ Object
Destroy a client
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/faye/redis/client_registry.rb', line 38 def destroy(client_id, &callback) @connection.with_redis do |redis| redis.multi do |multi| multi.del(client_key(client_id)) multi.srem?(clients_index_key, client_id) end end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to destroy client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#exists?(client_id, &callback) ⇒ Boolean
Check if a client exists
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/faye/redis/client_registry.rb', line 53 def exists?(client_id, &callback) result = @connection.with_redis do |redis| redis.exists?(client_key(client_id)) end # Redis 5.x returns boolean, older versions return integer exists = result.is_a?(Integer) ? result > 0 : result EventMachine.next_tick { callback.call(exists) } if callback exists rescue => e log_error("Failed to check client existence #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback false end |
#get(client_id, &callback) ⇒ Object
Get client data
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/faye/redis/client_registry.rb', line 84 def get(client_id, &callback) data = @connection.with_redis do |redis| redis.hgetall(client_key(client_id)) end client_data = data.empty? ? nil : symbolize_keys(data) EventMachine.next_tick { callback.call(client_data) } if callback client_data rescue => e log_error("Failed to get client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(nil) } if callback nil end |
#ping(client_id) ⇒ Object
Ping a client to keep it alive
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/faye/redis/client_registry.rb', line 70 def ping(client_id) = Time.now.to_i @connection.with_redis do |redis| redis.multi do |multi| multi.hset(client_key(client_id), 'last_ping', ) multi.expire(client_key(client_id), client_timeout) end end rescue => e log_error("Failed to ping client #{client_id}: #{e.}") end |