Class: Slack::RealTime::Concurrency::Async::Socket

Inherits:
Socket
  • Object
show all
Defined in:
lib/slack/real_time/concurrency/async.rb

Instance Attribute Summary collapse

Attributes inherited from Socket

#driver, #options, #url

Instance Method Summary collapse

Methods inherited from Socket

#connected?, #initialize, #send_data, #time_since_last_message

Constructor Details

This class inherits a constructor from Slack::RealTime::Socket

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/slack/real_time/concurrency/async.rb', line 15

def client
  @client
end

Instance Method Details

#closeObject

Close the socket.



90
91
92
93
94
95
96
97
# File 'lib/slack/real_time/concurrency/async.rb', line 90

def close
  super
ensure
  if @socket
    @socket.close
    @socket = nil
  end
end

#connect!Object



74
75
76
77
# File 'lib/slack/real_time/concurrency/async.rb', line 74

def connect!
  super
  run_loop
end

#current_timeObject



70
71
72
# File 'lib/slack/real_time/concurrency/async.rb', line 70

def current_time
  ::Async::Clock.now
end

#disconnect!Object

Kill the restart/ping loop.



80
81
82
83
84
85
86
87
# File 'lib/slack/real_time/concurrency/async.rb', line 80

def disconnect!
  super
ensure
  if restart = @restart
    @restart = nil
    restart.signal
  end
end

#restart_async(_client, new_url) ⇒ Object



63
64
65
66
67
68
# File 'lib/slack/real_time/concurrency/async.rb', line 63

def restart_async(_client, new_url)
  @url = new_url
  @last_message_at = current_time

  @restart.signal if @restart
end

#run_loopObject



99
100
101
102
103
# File 'lib/slack/real_time/concurrency/async.rb', line 99

def run_loop
  while @driver && @driver.next_event
    # $stderr.puts event.inspect
  end
end

#start_async(client) ⇒ Object



21
22
23
24
25
# File 'lib/slack/real_time/concurrency/async.rb', line 21

def start_async(client)
  Thread.new do
    start_reactor(client)
  end
end

#start_reactor(client) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/slack/real_time/concurrency/async.rb', line 27

def start_reactor(client)
  Async do |task|
    @restart = ::Async::Notification.new

    if client.run_ping?
      @ping_task = task.async do |subtask|
        subtask.annotate 'client keep-alive'

        # The timer task will naturally exit after the driver is set to nil.
        while @restart
          subtask.sleep client.websocket_ping
          client.run_ping! if @restart
        end
      end
    end

    while @restart
      @client_task.stop if @client_task

      @client_task = task.async do |subtask|
        begin
          subtask.annotate 'client run-loop'
          client.run_loop
        rescue ::Async::Wrapper::Cancelled => e
          # Will get restarted by ping worker.
          client.logger.warn(subtask.to_s) { e.message }
        end
      end

      @restart.wait
    end

    @ping_task.stop if @ping_task
  end
end

#start_sync(client) ⇒ Object



17
18
19
# File 'lib/slack/real_time/concurrency/async.rb', line 17

def start_sync(client)
  start_reactor(client).wait
end