Class: Faye::Redis::MessageQueue
- Inherits:
-
Object
- Object
- Faye::Redis::MessageQueue
- Defined in:
- lib/faye/redis/message_queue.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
-
#clear(client_id, &callback) ⇒ Object
Clear a client’s message queue.
-
#dequeue_all(client_id, &callback) ⇒ Object
Dequeue all messages for a client.
-
#enqueue(client_id, message, &callback) ⇒ Object
Enqueue a message for a client.
-
#initialize(connection, options = {}) ⇒ MessageQueue
constructor
A new instance of MessageQueue.
-
#peek(client_id, limit = 10, &callback) ⇒ Object
Peek at messages without removing them.
-
#size(client_id, &callback) ⇒ Object
Get queue size for a client.
Constructor Details
#initialize(connection, options = {}) ⇒ MessageQueue
Returns a new instance of MessageQueue.
9 10 11 12 |
# File 'lib/faye/redis/message_queue.rb', line 9 def initialize(connection, = {}) @connection = connection @options = end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/faye/redis/message_queue.rb', line 7 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/faye/redis/message_queue.rb', line 7 def @options end |
Instance Method Details
#clear(client_id, &callback) ⇒ Object
Clear a client’s message queue
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/faye/redis/message_queue.rb', line 114 def clear(client_id, &callback) # Simply delete the queue @connection.with_redis do |redis| redis.del(queue_key(client_id)) end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to clear queue for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#dequeue_all(client_id, &callback) ⇒ Object
Dequeue all messages for a client
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/faye/redis/message_queue.rb', line 44 def dequeue_all(client_id, &callback) # Get all messages and delete queue in a single atomic operation key = queue_key(client_id) = @connection.with_redis do |redis| # Use MULTI/EXEC to atomically get and delete redis.multi do |multi| multi.lrange(key, 0, -1) multi.del(key) end end # Parse messages from JSON = [] if && [0] [0].each do |json| begin << JSON.parse(json) rescue JSON::ParserError => e log_error("Failed to parse message JSON: #{e.}") end end end EventMachine.next_tick { callback.call() } if callback rescue => e log_error("Failed to dequeue messages for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#enqueue(client_id, message, &callback) ⇒ Object
Enqueue a message for a client
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/faye/redis/message_queue.rb', line 15 def enqueue(client_id, , &callback) # Add unique ID if not present (for message deduplication) = .dup ['id'] ||= # Store message directly as JSON = .to_json key = queue_key(client_id) @connection.with_redis do |redis| # Use Lua script to atomically RPUSH and set TTL only if key has no TTL # This prevents resetting TTL on every enqueue for hot queues redis.eval(<<~LUA, keys: [key], argv: [, .to_s]) redis.call('RPUSH', KEYS[1], ARGV[1]) local ttl = redis.call('TTL', KEYS[1]) if ttl == -1 then redis.call('EXPIRE', KEYS[1], tonumber(ARGV[2])) end return 1 LUA end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to enqueue message for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#peek(client_id, limit = 10, &callback) ⇒ Object
Peek at messages without removing them
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/faye/redis/message_queue.rb', line 77 def peek(client_id, limit = 10, &callback) = @connection.with_redis do |redis| redis.lrange(queue_key(client_id), 0, limit - 1) end = .map do |json| begin JSON.parse(json) rescue JSON::ParserError => e log_error("Failed to parse message JSON: #{e.}") nil end end.compact EventMachine.next_tick { callback.call() } if callback rescue => e log_error("Failed to peek messages for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#size(client_id, &callback) ⇒ Object
Get queue size for a client
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/faye/redis/message_queue.rb', line 100 def size(client_id, &callback) queue_size = @connection.with_redis do |redis| redis.llen(queue_key(client_id)) end EventMachine.next_tick { callback.call(queue_size) } if callback queue_size rescue => e log_error("Failed to get queue size for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(0) } if callback 0 end |