Class: MessageBus::Postgres::ReliablePubSub
- Inherits:
-
Object
- Object
- MessageBus::Postgres::ReliablePubSub
- Defined in:
- lib/message_bus/backends/postgres.rb
Constant Summary collapse
- UNSUB_MESSAGE =
"$$UNSUBSCRIBE"
Instance Attribute Summary collapse
-
#clear_every ⇒ Object
Returns the value of attribute clear_every.
-
#max_backlog_age ⇒ Object
Returns the value of attribute max_backlog_age.
-
#max_backlog_size ⇒ Object
Returns the value of attribute max_backlog_size.
-
#max_global_backlog_size ⇒ Object
Returns the value of attribute max_global_backlog_size.
-
#subscribed ⇒ Object
readonly
Returns the value of attribute subscribed.
Class Method Summary collapse
Instance Method Summary collapse
- #after_fork ⇒ Object
- #backend ⇒ Object
- #backlog(channel, last_id = nil) ⇒ Object
- #client ⇒ Object
- #get_message(channel, message_id) ⇒ Object
- #global_backlog(last_id = nil) ⇒ Object
- #global_subscribe(last_id = nil, &blk) ⇒ Object
- #global_unsubscribe ⇒ Object
-
#initialize(config = {}, max_backlog_size = 1000) ⇒ ReliablePubSub
constructor
max_backlog_size is per multiplexed channel.
- #last_id(channel) ⇒ Object
- #new_connection ⇒ Object
- #postgresql_channel_name ⇒ Object
- #process_global_backlog(highest_id) ⇒ Object
- #publish(channel, data, queue_in_memory = true) ⇒ Object
-
#reset! ⇒ Object
use with extreme care, will nuke all of the data.
- #subscribe(channel, last_id = nil) ⇒ Object
Constructor Details
#initialize(config = {}, max_backlog_size = 1000) ⇒ ReliablePubSub
max_backlog_size is per multiplexed channel
219 220 221 222 223 224 225 226 |
# File 'lib/message_bus/backends/postgres.rb', line 219 def initialize(config = {}, max_backlog_size = 1000) @config = config @max_backlog_size = max_backlog_size @max_global_backlog_size = 2000 # after 7 days inactive backlogs will be removed @max_backlog_age = 604800 @clear_every = config[:clear_every] || 1 end |
Instance Attribute Details
#clear_every ⇒ Object
Returns the value of attribute clear_every.
210 211 212 |
# File 'lib/message_bus/backends/postgres.rb', line 210 def clear_every @clear_every end |
#max_backlog_age ⇒ Object
Returns the value of attribute max_backlog_age.
210 211 212 |
# File 'lib/message_bus/backends/postgres.rb', line 210 def max_backlog_age @max_backlog_age end |
#max_backlog_size ⇒ Object
Returns the value of attribute max_backlog_size.
210 211 212 |
# File 'lib/message_bus/backends/postgres.rb', line 210 def max_backlog_size @max_backlog_size end |
#max_global_backlog_size ⇒ Object
Returns the value of attribute max_global_backlog_size.
210 211 212 |
# File 'lib/message_bus/backends/postgres.rb', line 210 def max_global_backlog_size @max_global_backlog_size end |
#subscribed ⇒ Object (readonly)
Returns the value of attribute subscribed.
209 210 211 |
# File 'lib/message_bus/backends/postgres.rb', line 209 def subscribed @subscribed end |
Class Method Details
.reset!(config) ⇒ Object
214 215 216 |
# File 'lib/message_bus/backends/postgres.rb', line 214 def self.reset!(config) MessageBus::Postgres::Client.new(config).reset! end |
Instance Method Details
#after_fork ⇒ Object
236 237 238 |
# File 'lib/message_bus/backends/postgres.rb', line 236 def after_fork client.reconnect end |
#backend ⇒ Object
232 233 234 |
# File 'lib/message_bus/backends/postgres.rb', line 232 def backend :postgres end |
#backlog(channel, last_id = nil) ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/message_bus/backends/postgres.rb', line 273 def backlog(channel, last_id = nil) items = client.backlog channel, last_id.to_i items.map! do |id, data| MessageBus::Message.new id, id, channel, data end end |
#client ⇒ Object
245 246 247 |
# File 'lib/message_bus/backends/postgres.rb', line 245 def client @client ||= new_connection end |
#get_message(channel, message_id) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/message_bus/backends/postgres.rb', line 289 def (channel, ) if data = client.get_value(channel, ) MessageBus::Message.new , , channel, data else nil end end |
#global_backlog(last_id = nil) ⇒ Object
281 282 283 284 285 286 287 |
# File 'lib/message_bus/backends/postgres.rb', line 281 def global_backlog(last_id = nil) items = client.global_backlog last_id.to_i items.map! do |id, channel, data| MessageBus::Message.new id, id, channel, data end end |
#global_subscribe(last_id = nil, &blk) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/message_bus/backends/postgres.rb', line 325 def global_subscribe(last_id=nil, &blk) raise ArgumentError unless block_given? highest_id = last_id begin client.subscribe(postgresql_channel_name) do |on| h = {} on.subscribe do if highest_id process_global_backlog(highest_id) do |m| h[m.global_id] = true yield m end end h = nil if h.empty? @subscribed = true end on.unsubscribe do @subscribed = false end on. do |c,m| if m == UNSUB_MESSAGE @subscribed = false return end m = MessageBus::Message.decode m # we have 3 options # # 1. message came in the correct order GREAT, just deal with it # 2. message came in the incorrect order COMPLICATED, wait a tiny bit and clear backlog # 3. message came in the incorrect order and is lowest than current highest id, reset if h # If already yielded during the clear backlog when subscribing, # don't yield a duplicate copy. unless h.delete(m.global_id) h = nil if h.empty? yield m end else yield m end end end rescue => error MessageBus.logger.warn "#{error} subscribe failed, reconnecting in 1 second. Call stack\n#{error.backtrace.join("\n")}" sleep 1 retry end end |
#global_unsubscribe ⇒ Object
320 321 322 323 |
# File 'lib/message_bus/backends/postgres.rb', line 320 def global_unsubscribe client.publish(postgresql_channel_name, UNSUB_MESSAGE) @subscribed = false end |
#last_id(channel) ⇒ Object
269 270 271 |
# File 'lib/message_bus/backends/postgres.rb', line 269 def last_id(channel) client.max_id(channel) end |
#new_connection ⇒ Object
228 229 230 |
# File 'lib/message_bus/backends/postgres.rb', line 228 def new_connection MessageBus::Postgres::Client.new(@config) end |
#postgresql_channel_name ⇒ Object
240 241 242 243 |
# File 'lib/message_bus/backends/postgres.rb', line 240 def postgresql_channel_name db = @config[:db] || 0 "_message_bus_#{db}" end |
#process_global_backlog(highest_id) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/message_bus/backends/postgres.rb', line 307 def process_global_backlog(highest_id) if highest_id > client.max_id highest_id = 0 end global_backlog(highest_id).each do |old| yield old highest_id = old.global_id end highest_id end |
#publish(channel, data, queue_in_memory = true) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/message_bus/backends/postgres.rb', line 254 def publish(channel, data, queue_in_memory=true) client = self.client backlog_id = client.add(channel, data) msg = MessageBus::Message.new backlog_id, backlog_id, channel, data payload = msg.encode client.publish postgresql_channel_name, payload if backlog_id % clear_every == 0 client.clear_global_backlog(backlog_id, @max_global_backlog_size) client.expire(@max_backlog_age) client.clear_channel_backlog(channel, backlog_id, @max_backlog_size) end backlog_id end |
#reset! ⇒ Object
use with extreme care, will nuke all of the data
250 251 252 |
# File 'lib/message_bus/backends/postgres.rb', line 250 def reset! client.reset! end |
#subscribe(channel, last_id = nil) ⇒ Object
297 298 299 300 301 302 303 304 305 |
# File 'lib/message_bus/backends/postgres.rb', line 297 def subscribe(channel, last_id = nil) # trivial implementation for now, # can cut down on connections if we only have one global subscriber raise ArgumentError unless block_given? global_subscribe(last_id) do |m| yield m if m.channel == channel end end |