Class: Cybertrain::Stream::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/stream/hub.rb

Overview

One connection’s subscription: the stream names it listens to and the queue the hub pushes its messages onto. The connection’s thread takes them with #stream_pop. A queue that reaches max_backlog undelivered messages drops the subscriber (#dropped?): the connection is closed and the client reconnects, as Action Cable does with a client that cannot keep up.

Instance Method Summary collapse

Instance Method Details

#dropped? ⇒ Boolean

True once the backlog overflowed.

Returns:

  • (Boolean)


145
146
147
# File 'cybertrain/stream/hub.rb', line 145

def dropped?
  @dropped
end

#skip_through!(boundary) ⇒ nil

The replay-to-live handoff: after a connection has subscribed and then written the replayed rows (Stream.replay), the messages queued meanwhile with an id at or below boundary (the larger of the replay’s start and the last replayed id) were already sent, and are dropped, so every row reaches the client once, in id order.

Parameters:

Returns:

  • (nil)


137
138
139
140
# File 'cybertrain/stream/hub.rb', line 137

def skip_through!(boundary)
  @boundary = boundary if boundary > @boundary
  nil
end

#stream_pop(timeout) ⇒ Message?

The next message, waiting at most timeout seconds; nil when none came, or once the subscriber is dropped. Messages with an id at or below the #skip_through! boundary are dropped on the way. (Queue#pop(timeout:) parks the green thread under Spinel too: spikes/NOTES.md rule 85.)

Parameters:

  • timeout (Numeric) —

    seconds

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'cybertrain/stream/hub.rb', line 106

def stream_pop(timeout)
  wait = timeout.to_f
  deadline = Clock.monotonic + wait
  while true
    return nil if @dropped

    message = @queue.pop(timeout: wait)
    return nil if @dropped || message.nil?

    # a negative id is a WebSocket reader's control message
    if message.message_id < 0
      # the overflow wake-up was never counted (stream_control refused it)
      @control_lock.synchronize { @controls -= 1 } unless message.message_stream == "overflow"
      return message
    end
    return message if message.message_id > @boundary

    wait = deadline - Clock.monotonic
    wait = 0.0 if wait < 0.0
  end
  nil
end