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.



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

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

#connect!Object



78
79
80
81
# File 'lib/slack/real_time/concurrency/async.rb', line 78

def connect!
  super
  run_loop
end

#current_timeObject



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

def current_time
  ::Async::Clock.now
end

#disconnect!Object

Kill the restart/ping loop.



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

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
end

#run_async(&block) ⇒ Object



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

def run_async(&block)
  ::Async.run(&block)
end

#run_loopObject



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

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
# 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|
        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

      @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