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.



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

def client
  @client
end

Instance Method Details

#closeObject

Close the socket.



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

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

#connect!Object



76
77
78
79
# File 'lib/slack/real_time/concurrency/async.rb', line 76

def connect!
  super
  run_loop
end

#current_timeObject



72
73
74
# File 'lib/slack/real_time/concurrency/async.rb', line 72

def current_time
  ::Async::Clock.now
end

#disconnect!Object

Kill the restart/ping loop.



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

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

#restart_async(_client, new_url) ⇒ Object



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

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

  @restart&.signal
end

#run_loopObject



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

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

#start_async(client) ⇒ Object



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

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

#start_reactor(client) ⇒ Object



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
62
63
# File 'lib/slack/real_time/concurrency/async.rb', line 28

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_timer
          client.run_ping! if @restart
        end
      end
    end

    while @restart
      @client_task&.stop

      @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.
        rescue StandardError => e
          client.logger.error(subtask.to_s) { e.message }
        end
      end

      @restart.wait
    end

    @ping_task&.stop
  end
end

#start_sync(client) ⇒ Object



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

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