Class: AnyCable::Rack::Server

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/anycable/rack/server.rb

Constant Summary

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: AnyCable::Rack.config) ⇒ Server

Returns a new instance of Server.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/anycable/rack/server.rb', line 28

def initialize(config: AnyCable::Rack.config)
  @config = config
  @hub = Hub.new
  @pinger = Pinger.new
  # TODO: Support other coders
  @coder = Coders::JSON

  @broadcast = resolve_broadcast_adapter
  @rpc_client = RPC::Client.new(
    host: config.rpc_addr,
    size: config.rpc_client_pool_size,
    timeout: config.rpc_client_timeout
  )

  @middleware = Middleware.new(
    header_names: config.headers,
    pinger: pinger,
    hub: hub,
    rpc_client: rpc_client,
    coder: coder
  )

  log(:info) { "Connecting to RPC server at #{config.rpc_addr}" }
end

Instance Attribute Details

#broadcastObject (readonly)

Returns the value of attribute broadcast.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def broadcast
  @broadcast
end

#coderObject (readonly)

Returns the value of attribute coder.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def coder
  @coder
end

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def headers
  @headers
end

#hubObject (readonly)

Returns the value of attribute hub.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def hub
  @hub
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def middleware
  @middleware
end

#pingerObject (readonly)

Returns the value of attribute pinger.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def pinger
  @pinger
end

#rpc_cliObject (readonly)

Returns the value of attribute rpc_cli.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def rpc_cli
  @rpc_cli
end

#rpc_clientObject (readonly)

Returns the value of attribute rpc_client.



18
19
20
# File 'lib/anycable/rack/server.rb', line 18

def rpc_client
  @rpc_client
end

Instance Method Details

#call(env) ⇒ Object



83
84
85
# File 'lib/anycable/rack/server.rb', line 83

def call(env)
  middleware.call(env)
end

#inspectObject



87
88
89
# File 'lib/anycable/rack/server.rb', line 87

def inspect
  "#<AnyCable::Rack::Server(rpc_addr: #{config.rpc_addr}, headers: [#{config.headers.join(", ")}])>"
end

#shutdownObject



64
65
66
67
68
# File 'lib/anycable/rack/server.rb', line 64

def shutdown
  log(:info) { "Shutting down..." }
  Rack.rpc_server&.shutdown
  hub.broadcast_all(coder.encode(type: "disconnect", reason: "server_restart", reconnect: true))
end

#start!Object

rubocop:enable



54
55
56
57
58
59
60
61
62
# File 'lib/anycable/rack/server.rb', line 54

def start!
  log(:info) { "Starting..." }

  pinger.run

  broadcast.start

  @_started = true
end

#started?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/anycable/rack/server.rb', line 70

def started?
  @_started == true
end

#stopObject



74
75
76
77
78
79
80
81
# File 'lib/anycable/rack/server.rb', line 74

def stop
  return unless started?

  @_started = false
  broadcast_subscriber.stop
  pinger.stop
  hub.close_all
end