-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: verify occurrence of redirections just in case (#376)
- Loading branch information
1 parent
8cbcaf4
commit a1ca9b3
Showing
12 changed files
with
283 additions
and
120 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 was deleted.
Oops, something went wrong.
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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module Middlewares | ||
module CommandCapture | ||
CapturedCommand = Struct.new('CapturedCommand', :server_url, :command, :pipelined, keyword_init: true) do | ||
def inspect | ||
"#<#{self.class.name} [on #{server_url}] #{command.join(' ')} >" | ||
end | ||
end | ||
|
||
# The CommandBuffer is what should be set as the :captured_commands custom option. | ||
# It needs to be threadsafe, because redis-cluster-client performs some redis operations on | ||
# multiple nodes in parallel, and in e.g. jruby it's not safe to concurrently manipulate the same array. | ||
class CommandBuffer | ||
def initialize | ||
@array = [] | ||
@mutex = Mutex.new | ||
end | ||
|
||
def to_a | ||
@mutex.synchronize do | ||
@array.dup | ||
end | ||
end | ||
|
||
def <<(command) | ||
@mutex.synchronize do | ||
@array << command | ||
end | ||
end | ||
|
||
def count(*cmd) | ||
@mutex.synchronize do | ||
next 0 if @array.empty? | ||
|
||
@array.count do |e| | ||
cmd.size.times.all? { |i| cmd[i].downcase == e.command[i]&.downcase } | ||
end | ||
end | ||
end | ||
|
||
def clear | ||
@mutex.synchronize do | ||
@array.clear | ||
end | ||
end | ||
end | ||
|
||
def call(command, redis_config) | ||
redis_config.custom[:captured_commands] << CapturedCommand.new( | ||
server_url: ::Middlewares::CommandCapture.normalize_captured_url(redis_config.server_url), | ||
command: command, | ||
pipelined: false | ||
) | ||
super | ||
end | ||
|
||
def call_pipelined(commands, redis_config) | ||
commands.map do |command| | ||
redis_config.custom[:captured_commands] << CapturedCommand.new( | ||
server_url: ::Middlewares::CommandCapture.normalize_captured_url(redis_config.server_url), | ||
command: command, | ||
pipelined: true | ||
) | ||
end | ||
super | ||
end | ||
|
||
def self.normalize_captured_url(url) | ||
URI.parse(url).tap do |u| | ||
u.path = '' | ||
end.to_s | ||
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module Middlewares | ||
module RedirectionCount | ||
class Counter | ||
Result = Struct.new('RedirectionCountResult', :moved, :ask, keyword_init: true) | ||
|
||
def initialize | ||
@moved = 0 | ||
@ask = 0 | ||
@mutex = Mutex.new | ||
end | ||
|
||
def moved | ||
@mutex.synchronize { @moved += 1 } | ||
end | ||
|
||
def ask | ||
@mutex.synchronize { @ask += 1 } | ||
end | ||
|
||
def get | ||
@mutex.synchronize { Result.new(moved: @moved, ask: @ask) } | ||
end | ||
|
||
def zero? | ||
@mutex.synchronize { @moved == 0 && @ask == 0 } | ||
end | ||
|
||
def clear | ||
@mutex.synchronize do | ||
@moved = 0 | ||
@ask = 0 | ||
end | ||
end | ||
end | ||
|
||
def call(cmd, cfg) | ||
super | ||
rescue ::RedisClient::CommandError => e | ||
if e.message.start_with?('MOVED') | ||
cfg.custom.fetch(:redirection_count).moved | ||
elsif e.message.start_with?('ASK') | ||
cfg.custom.fetch(:redirection_count).ask | ||
end | ||
|
||
raise | ||
end | ||
|
||
def call_pipelined(cmd, cfg) | ||
super | ||
rescue ::RedisClient::CommandError => e | ||
if e.message.start_with?('MOVED') | ||
cfg.custom.fetch(:redirection_count).moved | ||
elsif e.message.start_with?('ASK') | ||
cfg.custom.fetch(:redirection_count).ask | ||
end | ||
|
||
raise | ||
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Middlewares | ||
module RedirectionEmulation | ||
Setting = Struct.new( | ||
'RedirectionEmulationMiddlewareSetting', | ||
:slot, :to, :command, keyword_init: true | ||
) | ||
|
||
def call(cmd, cfg) | ||
s = cfg.custom.fetch(:redirect) | ||
raise RedisClient::CommandError, "MOVED #{s.slot} #{s.to}" if cmd == s.command | ||
|
||
super | ||
end | ||
|
||
def call_pipelined(cmd, cfg) | ||
s = cfg.custom.fetch(:redirect) | ||
raise RedisClient::CommandError, "MOVED #{s.slot} #{s.to}" if cmd == s.command | ||
|
||
super | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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.