Class: Cybertrain::Stream::Hub
- Inherits:
-
Object
- Object
- Cybertrain::Stream::Hub
- Defined in:
- cybertrain/stream/hub.rb
Overview
The in-process fan-out: the poller hands it each new row (#deliver), and it pushes the message onto the queue of every subscriber listening to that stream. One lock, taken when a connection subscribes or leaves and once per delivered row, never per request (spikes/NOTES.md rule 57).
Instance Method Summary collapse
-
#deliver(id, stream, payload) ⇒ Integer
Hands one message to every subscriber of
stream. -
#subscribe(names) ⇒ Subscriber
The new subscriber’s cutoff is the poller’s position (Poller.last_id, no query), taken under the lock #deliver takes: every row the poller delivers after this call is above it, every row it delivered before is not.
-
#subscriber_count ⇒ Integer
Subscribers in this process (the endpoint’s connection count).
-
#unsubscribe(sub) ⇒ nil
Instance Method Details
#deliver(id, stream, payload) ⇒ Integer
Hands one message to every subscriber of stream.
288 289 290 291 292 293 |
# File 'cybertrain/stream/hub.rb', line 288 def deliver(id, stream, payload) = Message.new(id, stream, payload) count = 0 @lock.synchronize { count = hub_offer() } count end |
#subscribe(names) ⇒ Subscriber
The new subscriber’s cutoff is the poller’s position (Poller.last_id, no query), taken under the lock #deliver takes: every row the poller delivers after this call is above it, every row it delivered before is not. So a row committed within the last poll interval before the subscription is still delivered.
244 245 246 247 248 249 250 251 |
# File 'cybertrain/stream/hub.rb', line 244 def subscribe(names) sub = Subscriber.new(names, @max_backlog) @lock.synchronize do sub.skip_through!(Poller.last_id) @subscribers << sub end sub end |
#subscriber_count ⇒ Integer
Subscribers in this process (the endpoint’s connection count).
333 334 335 336 337 |
# File 'cybertrain/stream/hub.rb', line 333 def subscriber_count count = 0 @lock.synchronize { count = @subscribers.size } count end |
#unsubscribe(sub) ⇒ nil
277 278 279 280 |
# File 'cybertrain/stream/hub.rb', line 277 def unsubscribe(sub) @lock.synchronize { hub_remove(sub) } nil end |