A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://socketry.github.io/async-redis/source/Async/Redis/Endpoint/index.html below:

Async::Redis::Endpoint

Represents a way to connect to a remote Redis server.

Definitions def self.local(**options)

Create a local Redis endpoint.

Signature
parameter options Hash

Additional options for the endpoint.

returns Endpoint

A local Redis endpoint.

Implementation
def self.local(**options)
	self.new(LOCALHOST, **options)
end
def self.remote(host, port = 6379, **options)

Create a remote Redis endpoint.

Signature
parameter host String

The hostname to connect to.

parameter port Integer

The port to connect to.

parameter options Hash

Additional options for the endpoint.

returns Endpoint

A remote Redis endpoint.

Implementation
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.

Signature
parameter string String

The URL string to parse.

parameter endpoint Endpoint

Optional underlying endpoint.

parameter options Hash

Additional options for the endpoint.

returns Endpoint

The parsed endpoint.

Implementation
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.

Signature
parameter scheme String, nil

The scheme to use, e.g. "redis" or "rediss". If nil, will auto-detect.

parameter hostname String

The hostname to connect to (or bind to).

parameter options Hash

Additional options, passed to #initialize.

Implementation
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.

Signature
parameter url String | Endpoint

The URL or endpoint to convert.

Implementation
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.

Signature
parameter url URI

The URL to connect to.

parameter endpoint Endpoint

The underlying endpoint to use.

option scheme String

The scheme to use, e.g. "redis" or "rediss".

option hostname String

The hostname to connect to (or bind to), overrides the URL hostname (used for SNI).

option port Integer

The port to bind to, overrides the URL port.

option ssl_context OpenSSL::SSL::SSLContext

The SSL context to use for secure connections.

Implementation
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.

Signature
returns URI

The URL representation of the endpoint.

Implementation
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.

Signature
returns String

A string representation of the endpoint.

Implementation
def to_s
	"\#<#{self.class} #{self.to_url} #{@options}>"
end
def inspect

Convert the endpoint to an inspectable string.

Signature
returns String

An inspectable string representation of the endpoint.

Implementation
def inspect
	"\#<#{self.class} #{self.to_url} #{@options.inspect}>"
end
def address

Get the address of the underlying endpoint.

Signature
returns String

The address of the endpoint.

Implementation
def address
	endpoint.address
end
def secure?

Check if the connection is secure (using TLS).

Signature
returns Boolean

True if the connection uses TLS.

Implementation
def secure?
	["rediss"].include?(self.scheme)
end
def protocol

Get the protocol for this endpoint.

Signature
returns Protocol

The protocol instance configured for this endpoint.

Implementation
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.

Signature
returns Integer

The default Redis port (6379).

Implementation
def default_port
	6379
end
def default_port?

Check if the endpoint is using the default port.

Signature
returns Boolean

True if using the default port.

Implementation
def default_port?
	port == default_port
end
def port

Get the port for this endpoint.

Signature
returns Integer

The port number.

Implementation
def port
	@options[:port] || @url.port || default_port
end
def hostname

The hostname is the server we are connecting to:

Implementation
def hostname
	@options[:hostname] || @url.hostname
end
def scheme

Get the scheme for this endpoint.

Signature
returns String

The URL scheme (e.g., "redis" or "rediss").

Implementation
def scheme
	@options[:scheme] || @url.scheme
end
def database

Get the database number for this endpoint.

Signature
returns Integer | Nil

The database number or nil if not specified.

Implementation
def database
	@options[:database] || extract_database(@url.path)
end
def credentials

Get the credentials for authentication.

Signature
returns Array(String) | Nil

The username and password credentials or nil if not specified.

Implementation
def credentials
	@options[:credentials] || extract_userinfo(@url.userinfo)
end
def localhost?

Check if the endpoint is connecting to localhost.

Signature
returns Boolean

True if connecting to localhost.

Implementation
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.

Implementation
def 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.

Signature
returns OpenSSL::SSL::SSLContext

The SSL context configured for this endpoint.

Implementation
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.

Signature
parameter endpoint IO::Endpoint

Optional base endpoint to wrap.

returns IO::Endpoint

The built endpoint, potentially wrapped with SSL.

Implementation
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.

Signature
returns IO::Endpoint

The underlying endpoint for connections.

Implementation
def endpoint
	@endpoint ||= build_endpoint
end
def endpoint=(endpoint)

Set the underlying endpoint.

Signature
parameter endpoint IO::Endpoint

The endpoint to wrap and use.

Implementation
def endpoint=(endpoint)
	@endpoint = build_endpoint(endpoint)
end
def bind(*arguments, &block)

Bind to the endpoint and yield the server socket.

Signature
parameter arguments Array

Arguments to pass to the underlying endpoint bind method.

Implementation
def bind(*arguments, &block)
	endpoint.bind(*arguments, &block)
end
def connect(&block)

Connect to the endpoint and yield the client socket.

Implementation
def connect(&block)
	endpoint.connect(&block)
end
def each

Iterate over each possible endpoint variation.

Implementation
def 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.

Signature
returns Array

The key components for this endpoint.

Implementation
def key
	[@url, @options]
end
def eql?(other)

Check if this endpoint is equal to another.

Signature
parameter other Endpoint

The other endpoint to compare with.

returns Boolean

True if the endpoints are equal.

Implementation
def eql? other
	self.key.eql? other.key
end
def hash

Get the hash code for this endpoint.

Signature
returns Integer

The hash code based on the endpoint's key.

Implementation
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