Class: MessageBus::Postgres::ReliablePubSub

Inherits:
Object
  • Object
show all
Defined in:
lib/message_bus/backends/postgres.rb

Constant Summary collapse

UNSUB_MESSAGE =
"$$UNSUBSCRIBE"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_everyObject

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_ageObject

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_sizeObject

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_sizeObject

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

#subscribedObject (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_forkObject



236
237
238
# File 'lib/message_bus/backends/postgres.rb', line 236

def after_fork
  client.reconnect
end

#backendObject



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

#clientObject



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 get_message(channel, message_id)
  if data = client.get_value(channel, message_id)
    MessageBus::Message.new message_id, message_id, 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

Raises:

  • (ArgumentError)


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.message 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_unsubscribeObject



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_connectionObject



228
229
230
# File 'lib/message_bus/backends/postgres.rb', line 228

def new_connection
  MessageBus::Postgres::Client.new(@config)
end

#postgresql_channel_nameObject



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

Raises:

  • (ArgumentError)


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