Class: AnyCable::Rack::Server

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

Constant Summary collapse

DEFAULT_HEADERS =
%w[cookie x-api-token].freeze

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Server

Returns a new instance of Server.



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
# File 'lib/anycable/rack/server.rb', line 30

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.last : {}

  @hub = Hub.new
  @pinger = Pinger.new
  @coder = options.fetch(:coder, Coders::JSON)
  @pubsub_channel = pubsub_channel

  @headers = options.fetch(:headers, DEFAULT_HEADERS)
  @rpc_host = options.fetch(:rpc_host)

  @broadcast = BroadcastSubscribers::RedisSubscriber.new(
    hub: hub,
    coder: coder,
    **AnyCable.config.to_redis_params
  )

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

  log(:info) { "Using RPC server at #{rpc_host}" }
end

Instance Attribute Details

#broadcastObject (readonly)

Returns the value of attribute broadcast.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def broadcast
  @broadcast
end

#coderObject (readonly)

Returns the value of attribute coder.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def coder
  @coder
end

#headersObject (readonly)

Returns the value of attribute headers.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def headers
  @headers
end

#hubObject (readonly)

Returns the value of attribute hub.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def hub
  @hub
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def middleware
  @middleware
end

#pingerObject (readonly)

Returns the value of attribute pinger.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def pinger
  @pinger
end

#pubsub_channelObject (readonly)

Returns the value of attribute pubsub_channel.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def pubsub_channel
  @pubsub_channel
end

#rpc_hostObject (readonly)

Returns the value of attribute rpc_host.



21
22
23
# File 'lib/anycable/rack/server.rb', line 21

def rpc_host
  @rpc_host
end

Instance Method Details

#call(env) ⇒ Object



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

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

#inspectObject



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

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

#start!Object

rubocop:enable



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/anycable/rack/server.rb', line 59

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

  pinger.run

  broadcast.subscribe(AnyCable.config.redis_channel)

  log(:info) { "Subscribed to #{AnyCable.config.redis_channel}" }

  @_started = true
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  @_started == true
end

#stopObject



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

def stop
  return unless started?

  @_started = false
  broadcast_subscriber.unsubscribe(@_redis_channel)
  pinger.stop
  hub.close_all
end