Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consumer: Retry operation upon shutdown errors #27

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/rafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,34 @@ module Rafka
port: 6380
}.freeze

# Server errors upon which we should retry the operation.
RETRIABLE_ERRORS = [
"CONS Server shutdown"
].freeze

# Wraps errors from redis-rb to our own error classes
def self.wrap_errors
yield
rescue Redis::CommandError => e
raise ProduceError, e.message[5..-1] if e.message.start_with?("PROD ")
raise ConsumeError, e.message[5..-1] if e.message.start_with?("CONS ")
raise CommandError, e.message
end

# Retries the operation upon Redis::CommandError
def self.with_retry(max_retries=15)
retries = 0

begin
yield
rescue Redis::CommandError => e
if RETRIABLE_ERRORS.include?(e.message) && retries < max_retries
sleep 1
retries += 1
retry
agis marked this conversation as resolved.
Show resolved Hide resolved
end

raise e
end
end
end
4 changes: 3 additions & 1 deletion lib/rafka/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ def consume_one(timeout)
msg = nil

Rafka.wrap_errors do
msg = @redis.blpop(@blpop_arg, timeout: timeout)
Rafka.with_retry do
msg = @redis.blpop(@blpop_arg, timeout: timeout)
end
end

msg = Message.new(msg) if msg
Expand Down