-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Add new createLazyClient() method to connect only on demand and implement "idle" timeout to close underlying connection when unused #87
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Closed
WyriHaximus
approved these changes
Mar 8, 2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clue
changed the title
Add new createLazyClient() method to only connect only on demand (on first command) and implement "idle" timeout to close underlying connection when unused
Add new createLazyClient() method to connect only on demand and implement "idle" timeout to close underlying connection when unused
Mar 8, 2019
🎉🎉🎉 |
This was referenced Mar 9, 2019
clue
added a commit
to clue-labs/reactphp-sqlite
that referenced
this pull request
May 10, 2019
Add new loadLazy() method to connect only on demand and implement "idle" timeout to close underlying connection when unused. Builds on top of clue/reactphp-redis#87 and friends-of-reactphp/mysql#87 and others.
This was referenced May 24, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a new
createLazyClient(string $redisUri): Client
method that can be used tocreate a new
Client
.It helps with establishing a plain TCP/IP or secure TLS connection to Redis
and optionally authenticating (AUTH) and selecting the right database (SELECT).
This method immediately returns a "virtual" connection implementing the
Client
that can be used to interface with your Redis database.Internally, it lazily creates the underlying database connection only on
demand once the first request is invoked on this instance and will queue
all outstanding requests until the underlying connection is ready.
Additionally, it will only keep this underlying connection in an "idle" state
for 60s by default and will automatically close the underlying connection when
it is no longer needed.
From a consumer side this means that you can start sending commands to the
database right away while the underlying connection may still be
outstanding. Because creating this underlying connection may take some
time, it will enqueue all oustanding commands and will ensure that all
commands will be executed in correct order once the connection is ready.
In other words, this "virtual" connection behaves just like a "real"
connection as described in the
Client
interface and frees you from havingto deal with its async resolution.
If the underlying database connection fails, it will reject all
outstanding commands and will return to the initial "idle" state. This
means that you can keep sending additional commands at a later time which
will again try to open a new underlying connection. Note that this may
require special care if you're using transactions (
MULTI
/EXEC
) that are keptopen for longer than the idle period.
While using PubSub channels (see
SUBSCRIBE
andPSUBSCRIBE
commands), this clientwill never reach an "idle" state and will keep pending forever (or until the
underlying database connection is lost). Additionally, if the underlying
database connection drops, it will automatically send the appropriate
unsubscribe
and
punsubscribe
events for all currently active channel and pattern subscriptions.This allows you to react to these events and restore your subscriptions by
creating a new underlying connection repeating the above commands again.
Note that creating the underlying connection will be deferred until the
first request is invoked. Accordingly, any eventual connection issues
will be detected once this instance is first used. You can use the
end()
method to ensure that the "virtual" connection will be soft-closedand no further commands can be enqueued. Similarly, calling
end()
onthis instance when not currently connected will succeed immediately and
will not have to wait for an actual underlying connection.
Depending on your particular use case, you may prefer this method or the
underlying
createClient()
which resolves with a promise. For manysimple use cases it may be easier to create a lazy connection.
The
$redisUri
can be given in thestandard form
[redis[s]://][:auth@]host[:port][/db]
.You can omit the URI scheme and port if you're connecting to the default port 6379:
Redis supports password-based authentication (
AUTH
command). Note that Redis'authentication mechanism does not employ a username, so you can pass the
password
h@llo
URL-encoded (percent-encoded) as part of the URI like this:You can optionally include a path that will be used to select (SELECT command) the right database:
You can use the standard
rediss://
URI scheme if you're using a secure TLS proxy in front of Redis:You can use the
redis+unix://
URI scheme if your Redis instance is listeningon a Unix domain socket (UDS) path:
This method respects PHP's
default_socket_timeout
setting (default 60s)as a timeout for establishing the underlying connection and waiting for
successful authentication. You can explicitly pass a custom timeout value
in seconds (or use a negative number to not apply a timeout) like this:
By default, this method will keep "idle" connection open for 60s and will
then end the underlying connection. The next request after an "idle"
connection ended will automatically create a new underlying connection.
This ensure you always get a "fresh" connection and as such should not be
confused with a "keepalive" or "heartbeat" mechanism, as this will not
actively try to probe the connection. You can explicitly pass a custom
idle timeout value in seconds (or use a negative number to not apply a
timeout) like this:
Supersedes / closes #82, thank you @WyriHaximus for the initial concept!
Builds on top of friends-of-reactphp/mysql#87 and friends-of-reactphp/mysql#88