Class: AnyCable::BroadcastAdapters::Http

Inherits:
Base
  • Object
show all
Defined in:
lib/anycable/broadcast_adapters/http.rb

Overview

HTTP adapter for broadcasting.

Example:

AnyCable.broadcast_adapter = :http

It uses configuration from global AnyCable config by default.

You can override these params:

AnyCable.broadcast_adapter = :http, url: "http://ws.example.com/_any_cable_"

Constant Summary collapse

RECOVERABLE_EXCEPTIONS =
[
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  SocketError,
  (OpenSSL::SSL::SSLError if defined?(OpenSSL))
].compact.freeze
OPEN_TIMEOUT =
5
READ_TIMEOUT =
10
MAX_ATTEMPTS =
3
DELAY =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#batching, #batching?, #broadcast, #broadcast_command, #finish_batching, #start_batching

Constructor Details

#initialize(url: AnyCable.config.http_broadcast_url, secret: AnyCable.config.broadcast_key) ⇒ Http

Returns a new instance of Http.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/anycable/broadcast_adapters/http.rb', line 45

def initialize(url: AnyCable.config.http_broadcast_url, secret: AnyCable.config.broadcast_key)
  @url = url
  @headers = {}
  @authorization = nil

  if !secret
    secret = AnyCable.config.broadcast_key!
    @authorization = "with authorization key inferred from the application secret" if secret
  end

  if secret
    headers["Authorization"] = "Bearer #{secret}"
    @authorization ||= "with authorization"
  end

  @uri = URI.parse(url)
  @queue = Queue.new
end

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



43
44
45
# File 'lib/anycable/broadcast_adapters/http.rb', line 43

def authorization
  @authorization
end

#headersObject (readonly)

Returns the value of attribute headers.



43
44
45
# File 'lib/anycable/broadcast_adapters/http.rb', line 43

def headers
  @headers
end

#urlObject (readonly)

Returns the value of attribute url.



43
44
45
# File 'lib/anycable/broadcast_adapters/http.rb', line 43

def url
  @url
end

Instance Method Details

#announce!Object



79
80
81
# File 'lib/anycable/broadcast_adapters/http.rb', line 79

def announce!
  logger.info "Broadcasting HTTP url: #{url} (#{authorization || "no authorization"})"
end

#raw_broadcast(payload) ⇒ Object



64
65
66
67
68
# File 'lib/anycable/broadcast_adapters/http.rb', line 64

def raw_broadcast(payload)
  ensure_thread_is_alive
  AnyCable.logger.info "Enqueue: #{payload}"
  queue << payload
end

#shutdownObject

Wait for background thread to process all the messages and stop it



72
73
74
75
76
77
# File 'lib/anycable/broadcast_adapters/http.rb', line 72

def shutdown
  queue << :stop
  thread.join if thread&.alive?
rescue Exception => e # rubocop:disable Lint/RescueException
  logger.error "Broadcasting thread exited with exception: #{e.message}"
end