-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from rage-rb/cable-redis-adapter
[Cable] Redis adapter
- Loading branch information
Showing
14 changed files
with
533 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ group :test do | |
gem "domain_name" | ||
gem "websocket-client-simple" | ||
gem "prism" | ||
gem "redis-client" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class Rage::Cable::Adapters::Base | ||
def pick_a_worker(&block) | ||
_lock, lock_path = Tempfile.new.yield_self { |file| [file, file.path] } | ||
|
||
Iodine.on_state(:on_start) do | ||
if File.new(lock_path).flock(File::LOCK_EX | File::LOCK_NB) | ||
if Rage.logger.debug? | ||
puts "INFO: #{Process.pid} is managing #{self.class.name.split("::").last} subscriptions." | ||
end | ||
block.call | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# frozen_string_literal: true | ||
|
||
require "securerandom" | ||
|
||
if !defined?(RedisClient) | ||
fail <<~ERR | ||
Redis adapter depends on the `redis-client` gem. Add the following line to your Gemfile: | ||
gem "redis-client" | ||
ERR | ||
end | ||
|
||
class Rage::Cable::Adapters::Redis < Rage::Cable::Adapters::Base | ||
REDIS_STREAM_NAME = "rage:cable:messages" | ||
DEFAULT_REDIS_OPTIONS = { reconnect_attempts: [0.05, 0.1, 0.5] } | ||
|
||
def initialize(config) | ||
@redis_stream = if (prefix = config.delete(:channel_prefix)) | ||
"#{prefix}:#{REDIS_STREAM_NAME}" | ||
else | ||
REDIS_STREAM_NAME | ||
end | ||
|
||
@redis_config = RedisClient.config(**DEFAULT_REDIS_OPTIONS.merge(config)) | ||
@server_uuid = SecureRandom.uuid | ||
|
||
redis_version = get_redis_version | ||
if redis_version < Gem::Version.create(5) | ||
raise "Redis adapter only supports Redis 5+. Detected Redis version: #{redis_version}." | ||
end | ||
|
||
@trimming_strategy = redis_version < Gem::Version.create("6.2.0") ? :maxlen : :minid | ||
|
||
pick_a_worker { poll } | ||
end | ||
|
||
def publish(stream_name, data) | ||
message_uuid = SecureRandom.uuid | ||
|
||
publish_redis.call( | ||
"XADD", | ||
@redis_stream, | ||
trimming_method, "~", trimming_value, | ||
"*", | ||
"1", stream_name, | ||
"2", data.to_json, | ||
"3", @server_uuid, | ||
"4", message_uuid | ||
) | ||
end | ||
|
||
private | ||
|
||
def publish_redis | ||
@publish_redis ||= @redis_config.new_client | ||
end | ||
|
||
def trimming_method | ||
@trimming_strategy == :maxlen ? "MAXLEN" : "MINID" | ||
end | ||
|
||
def trimming_value | ||
@trimming_strategy == :maxlen ? "10000" : ((Time.now.to_f - 5 * 60) * 1000).to_i | ||
end | ||
|
||
def get_redis_version | ||
service_redis = @redis_config.new_client | ||
version = service_redis.call("INFO").match(/redis_version:([[:graph:]]+)/)[1] | ||
|
||
Gem::Version.create(version) | ||
|
||
rescue RedisClient::Error => e | ||
puts "FATAL: Couldn't connect to Redis - all broadcasts will be limited to the current server." | ||
puts e.backtrace.join("\n") | ||
Gem::Version.create(5) | ||
|
||
ensure | ||
service_redis.close | ||
end | ||
|
||
def error_backoff_intervals | ||
@error_backoff_intervals ||= Enumerator.new do |y| | ||
y << 0.2 << 0.5 << 1 << 2 << 5 | ||
loop { y << 10 } | ||
end | ||
end | ||
|
||
def poll | ||
unless Fiber.scheduler | ||
Fiber.set_scheduler(Rage::FiberScheduler.new) | ||
end | ||
|
||
Iodine.on_state(:start_shutdown) do | ||
@stopping = true | ||
end | ||
|
||
Fiber.schedule do | ||
read_redis = @redis_config.new_client | ||
last_id = (Time.now.to_f * 1000).to_i | ||
last_message_uuid = nil | ||
|
||
loop do | ||
data = read_redis.blocking_call(5, "XREAD", "COUNT", "100", "BLOCK", "5000", "STREAMS", @redis_stream, last_id) | ||
|
||
if data | ||
data[@redis_stream].each do |id, (_, stream_name, _, serialized_data, _, broadcaster_uuid, _, message_uuid)| | ||
if broadcaster_uuid != @server_uuid && message_uuid != last_message_uuid | ||
Rage.config.cable.protocol.broadcast(stream_name, JSON.parse(serialized_data)) | ||
end | ||
|
||
last_id = id | ||
last_message_uuid = message_uuid | ||
end | ||
end | ||
|
||
rescue RedisClient::Error => e | ||
Rage.logger.error("Subscriber error: #{e.message} (#{e.class})") | ||
sleep error_backoff_intervals.next | ||
rescue => e | ||
@stopping ? break : raise(e) | ||
else | ||
error_backoff_intervals.rewind | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.