Class: AnyCable::BroadcastAdapters::Http
- 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
-
#authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #announce! ⇒ Object
-
#initialize(url: AnyCable.config.http_broadcast_url, secret: AnyCable.config.broadcast_key) ⇒ Http
constructor
A new instance of Http.
- #raw_broadcast(payload) ⇒ Object
-
#shutdown ⇒ Object
Wait for background thread to process all the messages and stop it.
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
#authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
43 44 45 |
# File 'lib/anycable/broadcast_adapters/http.rb', line 43 def @authorization end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
43 44 45 |
# File 'lib/anycable/broadcast_adapters/http.rb', line 43 def headers @headers end |
#url ⇒ Object (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} (#{ || "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 |
#shutdown ⇒ Object
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.}" end |