Class: ActionCable::Connection::StreamEventLoop

Inherits:
Object
  • Object
show all
Defined in:
actioncable/lib/action_cable/connection/stream_event_loop.rb

Instance Method Summary collapse

Constructor Details

#initializeStreamEventLoop

Returns a new instance of StreamEventLoop.


7
8
9
10
11
12
13
14
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 7

def initialize
  @nio = @executor = @thread = nil
  @map = {}
  @stopping = false
  @todo = Queue.new

  @spawn_mutex = Mutex.new
end

Instance Method Details

#attach(io, stream) ⇒ Object


27
28
29
30
31
32
33
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 27

def attach(io, stream)
  @todo << lambda do
    @map[io] = @nio.register(io, :r)
    @map[io].value = stream
  end
  wakeup
end

#detach(io, stream) ⇒ Object


35
36
37
38
39
40
41
42
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 35

def detach(io, stream)
  @todo << lambda do
    @nio.deregister io
    @map.delete io
    io.close
  end
  wakeup
end

#post(task = nil, &block) ⇒ Object


20
21
22
23
24
25
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 20

def post(task = nil, &block)
  task ||= block

  spawn
  @executor << task
end

#stopObject


53
54
55
56
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 53

def stop
  @stopping = true
  wakeup if @nio
end

#timer(interval, &block) ⇒ Object


16
17
18
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 16

def timer(interval, &block)
  Concurrent::TimerTask.new(execution_interval: interval, &block).tap(&:execute)
end

#writes_pending(io) ⇒ Object


44
45
46
47
48
49
50
51
# File 'actioncable/lib/action_cable/connection/stream_event_loop.rb', line 44

def writes_pending(io)
  @todo << lambda do
    if monitor = @map[io]
      monitor.interests = :rw
    end
  end
  wakeup
end