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

THREESCALE-11061: Async mode: Add support for Redis logical DBs #390

Merged
merged 3 commits into from
May 22, 2024
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
30 changes: 30 additions & 0 deletions lib/3scale/backend/async_redis/protocol/extended_resp2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'async/redis/protocol/resp2'

module ThreeScale
module Backend
module AsyncRedis
module Protocol

# Custom Redis Protocol supporting Redis logical DBs
class ExtendedRESP2
def initialize(db: nil)
@db = db
end

def client(stream)
client = Async::Redis::Protocol::RESP2.client(stream)

if @db
client.write_request(["SELECT", @db])
client.read_response
end

client
end
end
end
end
end
end
29 changes: 22 additions & 7 deletions lib/3scale/backend/storage_async/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'async/io'
require 'async/redis/client'
require 'async/redis/sentinels'
require '3scale/backend/async_redis/protocol/extended_resp2'

module ThreeScale
module Backend
Expand Down Expand Up @@ -71,20 +72,34 @@ def initialize_client(opts)
end

def init_host_client(opts)
uri = URI(opts[:url] || '')
host = uri.host || DEFAULT_HOST
port = uri.port || DEFAULT_PORT

endpoint = Async::IO::Endpoint.tcp(host, port)
Async::Redis::Client.new(endpoint, limit: opts[:max_connections])
endpoint = make_redis_endpoint(opts)
protocol = make_redis_protocol(opts)
Async::Redis::Client.new(endpoint, protocol: protocol, limit: opts[:max_connections])
end

def init_sentinels_client(opts)
uri = URI(opts[:url] || '')
name = uri.host
role = opts[:role] || :master
protocol = make_redis_protocol(opts)

Async::Redis::SentinelsClient.new(name, opts[:sentinels], role, protocol)
end

# RESP2 with support for logical DBs
def make_redis_protocol(opts)
uri = URI(opts[:url] || "")
db = uri.path[1..-1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking whether we need to report an error if the db is not an integer.
Or just let the client respond to select {invalid-value} with whatever it likes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wouldn't bother reraising this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with @akostadinov


ThreeScale::Backend::AsyncRedis::Protocol::ExtendedRESP2.new(db: db)
end

def make_redis_endpoint(opts)
uri = URI(opts[:url] || "")
host = uri.host || DEFAULT_HOST
port = uri.port || DEFAULT_PORT

Async::Redis::SentinelsClient.new(name, opts[:sentinels], role)
Async::IO::Endpoint.tcp(host, port)
end
end
end
Expand Down