typed_bus 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +158 -4
- data/LICENSE +21 -0
- data/README.md +25 -13
- data/lib/typed_bus/channel.rb +170 -91
- data/lib/typed_bus/configuration.rb +51 -8
- data/lib/typed_bus/dead_letter_queue.rb +32 -8
- data/lib/typed_bus/delivery.rb +1 -1
- data/lib/typed_bus/delivery_tracker.rb +28 -19
- data/lib/typed_bus/message_bus.rb +55 -14
- data/lib/typed_bus/version.rb +1 -1
- data/lib/typed_bus.rb +3 -0
- metadata +23 -62
- data/.envrc +0 -1
- data/.github/workflows/deploy-github-pages.yml +0 -52
- data/.github/workflows/deploy-yard-docs.yml +0 -52
- data/.irbrc +0 -10
- data/COMMITS.md +0 -196
- data/Rakefile +0 -12
- data/docs/api/channel.md +0 -94
- data/docs/api/configuration.md +0 -90
- data/docs/api/dead-letter-queue.md +0 -52
- data/docs/api/delivery-tracker.md +0 -66
- data/docs/api/delivery.md +0 -73
- data/docs/api/index.md +0 -45
- data/docs/api/message-bus.md +0 -101
- data/docs/api/stats.md +0 -50
- data/docs/architecture/concurrency-model.md +0 -38
- data/docs/architecture/configuration-cascade.md +0 -76
- data/docs/architecture/index.md +0 -7
- data/docs/architecture/publish-flow.md +0 -67
- data/docs/assets/css/custom.css +0 -11
- data/docs/assets/images/typed_bus.gif +0 -0
- data/docs/assets/images/typed_bus.jpg +0 -0
- data/docs/assets/images/typed_bus.mp4 +0 -0
- data/docs/concepts.md +0 -47
- data/docs/examples/index.md +0 -37
- data/docs/getting-started/configuration.md +0 -87
- data/docs/getting-started/index.md +0 -7
- data/docs/getting-started/installation.md +0 -33
- data/docs/getting-started/quick-start.md +0 -51
- data/docs/guides/adaptive-throttling.md +0 -76
- data/docs/guides/backpressure.md +0 -58
- data/docs/guides/dead-letter-queues.md +0 -78
- data/docs/guides/delivery-and-ack.md +0 -89
- data/docs/guides/error-handling.md +0 -91
- data/docs/guides/graceful-shutdown.md +0 -75
- data/docs/guides/index.md +0 -13
- data/docs/guides/logging.md +0 -93
- data/docs/guides/stats-and-monitoring.md +0 -75
- data/docs/guides/typed-channels.md +0 -63
- data/docs/index.md +0 -56
- data/examples/00_config.rb +0 -156
- data/examples/01_basic_usage.rb +0 -26
- data/examples/02_typed_channels.rb +0 -41
- data/examples/03_multiple_subscribers.rb +0 -44
- data/examples/04_nack_and_dead_letters.rb +0 -47
- data/examples/05_delivery_timeout.rb +0 -44
- data/examples/06_backpressure.rb +0 -32
- data/examples/07_message_bus.rb +0 -55
- data/examples/08_error_handling.rb +0 -82
- data/examples/09_stats_and_monitoring.rb +0 -71
- data/examples/10_concurrent_pipeline.rb +0 -116
- data/examples/11_logging_basic.rb +0 -60
- data/examples/12_logging_failures.rb +0 -65
- data/examples/13_logging_backpressure.rb +0 -56
- data/examples/14_adaptive_throttling.rb +0 -101
- data/examples/README.md +0 -27
- data/examples/temp.md +0 -95
- data/mkdocs.yml +0 -183
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 03 — Multiple Subscribers (Fan-Out)
|
|
5
|
-
#
|
|
6
|
-
# Every subscriber receives its own Delivery for each published message.
|
|
7
|
-
# A message is only considered "delivered" when ALL subscribers ack.
|
|
8
|
-
|
|
9
|
-
require_relative "../lib/typed_bus"
|
|
10
|
-
|
|
11
|
-
channel = TypedBus::Channel.new(:notifications, timeout: 5)
|
|
12
|
-
|
|
13
|
-
# Subscriber A — email sender
|
|
14
|
-
channel.subscribe do |delivery|
|
|
15
|
-
puts "[Email] Sending notification: #{delivery.message}"
|
|
16
|
-
delivery.ack!
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# Subscriber B — push notification
|
|
20
|
-
channel.subscribe do |delivery|
|
|
21
|
-
puts "[Push] Sending notification: #{delivery.message}"
|
|
22
|
-
delivery.ack!
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Subscriber C — audit log
|
|
26
|
-
channel.subscribe do |delivery|
|
|
27
|
-
puts "[Audit] Logging: #{delivery.message}"
|
|
28
|
-
delivery.ack!
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
puts "Subscribers: #{channel.subscriber_count}"
|
|
32
|
-
puts
|
|
33
|
-
|
|
34
|
-
Async do
|
|
35
|
-
tracker = channel.publish("User signed in")
|
|
36
|
-
|
|
37
|
-
# Yield to let subscriber fibers run and ack
|
|
38
|
-
sleep(0)
|
|
39
|
-
|
|
40
|
-
puts
|
|
41
|
-
puts "Fully delivered? #{tracker.fully_delivered?}"
|
|
42
|
-
puts "All resolved? #{tracker.fully_resolved?}"
|
|
43
|
-
puts "Pending count: #{tracker.pending_count}"
|
|
44
|
-
end
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 04 — Explicit NACK and Dead Letter Queue
|
|
5
|
-
#
|
|
6
|
-
# Subscribers can reject messages with nack!. Rejected deliveries are
|
|
7
|
-
# routed to the channel's dead letter queue for later inspection or retry.
|
|
8
|
-
|
|
9
|
-
require_relative "../lib/typed_bus"
|
|
10
|
-
|
|
11
|
-
channel = TypedBus::Channel.new(:orders, timeout: 5)
|
|
12
|
-
|
|
13
|
-
channel.subscribe do |delivery|
|
|
14
|
-
order = delivery.message
|
|
15
|
-
|
|
16
|
-
if order[:total] > 1000
|
|
17
|
-
puts "REJECTED order ##{order[:id]} — amount $#{order[:total]} exceeds limit"
|
|
18
|
-
delivery.nack!
|
|
19
|
-
else
|
|
20
|
-
puts "Processed order ##{order[:id]} — $#{order[:total]}"
|
|
21
|
-
delivery.ack!
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
Async do
|
|
26
|
-
channel.publish({ id: 1, total: 50 })
|
|
27
|
-
channel.publish({ id: 2, total: 2500 })
|
|
28
|
-
channel.publish({ id: 3, total: 150 })
|
|
29
|
-
channel.publish({ id: 4, total: 9999 })
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
puts
|
|
33
|
-
puts "--- Dead Letter Queue (#{channel.dead_letter_queue.size} entries) ---"
|
|
34
|
-
|
|
35
|
-
channel.dead_letter_queue.each do |dead|
|
|
36
|
-
puts " Failed: order ##{dead.message[:id]} ($#{dead.message[:total]})"
|
|
37
|
-
puts " channel: #{dead.channel_name}, nacked: #{dead.nacked?}, timed_out: #{dead.timed_out?}"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Drain removes entries from the DLQ while yielding them
|
|
41
|
-
puts
|
|
42
|
-
puts "Draining DLQ for retry..."
|
|
43
|
-
channel.dead_letter_queue.drain do |dead|
|
|
44
|
-
puts " Retrying order ##{dead.message[:id]}..."
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
puts "DLQ now empty: #{channel.dead_letter_queue.empty?}"
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 05 — Delivery Timeout
|
|
5
|
-
#
|
|
6
|
-
# Each delivery has a deadline. If a subscriber doesn't ack or nack
|
|
7
|
-
# within the timeout period, the delivery auto-nacks and lands in the DLQ.
|
|
8
|
-
# The timed_out? flag distinguishes timeouts from explicit nacks.
|
|
9
|
-
|
|
10
|
-
require_relative "../lib/typed_bus"
|
|
11
|
-
|
|
12
|
-
# Short timeout for demonstration
|
|
13
|
-
channel = TypedBus::Channel.new(:tasks, timeout: 0.3)
|
|
14
|
-
|
|
15
|
-
channel.subscribe do |delivery|
|
|
16
|
-
task = delivery.message
|
|
17
|
-
|
|
18
|
-
case task[:type]
|
|
19
|
-
when :fast
|
|
20
|
-
puts "[#{task[:name]}] Processing quickly..."
|
|
21
|
-
delivery.ack!
|
|
22
|
-
when :slow
|
|
23
|
-
puts "[#{task[:name]}] Starting long operation... (will exceed timeout)"
|
|
24
|
-
# Simulates work that takes longer than the 0.3s timeout.
|
|
25
|
-
# The subscriber never acks — the timeout will auto-nack.
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
Async do
|
|
30
|
-
channel.publish({ type: :fast, name: "compile" })
|
|
31
|
-
channel.publish({ type: :slow, name: "deploy" })
|
|
32
|
-
channel.publish({ type: :fast, name: "notify" })
|
|
33
|
-
channel.publish({ type: :slow, name: "backup" })
|
|
34
|
-
|
|
35
|
-
# Wait for timeouts to fire
|
|
36
|
-
sleep(0.5)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
puts
|
|
40
|
-
puts "--- Dead Letter Queue ---"
|
|
41
|
-
channel.dead_letter_queue.each do |dead|
|
|
42
|
-
status = dead.timed_out? ? "TIMED OUT" : "NACKED"
|
|
43
|
-
puts " [#{status}] #{dead.message[:name]}"
|
|
44
|
-
end
|
data/examples/06_backpressure.rb
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 06 — Bounded Channels & Backpressure
|
|
5
|
-
#
|
|
6
|
-
# Setting max_pending limits how many unresolved deliveries can exist.
|
|
7
|
-
# When the limit is reached, the publishing fiber blocks until a delivery
|
|
8
|
-
# resolves. This prevents fast producers from overwhelming slow consumers.
|
|
9
|
-
|
|
10
|
-
require_relative "../lib/typed_bus"
|
|
11
|
-
|
|
12
|
-
# Only 2 deliveries can be in-flight at once
|
|
13
|
-
channel = TypedBus::Channel.new(:pipeline, timeout: 5, max_pending: 2)
|
|
14
|
-
|
|
15
|
-
channel.subscribe do |delivery|
|
|
16
|
-
item = delivery.message
|
|
17
|
-
puts " [consumer] Processing #{item}... (pending: #{channel.pending_count})"
|
|
18
|
-
sleep(0.1) # simulate work
|
|
19
|
-
delivery.ack!
|
|
20
|
-
puts " [consumer] Done with #{item}"
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
Async do
|
|
24
|
-
5.times do |i|
|
|
25
|
-
puts "[producer] Publishing item_#{i} (pending before: #{channel.pending_count})"
|
|
26
|
-
channel.publish("item_#{i}")
|
|
27
|
-
puts "[producer] Published item_#{i}"
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
puts
|
|
32
|
-
puts "All items processed. Pending: #{channel.pending_count}"
|
data/examples/07_message_bus.rb
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 07 — MessageBus Facade
|
|
5
|
-
#
|
|
6
|
-
# MessageBus is a registry that manages multiple named channels.
|
|
7
|
-
# It provides a single entry point for publish, subscribe, stats,
|
|
8
|
-
# dead letter access, and lifecycle management.
|
|
9
|
-
|
|
10
|
-
require_relative "../lib/typed_bus"
|
|
11
|
-
|
|
12
|
-
UserEvent = Data.define(:action, :user)
|
|
13
|
-
OrderEvent = Data.define(:action, :order_id, :total)
|
|
14
|
-
|
|
15
|
-
bus = TypedBus::MessageBus.new
|
|
16
|
-
|
|
17
|
-
# Register typed channels
|
|
18
|
-
bus.add_channel(:users, type: UserEvent, timeout: 5)
|
|
19
|
-
bus.add_channel(:orders, type: OrderEvent, timeout: 5)
|
|
20
|
-
|
|
21
|
-
# Subscribe to user events
|
|
22
|
-
bus.subscribe(:users) do |delivery|
|
|
23
|
-
event = delivery.message
|
|
24
|
-
puts "[Users] #{event.action}: #{event.user}"
|
|
25
|
-
delivery.ack!
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# Subscribe to order events — two subscribers
|
|
29
|
-
bus.subscribe(:orders) do |delivery|
|
|
30
|
-
event = delivery.message
|
|
31
|
-
puts "[Orders] #{event.action}: order ##{event.order_id} ($#{event.total})"
|
|
32
|
-
delivery.ack!
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
bus.subscribe(:orders) do |delivery|
|
|
36
|
-
event = delivery.message
|
|
37
|
-
puts "[Audit] #{event.action}: order ##{event.order_id}"
|
|
38
|
-
delivery.ack!
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
puts "Channels: #{bus.channel_names.inspect}"
|
|
42
|
-
puts
|
|
43
|
-
|
|
44
|
-
Async do
|
|
45
|
-
bus.publish(:users, UserEvent.new(action: "registered", user: "alice"))
|
|
46
|
-
bus.publish(:users, UserEvent.new(action: "logged_in", user: "bob"))
|
|
47
|
-
bus.publish(:orders, OrderEvent.new(action: "placed", order_id: 101, total: 49.99))
|
|
48
|
-
bus.publish(:orders, OrderEvent.new(action: "shipped", order_id: 101, total: 49.99))
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
puts
|
|
52
|
-
puts "--- Stats ---"
|
|
53
|
-
bus.stats.to_h.each do |key, value|
|
|
54
|
-
puts " #{key}: #{value}"
|
|
55
|
-
end
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 08 — Error Handling & Lifecycle
|
|
5
|
-
#
|
|
6
|
-
# Demonstrates how the library handles edge cases:
|
|
7
|
-
# - Subscriber exceptions are caught and auto-nacked
|
|
8
|
-
# - Publishing to a closed channel raises
|
|
9
|
-
# - Unsubscribing removes a subscriber mid-stream
|
|
10
|
-
# - Publishing with no subscribers routes to DLQ
|
|
11
|
-
# - close force-nacks all pending deliveries
|
|
12
|
-
|
|
13
|
-
require_relative "../lib/typed_bus"
|
|
14
|
-
|
|
15
|
-
bus = TypedBus::MessageBus.new
|
|
16
|
-
bus.add_channel(:events, timeout: 1)
|
|
17
|
-
|
|
18
|
-
# --- Subscriber errors are caught ---
|
|
19
|
-
|
|
20
|
-
puts "=== Subscriber Errors ==="
|
|
21
|
-
bus.subscribe(:events) do |delivery|
|
|
22
|
-
raise "database connection lost!" if delivery.message == "crash"
|
|
23
|
-
|
|
24
|
-
puts " Processed: #{delivery.message}"
|
|
25
|
-
delivery.ack!
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
Async do
|
|
29
|
-
bus.publish(:events, "good message")
|
|
30
|
-
bus.publish(:events, "crash")
|
|
31
|
-
bus.publish(:events, "still works")
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
puts " DLQ after errors: #{bus.dead_letters(:events).size} entries"
|
|
35
|
-
puts
|
|
36
|
-
|
|
37
|
-
# --- Publishing with no subscribers ---
|
|
38
|
-
|
|
39
|
-
puts "=== No Subscribers ==="
|
|
40
|
-
bus.add_channel(:orphan, timeout: 5)
|
|
41
|
-
|
|
42
|
-
Async { bus.publish(:orphan, "nobody home") }
|
|
43
|
-
|
|
44
|
-
puts " DLQ: #{bus.dead_letters(:orphan).size} (message had no subscribers)"
|
|
45
|
-
puts
|
|
46
|
-
|
|
47
|
-
# --- Unsubscribe ---
|
|
48
|
-
|
|
49
|
-
puts "=== Unsubscribe ==="
|
|
50
|
-
bus.add_channel(:dynamic, timeout: 5)
|
|
51
|
-
|
|
52
|
-
received = []
|
|
53
|
-
sub_id = bus.subscribe(:dynamic) { |d| received << d.message; d.ack! }
|
|
54
|
-
|
|
55
|
-
Async { bus.publish(:dynamic, "before unsub") }
|
|
56
|
-
bus.unsubscribe(:dynamic, sub_id)
|
|
57
|
-
Async { bus.publish(:dynamic, "after unsub") }
|
|
58
|
-
|
|
59
|
-
puts " Received: #{received.inspect}"
|
|
60
|
-
puts " DLQ: #{bus.dead_letters(:dynamic).size} (second message had no subscriber)"
|
|
61
|
-
puts
|
|
62
|
-
|
|
63
|
-
# --- Close force-nacks pending deliveries ---
|
|
64
|
-
|
|
65
|
-
puts "=== Channel Close ==="
|
|
66
|
-
bus.add_channel(:closeable, timeout: 5)
|
|
67
|
-
bus.subscribe(:closeable) { |_d| } # never acks
|
|
68
|
-
|
|
69
|
-
Async do
|
|
70
|
-
bus.publish(:closeable, "will be force-nacked")
|
|
71
|
-
puts " Pending before close: #{bus.pending_count(:closeable)}"
|
|
72
|
-
bus.close(:closeable)
|
|
73
|
-
puts " Pending after close: #{bus.pending_count(:closeable)}"
|
|
74
|
-
puts " DLQ after close: #{bus.dead_letters(:closeable).size}"
|
|
75
|
-
|
|
76
|
-
# Publishing to a closed channel raises
|
|
77
|
-
begin
|
|
78
|
-
bus.publish(:closeable, "too late")
|
|
79
|
-
rescue RuntimeError => e
|
|
80
|
-
puts " Publish error: #{e.message}"
|
|
81
|
-
end
|
|
82
|
-
end
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 09 — Stats & Monitoring
|
|
5
|
-
#
|
|
6
|
-
# The MessageBus tracks per-channel counters automatically:
|
|
7
|
-
# - :channel_published — messages sent
|
|
8
|
-
# - :channel_delivered — all subscribers acked
|
|
9
|
-
# - :channel_nacked — explicitly rejected
|
|
10
|
-
# - :channel_timed_out — deadline exceeded
|
|
11
|
-
# - :channel_dead_lettered — routed to DLQ (nack + timeout + no-subscriber)
|
|
12
|
-
#
|
|
13
|
-
# Combine with DLQ callbacks for real-time alerting.
|
|
14
|
-
|
|
15
|
-
require_relative "../lib/typed_bus"
|
|
16
|
-
|
|
17
|
-
bus = TypedBus::MessageBus.new
|
|
18
|
-
bus.add_channel(:payments, timeout: 0.2)
|
|
19
|
-
|
|
20
|
-
# DLQ callback for alerting
|
|
21
|
-
bus.dead_letters(:payments).on_dead_letter do |delivery|
|
|
22
|
-
status = delivery.timed_out? ? "TIMEOUT" : "REJECTED"
|
|
23
|
-
$stderr.puts " ** ALERT ** [#{status}] payment failed: #{delivery.message.inspect}"
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# Subscriber that rejects large payments and ignores some (timeout)
|
|
27
|
-
bus.subscribe(:payments) do |delivery|
|
|
28
|
-
amount = delivery.message[:amount]
|
|
29
|
-
|
|
30
|
-
if amount > 500
|
|
31
|
-
delivery.nack!
|
|
32
|
-
elsif amount < 10
|
|
33
|
-
# "forget" to ack — will timeout
|
|
34
|
-
else
|
|
35
|
-
delivery.ack!
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
puts "Processing payments..."
|
|
40
|
-
puts
|
|
41
|
-
|
|
42
|
-
Async do
|
|
43
|
-
[25, 750, 5, 100, 1200, 8, 50].each do |amount|
|
|
44
|
-
bus.publish(:payments, { id: rand(1000..9999), amount: amount })
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Wait for timeouts
|
|
48
|
-
sleep(0.4)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
puts
|
|
52
|
-
puts "--- Payment Channel Stats ---"
|
|
53
|
-
|
|
54
|
-
stats = bus.stats
|
|
55
|
-
%i[published delivered nacked timed_out dead_lettered].each do |metric|
|
|
56
|
-
key = :"payments_#{metric}"
|
|
57
|
-
puts " #{metric}: #{stats[key]}"
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
puts
|
|
61
|
-
puts "--- DLQ Contents (#{bus.dead_letters(:payments).size} entries) ---"
|
|
62
|
-
|
|
63
|
-
bus.dead_letters(:payments).each do |dead|
|
|
64
|
-
reason = dead.timed_out? ? "timeout" : "nack"
|
|
65
|
-
puts " Payment ##{dead.message[:id]}: $#{dead.message[:amount]} (#{reason})"
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# Stats can be reset for a new monitoring window
|
|
69
|
-
bus.stats.reset!
|
|
70
|
-
puts
|
|
71
|
-
puts "Stats after reset: #{bus.stats.to_h.inspect}"
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 10 — Concurrent Pipeline
|
|
5
|
-
#
|
|
6
|
-
# Combines everything into a realistic scenario: an event-driven pipeline
|
|
7
|
-
# with multiple stages, typed messages, backpressure, timeout handling,
|
|
8
|
-
# DLQ monitoring, and graceful shutdown.
|
|
9
|
-
#
|
|
10
|
-
# Pipeline:
|
|
11
|
-
# [Ingest] -> :raw_events -> [Validate] -> :validated -> [Process] -> done
|
|
12
|
-
# | |
|
|
13
|
-
# nack timeout
|
|
14
|
-
# v v
|
|
15
|
-
# DLQ DLQ
|
|
16
|
-
|
|
17
|
-
require_relative "../lib/typed_bus"
|
|
18
|
-
|
|
19
|
-
RawEvent = Data.define(:source, :payload, :timestamp)
|
|
20
|
-
ValidatedEvent = Data.define(:source, :payload, :validated_at)
|
|
21
|
-
|
|
22
|
-
bus = TypedBus::MessageBus.new
|
|
23
|
-
|
|
24
|
-
bus.add_channel(:raw_events, type: RawEvent, timeout: 1, max_pending: 3)
|
|
25
|
-
bus.add_channel(:validated, type: ValidatedEvent, timeout: 0.3)
|
|
26
|
-
|
|
27
|
-
# --- Stage 1: Validation ---
|
|
28
|
-
|
|
29
|
-
bus.subscribe(:raw_events) do |delivery|
|
|
30
|
-
event = delivery.message
|
|
31
|
-
|
|
32
|
-
if event.payload.nil? || event.payload.empty?
|
|
33
|
-
puts " [Validate] REJECTED empty payload from #{event.source}"
|
|
34
|
-
delivery.nack!
|
|
35
|
-
else
|
|
36
|
-
puts " [Validate] OK: #{event.source} -> #{event.payload}"
|
|
37
|
-
delivery.ack!
|
|
38
|
-
|
|
39
|
-
# Forward to next stage
|
|
40
|
-
validated = ValidatedEvent.new(
|
|
41
|
-
source: event.source,
|
|
42
|
-
payload: event.payload,
|
|
43
|
-
validated_at: Time.now
|
|
44
|
-
)
|
|
45
|
-
bus.publish(:validated, validated)
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# --- Stage 2: Processing ---
|
|
50
|
-
|
|
51
|
-
bus.subscribe(:validated) do |delivery|
|
|
52
|
-
event = delivery.message
|
|
53
|
-
|
|
54
|
-
# Simulate slow processing for some events
|
|
55
|
-
if event.payload.include?("slow")
|
|
56
|
-
puts " [Process] Starting slow work: #{event.payload}..."
|
|
57
|
-
sleep(0.5) # exceeds 0.3s timeout
|
|
58
|
-
# Will never reach ack! — timeout fires first
|
|
59
|
-
else
|
|
60
|
-
puts " [Process] Completed: #{event.payload}"
|
|
61
|
-
delivery.ack!
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
# --- DLQ monitoring ---
|
|
66
|
-
|
|
67
|
-
%i[raw_events validated].each do |ch|
|
|
68
|
-
bus.dead_letters(ch).on_dead_letter do |dead|
|
|
69
|
-
reason = dead.timed_out? ? "TIMEOUT" : "NACK"
|
|
70
|
-
puts " [DLQ:#{ch}] #{reason}: #{dead.message.inspect}"
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# --- Produce events ---
|
|
75
|
-
|
|
76
|
-
puts "=== Pipeline Start ==="
|
|
77
|
-
puts
|
|
78
|
-
|
|
79
|
-
events = [
|
|
80
|
-
RawEvent.new(source: "web", payload: "user.signup", timestamp: Time.now),
|
|
81
|
-
RawEvent.new(source: "api", payload: "", timestamp: Time.now),
|
|
82
|
-
RawEvent.new(source: "mobile", payload: "order.placed", timestamp: Time.now),
|
|
83
|
-
RawEvent.new(source: "web", payload: "slow.export", timestamp: Time.now),
|
|
84
|
-
RawEvent.new(source: "api", payload: "user.login", timestamp: Time.now),
|
|
85
|
-
RawEvent.new(source: "batch", payload: nil.to_s, timestamp: Time.now),
|
|
86
|
-
]
|
|
87
|
-
|
|
88
|
-
Async do
|
|
89
|
-
events.each_with_index do |event, i|
|
|
90
|
-
puts "[Ingest ##{i}] #{event.source}: #{event.payload.inspect}"
|
|
91
|
-
bus.publish(:raw_events, event)
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# Wait for timeouts to resolve
|
|
95
|
-
sleep(0.6)
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
# --- Final report ---
|
|
99
|
-
|
|
100
|
-
puts
|
|
101
|
-
puts "=== Pipeline Report ==="
|
|
102
|
-
puts
|
|
103
|
-
|
|
104
|
-
stats = bus.stats
|
|
105
|
-
%i[raw_events validated].each do |ch|
|
|
106
|
-
puts "#{ch}:"
|
|
107
|
-
%i[published delivered nacked timed_out dead_lettered].each do |m|
|
|
108
|
-
val = stats[:"#{ch}_#{m}"]
|
|
109
|
-
puts " #{m}: #{val}" if val > 0
|
|
110
|
-
end
|
|
111
|
-
puts " DLQ size: #{bus.dead_letters(ch).size}"
|
|
112
|
-
puts
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
bus.close_all
|
|
116
|
-
puts "All channels closed."
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 11 — Basic Logging
|
|
5
|
-
#
|
|
6
|
-
# Shows how to enable logging by assigning a standard Ruby Logger to
|
|
7
|
-
# TypedBus.logger. Every lifecycle event is logged: channel creation,
|
|
8
|
-
# subscriber registration, message publish, delivery notification, ack/nack,
|
|
9
|
-
# and tracker resolution.
|
|
10
|
-
|
|
11
|
-
require "logger"
|
|
12
|
-
require_relative "../lib/typed_bus"
|
|
13
|
-
|
|
14
|
-
# Configure a colorized logger writing to STDOUT
|
|
15
|
-
logger = Logger.new($stdout)
|
|
16
|
-
logger.level = Logger::DEBUG
|
|
17
|
-
logger.formatter = proc do |severity, datetime, _progname, msg|
|
|
18
|
-
timestamp = datetime.strftime("%H:%M:%S.%L")
|
|
19
|
-
"#{timestamp} [#{severity.ljust(5)}] #{msg}\n"
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
TypedBus.logger = logger
|
|
23
|
-
|
|
24
|
-
# --- Set up a bus with two channels ---
|
|
25
|
-
|
|
26
|
-
bus = TypedBus::MessageBus.new
|
|
27
|
-
bus.add_channel(:orders, type: String, timeout: 2)
|
|
28
|
-
bus.add_channel(:alerts, timeout: 2)
|
|
29
|
-
|
|
30
|
-
# --- Register subscribers ---
|
|
31
|
-
|
|
32
|
-
bus.subscribe(:orders) do |delivery|
|
|
33
|
-
sleep 0.05 # simulate work
|
|
34
|
-
delivery.ack!
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
bus.subscribe(:orders) do |delivery|
|
|
38
|
-
sleep 0.1
|
|
39
|
-
delivery.ack!
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
bus.subscribe(:alerts) do |delivery|
|
|
43
|
-
delivery.ack!
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# --- Publish messages ---
|
|
47
|
-
|
|
48
|
-
Async do
|
|
49
|
-
bus.publish(:orders, "Order #1001")
|
|
50
|
-
bus.publish(:orders, "Order #1002")
|
|
51
|
-
bus.publish(:alerts, "System healthy")
|
|
52
|
-
sleep 0.3 # let deliveries settle
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
puts
|
|
56
|
-
puts "--- Stats ---"
|
|
57
|
-
bus.stats.to_h.each { |k, v| puts " #{k}: #{v}" }
|
|
58
|
-
|
|
59
|
-
# Clean up
|
|
60
|
-
TypedBus.logger = nil
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 12 — Logging Failures: NACKs, Timeouts, and Dead Letter Queue
|
|
5
|
-
#
|
|
6
|
-
# Demonstrates log output for failure scenarios:
|
|
7
|
-
# - Explicit nack! by a subscriber
|
|
8
|
-
# - Delivery timeout (subscriber never responds)
|
|
9
|
-
# - Publishing with no subscribers (auto dead-letter)
|
|
10
|
-
# - Dead letter queue drain
|
|
11
|
-
|
|
12
|
-
require "logger"
|
|
13
|
-
require_relative "../lib/typed_bus"
|
|
14
|
-
|
|
15
|
-
logger = Logger.new($stdout)
|
|
16
|
-
logger.level = Logger::DEBUG
|
|
17
|
-
logger.formatter = proc do |severity, datetime, _progname, msg|
|
|
18
|
-
timestamp = datetime.strftime("%H:%M:%S.%L")
|
|
19
|
-
color = case severity
|
|
20
|
-
when "WARN" then "\e[33m" # yellow
|
|
21
|
-
when "ERROR" then "\e[31m" # red
|
|
22
|
-
when "DEBUG" then "\e[36m" # cyan
|
|
23
|
-
else "\e[0m"
|
|
24
|
-
end
|
|
25
|
-
"#{color}#{timestamp} [#{severity.ljust(5)}]\e[0m #{msg}\n"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
TypedBus.logger = logger
|
|
29
|
-
|
|
30
|
-
bus = TypedBus::MessageBus.new
|
|
31
|
-
|
|
32
|
-
# Short timeout so the demo runs quickly
|
|
33
|
-
bus.add_channel(:jobs, type: String, timeout: 0.3)
|
|
34
|
-
|
|
35
|
-
# Subscriber that always rejects
|
|
36
|
-
bus.subscribe(:jobs) do |delivery|
|
|
37
|
-
delivery.nack!
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Subscriber that never responds (will timeout)
|
|
41
|
-
bus.subscribe(:jobs) do |_delivery|
|
|
42
|
-
# intentionally doing nothing — delivery will time out
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
Async do
|
|
46
|
-
puts "=== Publishing to channel with nack + timeout subscribers ==="
|
|
47
|
-
bus.publish(:jobs, "Job A")
|
|
48
|
-
sleep 0.5 # wait for timeout
|
|
49
|
-
|
|
50
|
-
puts
|
|
51
|
-
puts "=== Publishing to channel with no subscribers ==="
|
|
52
|
-
bus.remove_channel(:jobs)
|
|
53
|
-
bus.add_channel(:empty_channel, timeout: 0.3)
|
|
54
|
-
bus.publish(:empty_channel, "Lost message")
|
|
55
|
-
|
|
56
|
-
puts
|
|
57
|
-
puts "=== Draining dead letter queue ==="
|
|
58
|
-
dlq = bus.dead_letters(:empty_channel)
|
|
59
|
-
dlq.drain do |dead|
|
|
60
|
-
puts " Drained: channel=:#{dead.channel_name} message=#{dead.message.inspect}"
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
# Clean up
|
|
65
|
-
TypedBus.logger = nil
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# 13 — Logging with Backpressure
|
|
5
|
-
#
|
|
6
|
-
# Demonstrates log output when a bounded channel applies backpressure.
|
|
7
|
-
# The publisher fiber blocks when max_pending is reached, then resumes
|
|
8
|
-
# as subscribers drain the queue. Watch the DEBUG-level backpressure
|
|
9
|
-
# messages interleave with publish/ack logs.
|
|
10
|
-
|
|
11
|
-
require "logger"
|
|
12
|
-
require_relative "../lib/typed_bus"
|
|
13
|
-
|
|
14
|
-
logger = Logger.new($stdout)
|
|
15
|
-
logger.level = Logger::DEBUG
|
|
16
|
-
logger.formatter = proc do |severity, datetime, _progname, msg|
|
|
17
|
-
timestamp = datetime.strftime("%H:%M:%S.%L")
|
|
18
|
-
color = case severity
|
|
19
|
-
when "WARN" then "\e[33m"
|
|
20
|
-
when "ERROR" then "\e[31m"
|
|
21
|
-
when "DEBUG" then "\e[36m"
|
|
22
|
-
when "INFO" then "\e[32m"
|
|
23
|
-
else "\e[0m"
|
|
24
|
-
end
|
|
25
|
-
"#{color}#{timestamp} [#{severity.ljust(5)}]\e[0m #{msg}\n"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
TypedBus.logger = logger
|
|
29
|
-
|
|
30
|
-
channel = TypedBus::Channel.new(
|
|
31
|
-
:pipeline,
|
|
32
|
-
type: Integer,
|
|
33
|
-
timeout: 2,
|
|
34
|
-
max_pending: 2
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
# Slow subscriber — takes 0.15s per message
|
|
38
|
-
channel.subscribe do |delivery|
|
|
39
|
-
sleep 0.15
|
|
40
|
-
delivery.ack!
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
Async do
|
|
44
|
-
5.times do |i|
|
|
45
|
-
puts "--- Publisher: sending message #{i + 1} ---"
|
|
46
|
-
channel.publish(i + 1)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
sleep 0.5 # let remaining deliveries settle
|
|
50
|
-
puts
|
|
51
|
-
puts "All messages processed."
|
|
52
|
-
channel.close
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Clean up
|
|
56
|
-
TypedBus.logger = nil
|