Class: Faye::Redis
- Inherits:
-
Object
- Object
- Faye::Redis
- Defined in:
- lib/faye/redis.rb
Constant Summary collapse
- DEFAULT_HOST =
'localhost'
- DEFAULT_PORT =
6379
- DEFAULT_DATABASE =
0
- DEFAULT_GC =
60
- LOCK_TIMEOUT =
120
Class Method Summary collapse
Instance Method Summary collapse
- #after_subscriptions_removed(client_id, &callback) ⇒ Object
- #client_exists(client_id, &callback) ⇒ Object
- #create_client(&callback) ⇒ Object
- #destroy_client(client_id, &callback) ⇒ Object
- #disconnect ⇒ Object
- #empty_queue(client_id) ⇒ Object
- #init ⇒ Object
-
#initialize(server, options) ⇒ Redis
constructor
A new instance of Redis.
- #ping(client_id) ⇒ Object
- #publish(message, channels) ⇒ Object
- #subscribe(client_id, channel, &callback) ⇒ Object
- #unsubscribe(client_id, channel, &callback) ⇒ Object
Constructor Details
#initialize(server, options) ⇒ Redis
Returns a new instance of Redis.
17 18 19 20 |
# File 'lib/faye/redis.rb', line 17 def initialize(server, ) @server = server @options = end |
Class Method Details
.create(server, options) ⇒ Object
13 14 15 |
# File 'lib/faye/redis.rb', line 13 def self.create(server, ) new(server, ) end |
Instance Method Details
#after_subscriptions_removed(client_id, &callback) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/faye/redis.rb', line 101 def after_subscriptions_removed(client_id, &callback) @redis.del(@ns + "/clients/#{client_id}/messages") do @redis.zrem(@ns + '/clients', client_id) do @server.debug 'Destroyed client ?', client_id @server.trigger(:disconnect, client_id) @redis.publish(@close_channel, client_id) callback.call if callback end end end |
#client_exists(client_id, &callback) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/faye/redis.rb', line 75 def client_exists(client_id, &callback) init cutoff = get_current_time - (1000 * 1.6 * @server.timeout) @redis.zscore(@ns + '/clients', client_id) do |score| callback.call(score.to_i > cutoff) end end |
#create_client(&callback) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/faye/redis.rb', line 63 def create_client(&callback) init client_id = @server.generate_id @redis.zadd(@ns + '/clients', 0, client_id) do |added| next create_client(&callback) if added == 0 @server.debug 'Created new client ?', client_id ping(client_id) @server.trigger(:handshake, client_id) callback.call(client_id) end end |
#destroy_client(client_id, &callback) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/faye/redis.rb', line 84 def destroy_client(client_id, &callback) init @redis.zadd(@ns + '/clients', 0, client_id) do @redis.smembers(@ns + "/clients/#{client_id}/channels") do |channels| i, n = 0, channels.size next after_subscriptions_removed(client_id, &callback) if i == n channels.each do |channel| unsubscribe(client_id, channel) do i += 1 after_subscriptions_removed(client_id, &callback) if i == n end end end end end |
#disconnect ⇒ Object
56 57 58 59 60 61 |
# File 'lib/faye/redis.rb', line 56 def disconnect return unless @redis @subscriber.unsubscribe(@message_channel) @subscriber.unsubscribe(@close_channel) EventMachine.cancel_timer(@gc) end |
#empty_queue(client_id) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/faye/redis.rb', line 169 def empty_queue(client_id) return unless @server.has_connection?(client_id) init key = @ns + "/clients/#{client_id}/messages" @redis.multi @redis.lrange(key, 0, -1) @redis.del(key) @redis.exec.callback do |, deleted| next unless = .map { |json| MultiJson.load(json) } @server.deliver(client_id, ) end end |
#init ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/faye/redis.rb', line 22 def init return if @redis uri = @options[:uri] || nil host = @options[:host] || DEFAULT_HOST port = @options[:port] || DEFAULT_PORT db = @options[:database] || DEFAULT_DATABASE auth = @options[:password] || nil gc = @options[:gc] || DEFAULT_GC @ns = @options[:namespace] || '' socket = @options[:socket] || nil if uri @redis = EventMachine::Hiredis.connect(uri) elsif socket @redis = EventMachine::Hiredis::Client.new(socket, nil, auth, db).connect else @redis = EventMachine::Hiredis::Client.new(host, port, auth, db).connect end @subscriber = @redis.pubsub @message_channel = @ns + '/notifications/messages' @close_channel = @ns + '/notifications/close' @subscriber.subscribe(@message_channel) @subscriber.subscribe(@close_channel) @subscriber.on(:message) do |topic, | empty_queue() if topic == @message_channel @server.trigger(:close, ) if topic == @close_channel end @gc = EventMachine.add_periodic_timer(gc, &method(:gc)) end |
#ping(client_id) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/faye/redis.rb', line 112 def ping(client_id) init timeout = @server.timeout return unless Numeric === timeout time = get_current_time @server.debug 'Ping ?, ?', client_id, time @redis.zadd(@ns + '/clients', time, client_id) end |
#publish(message, channels) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/faye/redis.rb', line 144 def publish(, channels) init @server.debug 'Publishing message ?', = MultiJson.dump() channels = Channel.(['channel']) keys = channels.map { |c| @ns + "/channels#{c}" } @redis.sunion(*keys) do |clients| clients.each do |client_id| queue = @ns + "/clients/#{client_id}/messages" @server.debug 'Queueing for client ?: ?', client_id, @redis.rpush(queue, ) @redis.publish(@message_channel, client_id) client_exists(client_id) do |exists| @redis.del(queue) unless exists end end end @server.trigger(:publish, ['clientId'], ['channel'], ['data']) end |
#subscribe(client_id, channel, &callback) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/faye/redis.rb', line 122 def subscribe(client_id, channel, &callback) init @redis.sadd(@ns + "/clients/#{client_id}/channels", channel) do |added| @server.trigger(:subscribe, client_id, channel) if added == 1 end @redis.sadd(@ns + "/channels#{channel}", client_id) do @server.debug 'Subscribed client ? to channel ?', client_id, channel callback.call if callback end end |
#unsubscribe(client_id, channel, &callback) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/faye/redis.rb', line 133 def unsubscribe(client_id, channel, &callback) init @redis.srem(@ns + "/clients/#{client_id}/channels", channel) do |removed| @server.trigger(:unsubscribe, client_id, channel) if removed == 1 end @redis.srem(@ns + "/channels#{channel}", client_id) do @server.debug 'Unsubscribed client ? from channel ?', client_id, channel callback.call if callback end end |