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

Add new createLazyConnection() method to only connect only on demand (on first command) #87

Merged
merged 2 commits into from
Oct 19, 2018

Conversation

clue
Copy link
Contributor

@clue clue commented Oct 18, 2018

This PR implements a new Factory::createLazyConnection() method.

It helps with establishing a TCP/IP connection to your MySQL database
and issuing the initial authentication handshake.

$connection = $factory->createLazyConnection($url);

$connection->query(…);

This method immediately returns a "virtual" connection implementing the
ConnectionInterface that can be used to
interface with your MySQL database. Internally, it lazily creates the
underlying database connection (which may take some time) only once the
first request is invoked on this instance and will queue all outstanding
requests until the underlying connection is ready.

From a consumer side this means that you can start sending queries to the
database right away while the actual connection may still be outstanding.
It will ensure that all commands will be executed in the order they are
enqueued once the connection is ready. If the database connection fails,
it will emit an error event, reject all outstanding commands and close
the connection as described in the ConnectionInterface. In other words,
it behaves just like a real connection and frees you from having to deal
with its async resolution.

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. Similarly, calling
quit() on this instance before invoking any requests will succeed
immediately and will not wait for an actual underlying connection.

Depending on your particular use case, you may prefer this method or the
underlying createConnection() which resolves with a promise. For many
simple use cases it may be easier to create a lazy connection.

Builds on top of #86 and #84
This is also done in preparation for a future connection pool (#27)

@clue clue added this to the v0.5.0 milestone Oct 18, 2018
@WyriHaximus WyriHaximus merged commit d0b0c0b into friends-of-reactphp:master Oct 19, 2018
@clue clue deleted the lazy-connection branch October 19, 2018 07:49
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants