Class: Cybertrain::Stream::Hub

Inherits:
Object
  • Object
show all
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

Instance Method Details

#deliver(id, stream, payload) ⇒ Integer

Hands one message to every subscriber of stream.

Parameters:

  • id (Integer)
  • stream (String)
  • payload (String)

Returns:

  • (Integer) —

    how many subscribers it went to



288
289
290
291
292
293
# File 'cybertrain/stream/hub.rb', line 288

def deliver(id, stream, payload)
  message = Message.new(id, stream, payload)
  count = 0
  @lock.synchronize { count = hub_offer(message) }
  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.

Parameters:

  • names (Array<String>)

Returns:



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).

Returns:



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

Parameters:

Returns:

  • (nil)


277
278
279
280
# File 'cybertrain/stream/hub.rb', line 277

def unsubscribe(sub)
  @lock.synchronize { hub_remove(sub) }
  nil
end