Skip to content

Commit

Permalink
THREESCALE-11061: Async mode: Add support for Redis logical DBs (#390)
Browse files Browse the repository at this point in the history
* Add support for Redis logical DBs in async mode
  • Loading branch information
jlledom authored May 22, 2024
1 parent 3065b04 commit dea998b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
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]

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

0 comments on commit dea998b

Please sign in to comment.