local_bus 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -19
- data/lib/local_bus/bus.rb +0 -2
- data/lib/local_bus/message.rb +0 -2
- data/lib/local_bus/publication.rb +0 -2
- data/lib/local_bus/station.rb +6 -9
- data/lib/local_bus/subscriber.rb +0 -2
- data/lib/local_bus/version.rb +1 -3
- data/lib/local_bus.rb +0 -2
- data/sig/generated/local_bus/station.rbs +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6f1119ac19b5642c84099312240ce0baf8320b42098cb48c918b627385a5aff
|
4
|
+
data.tar.gz: 936a119be41c3885de3d27bf7a68effb07bbda58fe0856e8ca7043b76bfbfe47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad441d27aacd1447fd9cebc7aaa5b55ab79b75eb6ce8ed756fc6f11d8b77203412796ce724067707e4c859fd28c384c16c2a6abe56139e4df3a727bd76cc104b
|
7
|
+
data.tar.gz: be8cc2ed7325347e6b3447777144dce7992fdc07385f0e20af8cfe300834c7cfe651fd7b6b0ffc9bdd797fb439ad702095220e8caa5a3c20651deae647fc97a0
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[![Lines of Code](https://img.shields.io/badge/loc-
|
1
|
+
[![Lines of Code](https://img.shields.io/badge/loc-365-47d299.svg)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
|
2
2
|
[![GEM Version](https://img.shields.io/gem/v/local_bus)](https://rubygems.org/gems/local_bus)
|
3
3
|
[![GEM Downloads](https://img.shields.io/gem/dt/local_bus)](https://rubygems.org/gems/local_bus)
|
4
4
|
[![Tests](https://github.com/hopsoft/local_bus/actions/workflows/tests.yml/badge.svg)](https://github.com/hopsoft/local_bus/actions)
|
@@ -205,6 +205,13 @@ message.subscribers # blocks and waits until all subscribers complete and return
|
|
205
205
|
|
206
206
|
message.subscribers.first.value
|
207
207
|
#=> "It worked!"
|
208
|
+
|
209
|
+
# subscribe with any object that responds to `#call`.
|
210
|
+
worker = ->(message) do
|
211
|
+
# business logic (e.g. API calls, database queries, disk operations, etc.)
|
212
|
+
"It worked!"
|
213
|
+
end
|
214
|
+
LocalBus.subscribe "user.created", callable: worker
|
208
215
|
```
|
209
216
|
|
210
217
|
### Bus
|
@@ -215,6 +222,7 @@ bus = LocalBus::Bus.new # ... or LocalBus.instance.bus
|
|
215
222
|
# register a subscriber
|
216
223
|
bus.subscribe "user.created" do |message|
|
217
224
|
# business logic (e.g. API calls, database queries, disk operations, etc.)
|
225
|
+
"It worked!"
|
218
226
|
end
|
219
227
|
|
220
228
|
message = bus.publish("user.created", user_id: 123)
|
@@ -222,12 +230,8 @@ message.wait # blocks until all subscribers complete
|
|
222
230
|
message.subscribers # waits and returns the subscribers
|
223
231
|
#=> [#<LocalBus::Subscriber:0x000000012bbb79a8 ...>]
|
224
232
|
|
225
|
-
|
226
|
-
|
227
|
-
# business logic (e.g. API calls, database queries, disk operations, etc.)
|
228
|
-
"It worked!"
|
229
|
-
end
|
230
|
-
bus.subscribe "user.created", callable: worker
|
233
|
+
message.subscribers.first.value
|
234
|
+
#=> "It worked!"
|
231
235
|
```
|
232
236
|
|
233
237
|
### Station
|
@@ -247,13 +251,6 @@ message.subscribers # blocks and waits until all subscribers complete and return
|
|
247
251
|
|
248
252
|
message.subscribers.first.value
|
249
253
|
#=> "It worked!"
|
250
|
-
|
251
|
-
# subscribe with any object that responds to `#call`.
|
252
|
-
worker = ->(message) do
|
253
|
-
# business logic (e.g. API calls, database queries, disk operations, etc.)
|
254
|
-
"It worked!"
|
255
|
-
end
|
256
|
-
station.subscribe "user.created", callable: worker
|
257
254
|
```
|
258
255
|
|
259
256
|
## Advanced Usage
|
@@ -377,12 +374,11 @@ bin/demo-bus # demonstrates Bus performance
|
|
377
374
|
bin/demo-station # demonstrates Station performance
|
378
375
|
```
|
379
376
|
|
380
|
-
Both demos simulate I/O-bound operations _(1 second latency per subscriber)_ to show how LocalBus handles concurrent processing.
|
381
|
-
|
382
|
-
- The Bus processes a message with 10 I/O-bound subscribers in ~1 second instead of 10 seconds
|
383
|
-
- The Station processes 10 messages with 10 I/O-bound subscribers each in ~1 second instead of 100 seconds
|
377
|
+
Both demos simulate I/O-bound operations _(1 second latency per subscriber)_ to show how LocalBus handles concurrent processing.
|
384
378
|
|
385
|
-
|
379
|
+
For example,
|
380
|
+
LocalBus can process 10 messages with 10 I/O-bound subscribers each in **~1 second instead of 100 seconds**,
|
381
|
+
on a 10-core system.
|
386
382
|
|
387
383
|
## See Also
|
388
384
|
|
data/lib/local_bus/bus.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# rbs_inline: enabled
|
4
|
-
|
5
3
|
class LocalBus
|
6
4
|
# The Bus acts as a direct transport mechanism for messages, akin to placing a passenger directly onto a bus.
|
7
5
|
# When a message is published to the Bus, it is immediately delivered to all subscribers, ensuring prompt execution of tasks.
|
data/lib/local_bus/message.rb
CHANGED
data/lib/local_bus/station.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# rbs_inline: enabled
|
4
|
-
# rubocop:disable Lint/MissingCopEnableDirective
|
5
|
-
# rubocop:disable Style/ArgumentsForwarding
|
6
|
-
|
7
3
|
class LocalBus
|
8
4
|
# The Station serves as a queuing system for messages, similar to a bus station where passengers wait for their bus.
|
9
5
|
#
|
@@ -28,16 +24,17 @@ class LocalBus
|
|
28
24
|
# Will not delay process exit when the queue is empty.
|
29
25
|
#
|
30
26
|
# @rbs bus: Bus -- local message bus (default: Bus.new)
|
31
|
-
# @rbs interval: Float -- queue polling interval in seconds (default: 0.
|
27
|
+
# @rbs interval: Float -- queue polling interval in seconds (default: 0.1)
|
32
28
|
# @rbs limit: Integer -- max queue size (default: 10_000)
|
33
29
|
# @rbs threads: Integer -- number of threads to use (default: Etc.nprocessors)
|
34
30
|
# @rbs timeout: Float -- seconds to wait for subscribers to process the message before cancelling (default: 60)
|
35
31
|
# @rbs wait: Float -- seconds to wait for the queue to flush at process exit (default: 5)
|
36
32
|
# @rbs return: void
|
37
|
-
def initialize(bus: Bus.new, interval: 0.
|
33
|
+
def initialize(bus: Bus.new, interval: 0.1, limit: 10_000, threads: Etc.nprocessors, timeout: 60, wait: 5)
|
38
34
|
super()
|
39
35
|
@bus = bus
|
40
|
-
@interval =
|
36
|
+
@interval = interval.to_f
|
37
|
+
@interval = 0.1 unless @interval.positive?
|
41
38
|
@limit = limit.to_i.positive? ? limit.to_i : 10_000
|
42
39
|
@threads = [threads.to_i, 1].max
|
43
40
|
@timeout = timeout.to_f
|
@@ -67,11 +64,11 @@ class LocalBus
|
|
67
64
|
attr_reader :timeout
|
68
65
|
|
69
66
|
# Starts the station
|
70
|
-
# @rbs interval: Float -- queue polling interval in seconds (default:
|
67
|
+
# @rbs interval: Float -- queue polling interval in seconds (default: self.interval)
|
71
68
|
# @rbs threads: Integer -- number of threads to use (default: self.threads)
|
72
69
|
# @rbs return: void
|
73
70
|
def start(interval: self.interval, threads: self.threads)
|
74
|
-
interval =
|
71
|
+
interval = 0.1 unless interval.positive?
|
75
72
|
threads = [threads.to_i, 1].max
|
76
73
|
|
77
74
|
synchronize do
|
data/lib/local_bus/subscriber.rb
CHANGED
data/lib/local_bus/version.rb
CHANGED
data/lib/local_bus.rb
CHANGED
@@ -25,7 +25,7 @@ class LocalBus
|
|
25
25
|
# Will not delay process exit when the queue is empty.
|
26
26
|
#
|
27
27
|
# @rbs bus: Bus -- local message bus (default: Bus.new)
|
28
|
-
# @rbs interval: Float -- queue polling interval in seconds (default: 0.
|
28
|
+
# @rbs interval: Float -- queue polling interval in seconds (default: 0.1)
|
29
29
|
# @rbs limit: Integer -- max queue size (default: 10_000)
|
30
30
|
# @rbs threads: Integer -- number of threads to use (default: Etc.nprocessors)
|
31
31
|
# @rbs timeout: Float -- seconds to wait for subscribers to process the message before cancelling (default: 60)
|
@@ -54,7 +54,7 @@ class LocalBus
|
|
54
54
|
attr_reader timeout: untyped
|
55
55
|
|
56
56
|
# Starts the station
|
57
|
-
# @rbs interval: Float -- queue polling interval in seconds (default:
|
57
|
+
# @rbs interval: Float -- queue polling interval in seconds (default: self.interval)
|
58
58
|
# @rbs threads: Integer -- number of threads to use (default: self.threads)
|
59
59
|
# @rbs return: void
|
60
60
|
def start: (?interval: Float, ?threads: Integer) -> void
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: local_bus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Hopkins (hopsoft)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: algorithms
|
@@ -267,7 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
267
267
|
- !ruby/object:Gem::Version
|
268
268
|
version: '0'
|
269
269
|
requirements: []
|
270
|
-
rubygems_version: 3.5.
|
270
|
+
rubygems_version: 3.5.23
|
271
271
|
signing_key:
|
272
272
|
specification_version: 4
|
273
273
|
summary: A lightweight pub/sub system for decoupled intra-process communication in
|