tiny_bus 3.7.2 → 3.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tiny_bus.rb +19 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429d6ba2f802f4f2bc9ad911848ce80bc8c32cc88036917014314056d585b0d3
|
4
|
+
data.tar.gz: 574ef955a90a8c5dcb5be27b360b7d4252f205aaaaa726d6324dcc279746fd94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6222ee3a89fd3c310aea779231757ec0c6e32eda7b7ffa5a80a593ff0ae6259ebeab5db3aab063aa1c132643e3acb73014c4fc04116dedc837a4724ac610dce0
|
7
|
+
data.tar.gz: f08b289ca33b5ad679487c1cdad4e04413b4ea6cbe6442ec966e5b859c15a2e689ea3d7fc425a5c6ed132851a87d45540ca797c0f221a646978495f24ecf3116
|
data/lib/tiny_bus.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'set'
|
1
2
|
require 'time'
|
2
3
|
require 'set'
|
3
4
|
require 'securerandom'
|
@@ -29,6 +30,8 @@ require 'tiny_pipe'
|
|
29
30
|
class TinyBus
|
30
31
|
ANNOTATION_PREFIX_DEFAULT = '.'
|
31
32
|
|
33
|
+
attr_reader :dead_topics
|
34
|
+
|
32
35
|
# log:
|
33
36
|
# if specified, it should be a TinyLog instance
|
34
37
|
# if not specified, it will create a new TinyLog instance for $stdout
|
@@ -55,6 +58,7 @@ class TinyBus
|
|
55
58
|
def initialize(log: nil, dead: nil, translator: nil, raise_on_dead: false,
|
56
59
|
annotation_prefix: ANNOTATION_PREFIX_DEFAULT)
|
57
60
|
@subs = {}
|
61
|
+
@dead_topics = Set.new
|
58
62
|
@translator = translator
|
59
63
|
|
60
64
|
@total_key = "#{annotation_prefix}total"
|
@@ -80,6 +84,7 @@ class TinyBus
|
|
80
84
|
def sub(topic, subber)
|
81
85
|
raise TinyBus::SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
|
82
86
|
|
87
|
+
@dead_topics.delete(topic)
|
83
88
|
@subs[topic] ||= Set.new
|
84
89
|
@subs[topic] << subber
|
85
90
|
@stats[topic] ||= 0
|
@@ -90,6 +95,7 @@ class TinyBus
|
|
90
95
|
# removes a subscriber from a topic
|
91
96
|
def unsub(topic, subber)
|
92
97
|
@subs[topic]&.delete(subber)
|
98
|
+
@dead_topics << topic if @subs[topic].empty?
|
93
99
|
|
94
100
|
msg({ @topic_key => 'unsub', 'from_topic' => topic, 'subber' => _to_subber_id(subber) }, 'TINYBUS-UNSUB')
|
95
101
|
end
|
@@ -121,13 +127,19 @@ class TinyBus
|
|
121
127
|
@stats[@total_key] += 1
|
122
128
|
@stats[topic] += 1
|
123
129
|
if (subbers&.length || 0) > 0
|
124
|
-
|
130
|
+
# cloning is necessary, because sending messanges may result in new
|
131
|
+
# subscribers to the same topic we're iterating over right now. in that
|
132
|
+
# situation we would run into a RuntimeError that prevented the
|
133
|
+
# modification of the Set we're iterating over to send messages.
|
134
|
+
subbers.clone.each{|s| s.msg(msg) }
|
135
|
+
|
125
136
|
@log.send(lvl, "S #{msg}")
|
126
137
|
else
|
127
138
|
if @raise_on_dead
|
128
139
|
raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{topic}'")
|
129
140
|
else
|
130
141
|
@stats[@dead_key] += 1
|
142
|
+
@dead_topics << topic
|
131
143
|
@dead.send(lvl, "D #{msg}")
|
132
144
|
end
|
133
145
|
end
|
@@ -144,9 +156,12 @@ class TinyBus
|
|
144
156
|
# there are no subscribes for a given topic
|
145
157
|
def to_s
|
146
158
|
<<~DEBUG
|
147
|
-
|
148
|
-
|
149
|
-
|
159
|
+
TinyBus stats: #{@stats.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
|
160
|
+
Dead topics: [
|
161
|
+
#{@dead_topics.sort.each_slice(3).map{|slice| slice.join(', ') }.join("\n ")}
|
162
|
+
]
|
163
|
+
Topics & Subscribers:
|
164
|
+
#{@subs.map{|topic, subbers| "#{topic}:\n #{subbers.map(&:to_s).join("\n ")}" }.join("\n ") }
|
150
165
|
DEBUG
|
151
166
|
end
|
152
167
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_bus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Lunt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tiny_log
|