Class: Pgbus::Client

Inherits:
Object
  • Object
show all
Includes:
EnsureStreamQueue, ReadAfter
Defined in:
lib/pgbus/client.rb,
lib/pgbus/client/read_after.rb,
lib/pgbus/client/ensure_stream_queue.rb

Defined Under Namespace

Modules: EnsureStreamQueue, ReadAfter

Constant Summary collapse

NOTIFY_THROTTLE_MS =

Throttle window for PGMQ’s enable_notify_insert trigger. Postgres NOTIFYs are coalesced into one wake-up per window, so a value of 250ms means: at most 4 broadcasts/sec per queue, regardless of insert rate. The trigger is a Postgres-level concern; exposing it as a setting never came up in practice and changing it on the fly would require re-running the trigger DDL on every queue.

250

Constants included from ReadAfter

ReadAfter::DEFAULT_LIMIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnsureStreamQueue

#ensure_stream_queue

Methods included from ReadAfter

#read_after, #stream_current_msg_id, #stream_oldest_msg_id

Constructor Details

#initialize(config = Pgbus.configuration) ⇒ Client

Returns a new instance of Client.



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
55
56
57
58
59
60
# File 'lib/pgbus/client.rb', line 25

def initialize(config = Pgbus.configuration)
  # Define the PGMQ module before requiring the gem so that Zeitwerk's
  # eager_load (called inside pgmq.rb) can resolve the constant.
  # Without this, Ruby 4.0 + Zeitwerk 2.7.5 raises NameError because
  # eager_load runs const_get(:Client) on PGMQ before the module is defined.
  PGMQ_REQUIRE_MUTEX.synchronize do
    Object.const_set(:PGMQ, Module.new) unless defined?(::PGMQ)
    require "pgmq"
  end
  @config = config
  conn_opts = config.connection_options
  @shared_connection = conn_opts.is_a?(Proc)

  if @shared_connection
    # When using the Rails lambda path (-> { AR::Base.connection.raw_connection }),
    # the Proc returns the same underlying PG::Connection that ActiveRecord uses.
    # PG::Connection (libpq) is not thread-safe — concurrent access causes
    # segfaults and result corruption. Force pool_size=1 and serialize all
    # operations through a mutex.
    @pgmq = PGMQ::Client.new(conn_opts, pool_size: 1, pool_timeout: config.pool_timeout)
    @pgmq_mutex = Mutex.new
  else
    # With a String URL or Hash params, pgmq-ruby creates its own dedicated
    # PG::Connection per pool slot — no shared state with ActiveRecord.
    # Use the resolved pool size (auto-tuned from worker thread counts
    # unless explicitly set) and let pgmq-ruby's connection_pool handle
    # concurrency internally (no mutex needed).
    @pgmq = PGMQ::Client.new(conn_opts, pool_size: config.resolved_pool_size, pool_timeout: config.pool_timeout)
    @pgmq_mutex = nil
  end

  @queues_created = Concurrent::Map.new
  @stream_indexes_created = Concurrent::Map.new
  @queue_strategy = QueueFactory.for(config)
  @schema_ensured = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/pgbus/client.rb', line 12

def config
  @config
end

#pgmqObject (readonly)

Returns the value of attribute pgmq.



12
13
14
# File 'lib/pgbus/client.rb', line 12

def pgmq
  @pgmq
end

Instance Method Details

#archive_batch(queue_name, msg_ids, prefixed: true) ⇒ Object

Batch archive — moves multiple messages to the archive table in one call.



188
189
190
191
# File 'lib/pgbus/client.rb', line 188

def archive_batch(queue_name, msg_ids, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  synchronized { @pgmq.archive_batch(name, msg_ids) }
end

#archive_message(queue_name, msg_id, prefixed: true) ⇒ Object

Archive a message. Pass prefixed: false when queue_name is already the full PGMQ queue name.



182
183
184
185
# File 'lib/pgbus/client.rb', line 182

def archive_message(queue_name, msg_id, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  synchronized { @pgmq.archive(name, msg_id) }
end

#bind_topic(pattern, queue_name) ⇒ Object

Topic routing



314
315
316
317
318
# File 'lib/pgbus/client.rb', line 314

def bind_topic(pattern, queue_name)
  full_name = config.queue_name(queue_name)
  ensure_queue(queue_name)
  synchronized { @pgmq.bind_topic(pattern, full_name) }
end

#closeObject



331
332
333
# File 'lib/pgbus/client.rb', line 331

def close
  synchronized { @pgmq.close }
end

#delete_batch(queue_name, msg_ids, prefixed: true) ⇒ Object

Batch delete — permanently removes multiple messages in one call.



194
195
196
197
# File 'lib/pgbus/client.rb', line 194

def delete_batch(queue_name, msg_ids, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  synchronized { @pgmq.delete_batch(name, msg_ids) }
end

#delete_message(queue_name, msg_id, prefixed: true) ⇒ Object

Delete a message. Pass prefixed: false when queue_name is already the full PGMQ queue name (e.g. from priority sub-queues or dashboard).



175
176
177
178
# File 'lib/pgbus/client.rb', line 175

def delete_message(queue_name, msg_id, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  synchronized { @pgmq.delete(name, msg_id) }
end

#drop_queue(queue_name, prefixed: true) ⇒ Object



242
243
244
245
246
247
# File 'lib/pgbus/client.rb', line 242

def drop_queue(queue_name, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  result = synchronized { @pgmq.drop_queue(name) }
  @queues_created.delete(name)
  result
end

#ensure_all_queuesObject



67
68
69
70
71
# File 'lib/pgbus/client.rb', line 67

def ensure_all_queues
  queue_names = collect_configured_queues
  Pgbus.logger.info { "[Pgbus] Bootstrapping #{queue_names.size} queue(s): #{queue_names.join(", ")}" }
  queue_names.each { |name| ensure_queue(name) }
end

#ensure_dead_letter_queue(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pgbus/client.rb', line 73

def ensure_dead_letter_queue(name)
  dlq_name = config.dead_letter_queue_name(name)
  return if @queues_created[dlq_name]

  @queues_created.compute_if_absent(dlq_name) do
    synchronized do
      @pgmq.create(dlq_name)
      tune_autovacuum(dlq_name)
    end
    true
  end
end

#ensure_queue(name) ⇒ Object



62
63
64
65
# File 'lib/pgbus/client.rb', line 62

def ensure_queue(name)
  ensure_pgmq_schema
  @queue_strategy.physical_queue_names(name).each { |pq| ensure_single_queue(pq) }
end

#list_queuesObject



233
234
235
# File 'lib/pgbus/client.rb', line 233

def list_queues
  synchronized { @pgmq.list_queues }
end

#message_exists?(queue_name, msg_id: nil, uniqueness_key: nil) ⇒ Boolean

Check whether a message exists in the given queue.

Pass either msg_id for a fast primary-key lookup, or uniqueness_key to scan the queue for any message whose payload carries that key in the pgbus_uniqueness_key JSONB field. The latter is used by the dispatcher reaper to determine if a uniqueness lock with msg_id=0 (placeholder) still has a corresponding queue message.

queue_name may be either a logical name (e.g. “default”) or an already prefixed physical name (e.g. “pgbus_default”). The client normalizes both.

Returns:

true  — the message definitely exists in the queue
false — the message definitely does not exist
nil   — could not determine (e.g. queue table missing or unknown error).
        Callers MUST treat nil as "exists" for safety.

Returns:

  • (Boolean)


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/pgbus/client.rb', line 265

def message_exists?(queue_name, msg_id: nil, uniqueness_key: nil)
  has_msg_id = !msg_id.nil?
  has_uniqueness_key = !uniqueness_key.nil?
  raise ArgumentError, "pass exactly one of msg_id or uniqueness_key" unless has_msg_id ^ has_uniqueness_key

  full_name = resolve_full_queue_name(queue_name)
  sanitized = QueueNameValidator.sanitize!(full_name)

  synchronized do
    with_raw_connection do |conn|
      if has_msg_id
        msg_id_present?(conn, sanitized, msg_id.to_i)
      else
        uniqueness_key_present?(conn, sanitized, uniqueness_key)
      end
    end
  end
rescue ActiveRecord::StatementInvalid => e
  raise unless undefined_table_error?(e)

  nil
rescue StandardError => e
  raise unless defined?(PG::UndefinedTable) && e.is_a?(PG::UndefinedTable)

  nil
end

#metrics(queue_name = nil) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'lib/pgbus/client.rb', line 223

def metrics(queue_name = nil)
  synchronized do
    if queue_name
      @pgmq.metrics(config.queue_name(queue_name))
    else
      @pgmq.metrics_all
    end
  end
end

#move_to_dead_letter(queue_name, message) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pgbus/client.rb', line 210

def move_to_dead_letter(queue_name, message)
  ensure_dead_letter_queue(queue_name)
  dlq_name = config.dead_letter_queue_name(queue_name)
  full_queue = config.queue_name(queue_name)

  synchronized do
    @pgmq.transaction do |txn|
      txn.produce(dlq_name, message.message, headers: message.headers)
      txn.delete(full_queue, message.msg_id.to_i)
    end
  end
end

#publish_to_topic(routing_key, payload, headers: nil, delay: 0) ⇒ Object



320
321
322
323
324
325
326
327
328
329
# File 'lib/pgbus/client.rb', line 320

def publish_to_topic(routing_key, payload, headers: nil, delay: 0)
  synchronized do
    @pgmq.produce_topic(
      routing_key,
      serialize(payload),
      headers: headers && serialize(headers),
      delay: delay
    )
  end
end

#purge_archive(queue_name, older_than:, batch_size: 1000) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/pgbus/client.rb', line 292

def purge_archive(queue_name, older_than:, batch_size: 1000)
  full_name = config.queue_name(queue_name)
  sanitized = QueueNameValidator.sanitize!(full_name)
  total = 0

  sql = "DELETE FROM pgmq.a_#{sanitized} " \
        "WHERE ctid = ANY(ARRAY(SELECT ctid FROM pgmq.a_#{sanitized} WHERE enqueued_at < $1 LIMIT $2))"

  loop do
    deleted = synchronized do
      with_raw_connection do |conn|
        conn.exec_params(sql, [older_than, batch_size]).cmd_tuples
      end
    end
    total += deleted
    break if deleted < batch_size
  end

  total
end

#purge_queue(queue_name, prefixed: true) ⇒ Object



237
238
239
240
# File 'lib/pgbus/client.rb', line 237

def purge_queue(queue_name, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  synchronized { @pgmq.purge_queue(name) }
end

#read_batch(queue_name, qty:, vt: nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/pgbus/client.rb', line 110

def read_batch(queue_name, qty:, vt: nil)
  full_name = config.queue_name(queue_name)
  Instrumentation.instrument("pgbus.client.read_batch", queue: full_name, qty: qty) do
    synchronized { @pgmq.read_batch(full_name, vt: vt || config.visibility_timeout, qty: qty) }
  end
end

#read_batch_prioritized(queue_name, qty:, vt: nil) ⇒ Object

Read from priority sub-queues, highest priority (p0) first. Returns [priority_queue_name, messages] pairs.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pgbus/client.rb', line 119

def read_batch_prioritized(queue_name, qty:, vt: nil)
  unless @queue_strategy.priority?
    return (read_batch(queue_name, qty: qty, vt: vt) || []).map do |m|
      [config.queue_name(queue_name), m]
    end
  end

  remaining = qty
  results = []

  config.priority_queue_names(queue_name).each do |pq_name|
    break if remaining <= 0

    msgs = Instrumentation.instrument("pgbus.client.read_batch", queue: pq_name, qty: remaining) do
      synchronized { @pgmq.read_batch(pq_name, vt: vt || config.visibility_timeout, qty: remaining) }
    end || []

    msgs.each { |m| results << [pq_name, m] }
    remaining -= msgs.size
  end

  results
end

#read_message(queue_name, vt: nil) ⇒ Object



103
104
105
106
107
108
# File 'lib/pgbus/client.rb', line 103

def read_message(queue_name, vt: nil)
  full_name = config.queue_name(queue_name)
  Instrumentation.instrument("pgbus.client.read_message", queue: full_name) do
    synchronized { @pgmq.read(full_name, vt: vt || config.visibility_timeout) }
  end
end

#read_multi(queue_names, qty:, vt: nil, limit: nil) ⇒ Object

Read from multiple queues in a single SQL query (UNION ALL). Each returned message includes a queue_name field identifying its source. queue_names should be logical names (prefix is added automatically).

‘qty` is the per-queue cap (pgmq-ruby semantics), so without `limit:` the caller receives up to `queue_count * qty` messages. Pass `limit:` to cap the total across all queues — required when feeding a fixed-size pool, otherwise the pool can overflow on multi-queue reads (issue #123).



164
165
166
167
168
169
170
171
# File 'lib/pgbus/client.rb', line 164

def read_multi(queue_names, qty:, vt: nil, limit: nil)
  full_names = queue_names.map { |q| config.queue_name(q) }
  Instrumentation.instrument("pgbus.client.read_multi", queues: full_names, qty: qty, limit: limit) do
    synchronized do
      @pgmq.read_multi(full_names, vt: vt || config.visibility_timeout, qty: qty, limit: limit)
    end
  end
end

#read_with_poll(queue_name, qty:, vt: nil, max_poll_seconds: 5, poll_interval_ms: 100) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pgbus/client.rb', line 143

def read_with_poll(queue_name, qty:, vt: nil, max_poll_seconds: 5, poll_interval_ms: 100)
  full_name = config.queue_name(queue_name)
  synchronized do
    @pgmq.read_with_poll(
      full_name,
      vt: vt || config.visibility_timeout,
      qty: qty,
      max_poll_seconds: max_poll_seconds,
      poll_interval_ms: poll_interval_ms
    )
  end
end

#send_batch(queue_name, payloads, headers: nil, delay: 0) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/pgbus/client.rb', line 94

def send_batch(queue_name, payloads, headers: nil, delay: 0)
  full_name = config.queue_name(queue_name)
  ensure_queue(queue_name)
  serialized, serialized_headers = serialize_batch(payloads, headers)
  Instrumentation.instrument("pgbus.client.send_batch", queue: full_name, size: payloads.size) do
    synchronized { @pgmq.produce_batch(full_name, serialized, headers: serialized_headers, delay: delay) }
  end
end

#send_message(queue_name, payload, headers: nil, delay: 0, priority: nil) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/pgbus/client.rb', line 86

def send_message(queue_name, payload, headers: nil, delay: 0, priority: nil)
  target = @queue_strategy.target_queue(queue_name, priority)
  ensure_queue(queue_name)
  Instrumentation.instrument("pgbus.client.send_message", queue: target) do
    synchronized { @pgmq.produce(target, serialize(payload), headers: headers && serialize(headers), delay: delay) }
  end
end

#set_visibility_timeout(queue_name, msg_id, vt:, prefixed: true) ⇒ Object

Set visibility timeout. Pass prefixed: false when queue_name is already the full PGMQ queue name.



201
202
203
204
# File 'lib/pgbus/client.rb', line 201

def set_visibility_timeout(queue_name, msg_id, vt:, prefixed: true)
  name = prefixed ? config.queue_name(queue_name) : queue_name
  synchronized { @pgmq.set_vt(name, msg_id, vt: vt) }
end

#transaction(&block) ⇒ Object



206
207
208
# File 'lib/pgbus/client.rb', line 206

def transaction(&block)
  synchronized { @pgmq.transaction(&block) }
end