Represents a way to connect to a remote Redis server.
Definitionsdef self.local(**options)
Create a local Redis endpoint.
Signatureoptions
Hash
Additional options for the endpoint.
Endpoint
A local Redis endpoint.
def self.local(**options)
self.new(LOCALHOST, **options)
end
def self.remote(host, port = 6379, **options)
Create a remote Redis endpoint.
Signaturehost
String
The hostname to connect to.
port
Integer
The port to connect to.
options
Hash
Additional options for the endpoint.
Endpoint
A remote Redis endpoint.
def self.remote(host, port = 6379, **options)
# URI::Generic.build automatically handles IPv6 addresses correctly:
self.new(URI::Generic.build(scheme: "redis", host: host, port: port), **options)
end
def self.parse(string, endpoint = nil, **options)
Parse a Redis URL string into an endpoint.
Signaturestring
String
The URL string to parse.
endpoint
Endpoint
Optional underlying endpoint.
options
Hash
Additional options for the endpoint.
Endpoint
The parsed endpoint.
def self.parse(string, endpoint = nil, **options)
url = URI.parse(string)
return self.new(url, endpoint, **options)
end
def self.for(scheme, host, credentials: nil, port: nil, database: nil, **options)
Construct an endpoint with a specified scheme, hostname, optional path, and options. If no scheme is provided, it will be auto-detected based on SSL context.
Signaturescheme
String, nil
The scheme to use, e.g. "redis" or "rediss". If nil, will auto-detect.
hostname
String
The hostname to connect to (or bind to).
options
Hash
Additional options, passed to #initialize
.
def self.for(scheme, host, credentials: nil, port: nil, database: nil, **options)
# Auto-detect scheme if not provided:
if default_scheme = options.delete(:scheme)
scheme ||= default_scheme
end
scheme ||= options.key?(:ssl_context) ? "rediss" : "redis"
uri_klass = SCHEMES.fetch(scheme.downcase) do
raise ArgumentError, "Unsupported scheme: #{scheme.inspect}"
end
if database
path = "/#{database}"
end
self.new(
uri_klass.build(
scheme: scheme,
userinfo: credentials&.join(":"),
host: host,
port: port,
path: path,
),
**options
)
end
def self.[](object)
Coerce the given object into an endpoint.
Signatureurl
String | Endpoint
The URL or endpoint to convert.
def self.[](object)
if object.is_a?(self)
return object
else
self.parse(object.to_s)
end
end
def initialize(url, endpoint = nil, **options)
Create a new endpoint.
Signatureurl
URI
The URL to connect to.
endpoint
Endpoint
The underlying endpoint to use.
scheme
String
The scheme to use, e.g. "redis" or "rediss".
hostname
String
The hostname to connect to (or bind to), overrides the URL hostname (used for SNI).
port
Integer
The port to bind to, overrides the URL port.
ssl_context
OpenSSL::SSL::SSLContext
The SSL context to use for secure connections.
def initialize(url, endpoint = nil, **options)
super(**options)
raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute?
@url = url
if endpoint
@endpoint = self.build_endpoint(endpoint)
else
@endpoint = nil
end
end
def to_url
Convert the endpoint to a URL.
SignatureURI
The URL representation of the endpoint.
def to_url
url = @url.dup
unless default_port?
url.port = self.port
end
return url
end
def to_s
Convert the endpoint to a string representation.
SignatureString
A string representation of the endpoint.
def to_s
"\#<#{self.class} #{self.to_url} #{@options}>"
end
def inspect
Convert the endpoint to an inspectable string.
SignatureString
An inspectable string representation of the endpoint.
def inspect
"\#<#{self.class} #{self.to_url} #{@options.inspect}>"
end
def address
Get the address of the underlying endpoint.
SignatureString
The address of the endpoint.
def address
endpoint.address
end
def secure?
Check if the connection is secure (using TLS).
SignatureBoolean
True if the connection uses TLS.
def secure?
["rediss"].include?(self.scheme)
end
def protocol
Get the protocol for this endpoint.
SignatureProtocol
The protocol instance configured for this endpoint.
def protocol
protocol = @options.fetch(:protocol, Protocol::RESP2)
if credentials = self.credentials
protocol = Protocol::Authenticated.new(credentials, protocol)
end
if database = self.database
protocol = Protocol::Selected.new(database, protocol)
end
return protocol
end
def default_port
Get the default port for Redis connections.
SignatureInteger
The default Redis port (6379).
def default_port
6379
end
def default_port?
Check if the endpoint is using the default port.
SignatureBoolean
True if using the default port.
def default_port?
port == default_port
end
def port
Get the port for this endpoint.
SignatureInteger
The port number.
def port
@options[:port] || @url.port || default_port
end
def hostname
The hostname is the server we are connecting to:
Implementationdef hostname
@options[:hostname] || @url.hostname
end
def scheme
Get the scheme for this endpoint.
SignatureString
The URL scheme (e.g., "redis" or "rediss").
def scheme
@options[:scheme] || @url.scheme
end
def database
Get the database number for this endpoint.
SignatureInteger | Nil
The database number or nil if not specified.
def database
@options[:database] || extract_database(@url.path)
end
def credentials
Get the credentials for authentication.
SignatureArray(String) | Nil
The username and password credentials or nil if not specified.
def credentials
@options[:credentials] || extract_userinfo(@url.userinfo)
end
def localhost?
Check if the endpoint is connecting to localhost.
SignatureBoolean
True if connecting to localhost.
def localhost?
@url.hostname =~ /^(.*?\.)?localhost\.?$/
end
def ssl_verify_mode
We don't try to validate peer certificates when talking to localhost because they would always be self-signed.
Implementationdef ssl_verify_mode
if self.localhost?
OpenSSL::SSL::VERIFY_NONE
else
OpenSSL::SSL::VERIFY_PEER
end
end
def ssl_context
Get the SSL context for secure connections.
SignatureOpenSSL::SSL::SSLContext
The SSL context configured for this endpoint.
def ssl_context
@options[:ssl_context] || OpenSSL::SSL::SSLContext.new.tap do |context|
context.set_params(
verify_mode: self.ssl_verify_mode
)
end
end
def build_endpoint(endpoint = nil)
Build the underlying endpoint with optional SSL wrapping.
Signatureendpoint
IO::Endpoint
Optional base endpoint to wrap.
IO::Endpoint
The built endpoint, potentially wrapped with SSL.
def build_endpoint(endpoint = nil)
endpoint ||= tcp_endpoint
if secure?
# Wrap it in SSL:
return ::IO::Endpoint::SSLEndpoint.new(endpoint,
ssl_context: self.ssl_context,
hostname: @url.hostname,
timeout: self.timeout,
)
end
return endpoint
end
def endpoint
Get the underlying endpoint, building it if necessary.
SignatureIO::Endpoint
The underlying endpoint for connections.
def endpoint
@endpoint ||= build_endpoint
end
def endpoint=(endpoint)
Set the underlying endpoint.
Signatureendpoint
IO::Endpoint
The endpoint to wrap and use.
def endpoint=(endpoint)
@endpoint = build_endpoint(endpoint)
end
def bind(*arguments, &block)
Bind to the endpoint and yield the server socket.
Signaturearguments
Array
Arguments to pass to the underlying endpoint bind method.
def bind(*arguments, &block)
endpoint.bind(*arguments, &block)
end
def connect(&block)
Connect to the endpoint and yield the client socket.
Implementationdef connect(&block)
endpoint.connect(&block)
end
def each
Iterate over each possible endpoint variation.
Implementationdef each
return to_enum unless block_given?
self.tcp_endpoint.each do |endpoint|
yield self.class.new(@url, endpoint, **@options)
end
end
def key
Get the key for hashing and equality comparison.
SignatureArray
The key components for this endpoint.
def key
[@url, @options]
end
def eql?(other)
Check if this endpoint is equal to another.
Signatureother
Endpoint
The other endpoint to compare with.
Boolean
True if the endpoints are equal.
def eql? other
self.key.eql? other.key
end
def hash
Get the hash code for this endpoint.
SignatureInteger
The hash code based on the endpoint's key.
def hash
self.key.hash
end
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4