tipo: modificación del cpanel y gitignore de la carpeta vendor
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
github: clue
|
||||
custom: https://clue.engineering/support
|
||||
Vendored
+287
@@ -0,0 +1,287 @@
|
||||
# Changelog
|
||||
|
||||
## 2.8.0 (2025-01-03)
|
||||
|
||||
This is a compatibility release that contains backported features from the `3.x` branch.
|
||||
Once v3 is released, it will be the way forward for this project.
|
||||
|
||||
* Feature: Improve PHP 8.4+ support by avoiding implicitly nullable types.
|
||||
(#165 and #170 by @clue)
|
||||
|
||||
## 2.7.0 (2024-01-05)
|
||||
|
||||
This is a compatibility release that contains backported features from the `3.x` branch.
|
||||
Once v3 is released, it will be the way forward for this project.
|
||||
|
||||
* Feature: Forward compatibility with Promise v3.
|
||||
(#152 by @clue)
|
||||
|
||||
* Feature: Full PHP 8.3 compatibility and update test suite.
|
||||
(#151 by @clue)
|
||||
|
||||
## 2.6.0 (2022-05-09)
|
||||
|
||||
* Feature: Support PHP 8.1 release.
|
||||
(#119 by @clue)
|
||||
|
||||
* Improve documentation and CI configuration.
|
||||
(#123 and #125 by @SimonFrings)
|
||||
|
||||
## 2.5.0 (2021-08-31)
|
||||
|
||||
* Feature: Simplify usage by supporting new [default loop](https://reactphp.org/event-loop/#loop) and new Socket API.
|
||||
(#114 and #115 by @SimonFrings)
|
||||
|
||||
```php
|
||||
// old (still supported)
|
||||
$factory = new Clue\React\Redis\Factory($loop);
|
||||
|
||||
// new (using default loop)
|
||||
$factory = new Clue\React\Redis\Factory();
|
||||
```
|
||||
|
||||
* Feature: Improve error reporting, include Redis URI and socket error codes in all connection errors.
|
||||
(#116 by @clue)
|
||||
|
||||
* Documentation improvements and updated examples.
|
||||
(#117 by @clue, #112 by @Nyholm and #113 by @PaulRotmann)
|
||||
|
||||
* Improve test suite and use GitHub actions for continuous integration (CI).
|
||||
(#111 by @SimonFrings)
|
||||
|
||||
## 2.4.0 (2020-09-25)
|
||||
|
||||
* Fix: Fix dangling timer when lazy connection closes with pending commands.
|
||||
(#105 by @clue)
|
||||
|
||||
* Improve test suite and add `.gitattributes` to exclude dev files from exports.
|
||||
Prepare PHP 8 support, update to PHPUnit 9 and simplify test matrix.
|
||||
(#96 and #97 by @clue and #99, #101 and #104 by @SimonFrings)
|
||||
|
||||
## 2.3.0 (2019-03-11)
|
||||
|
||||
* Feature: Add new `createLazyClient()` method to connect only on demand and
|
||||
implement "idle" timeout to close underlying connection when unused.
|
||||
(#87 and #88 by @clue and #82 by @WyriHaximus)
|
||||
|
||||
```php
|
||||
$client = $factory->createLazyClient('redis://localhost:6379');
|
||||
|
||||
$client->incr('hello');
|
||||
$client->end();
|
||||
```
|
||||
|
||||
* Feature: Support cancellation of pending connection attempts.
|
||||
(#85 by @clue)
|
||||
|
||||
```php
|
||||
$promise = $factory->createClient($redisUri);
|
||||
|
||||
$loop->addTimer(3.0, function () use ($promise) {
|
||||
$promise->cancel();
|
||||
});
|
||||
```
|
||||
|
||||
* Feature: Support connection timeouts.
|
||||
(#86 by @clue)
|
||||
|
||||
```php
|
||||
$factory->createClient('localhost?timeout=0.5');
|
||||
```
|
||||
|
||||
* Feature: Improve Exception messages for connection issues.
|
||||
(#89 by @clue)
|
||||
|
||||
```php
|
||||
$factory->createClient('redis://localhost:6379')->then(
|
||||
function (Client $client) {
|
||||
// client connected (and authenticated)
|
||||
},
|
||||
function (Exception $e) {
|
||||
// an error occurred while trying to connect (or authenticate) client
|
||||
echo $e->getMessage() . PHP_EOL;
|
||||
if ($e->getPrevious()) {
|
||||
echo $e->getPrevious()->getMessage() . PHP_EOL;
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
* Improve test suite structure and add forward compatibility with PHPUnit 7 and PHPUnit 6
|
||||
and test against PHP 7.1, 7.2, and 7.3 on TravisCI.
|
||||
(#83 by @WyriHaximus and #84 by @clue)
|
||||
|
||||
* Improve documentation and update project homepage.
|
||||
(#81 and #90 by @clue)
|
||||
|
||||
## 2.2.0 (2018-01-24)
|
||||
|
||||
* Feature: Support communication over Unix domain sockets (UDS)
|
||||
(#70 by @clue)
|
||||
|
||||
```php
|
||||
// new: now supports redis over Unix domain sockets (UDS)
|
||||
$factory->createClient('redis+unix:///tmp/redis.sock');
|
||||
```
|
||||
|
||||
## 2.1.0 (2017-09-25)
|
||||
|
||||
* Feature: Update Socket dependency to support hosts file on all platforms
|
||||
(#66 by @clue)
|
||||
|
||||
This means that connecting to hosts such as `localhost` (and for example
|
||||
those used for Docker containers) will now work as expected across all
|
||||
platforms with no changes required:
|
||||
|
||||
```php
|
||||
$factory->createClient('localhost');
|
||||
```
|
||||
|
||||
## 2.0.0 (2017-09-20)
|
||||
|
||||
A major compatibility release to update this package to support all latest
|
||||
ReactPHP components!
|
||||
|
||||
This update involves a minor BC break due to dropped support for legacy
|
||||
versions. We've tried hard to avoid BC breaks where possible and minimize impact
|
||||
otherwise. We expect that most consumers of this package will actually not be
|
||||
affected by any BC breaks, see below for more details.
|
||||
|
||||
* BC break: Remove all deprecated APIs, default to `redis://` URI scheme
|
||||
and drop legacy SocketClient in favor of new Socket component.
|
||||
(#61 by @clue)
|
||||
|
||||
> All of this affects the `Factory` only, which is mostly considered
|
||||
"advanced usage". If you're affected by this BC break, then it's
|
||||
recommended to first update to the intermediary v1.2.0 release, which
|
||||
allows you to use the `redis://` URI scheme and a standard
|
||||
`ConnectorInterface` and then update to this version without causing a
|
||||
BC break.
|
||||
|
||||
* BC break: Remove uneeded `data` event and support for advanced `MONITOR`
|
||||
command for performance and consistency reasons and
|
||||
remove underdocumented `isBusy()` method.
|
||||
(#62, #63 and #64 by @clue)
|
||||
|
||||
* Feature: Forward compatibility with upcoming Socket v1.0 and v0.8 and EventLoop v1.0 and Evenement v3
|
||||
(#65 by @clue)
|
||||
|
||||
## 1.2.0 (2017-09-19)
|
||||
|
||||
* Feature: Support `redis[s]://` URI scheme and deprecate legacy URIs
|
||||
(#60 by @clue)
|
||||
|
||||
```php
|
||||
$factory->createClient('redis://:secret@localhost:6379/4');
|
||||
$factory->createClient('redis://localhost:6379?password=secret&db=4');
|
||||
```
|
||||
|
||||
* Feature: Factory accepts Connector from Socket and deprecate legacy SocketClient
|
||||
(#59 by @clue)
|
||||
|
||||
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
|
||||
proxy servers etc.), you can explicitly pass a custom instance of the
|
||||
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
|
||||
|
||||
```php
|
||||
$connector = new \React\Socket\Connector($loop, array(
|
||||
'dns' => '127.0.0.1',
|
||||
'tcp' => array(
|
||||
'bindto' => '192.168.10.1:0'
|
||||
),
|
||||
'tls' => array(
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false
|
||||
)
|
||||
));
|
||||
|
||||
$factory = new Factory($loop, $connector);
|
||||
```
|
||||
|
||||
## 1.1.0 (2017-09-18)
|
||||
|
||||
* Feature: Update SocketClient dependency to latest version
|
||||
(#58 by @clue)
|
||||
|
||||
* Improve test suite by adding PHPUnit to require-dev,
|
||||
fix HHVM build for now again and ignore future HHVM build errors,
|
||||
lock Travis distro so new defaults will not break the build and
|
||||
skip functional integration tests by default
|
||||
(#52, #53, #56 and #57 by @clue)
|
||||
|
||||
## 1.0.0 (2016-05-20)
|
||||
|
||||
* First stable release, now following SemVer
|
||||
|
||||
* BC break: Consistent public API, mark internal APIs as such
|
||||
(#38 by @clue)
|
||||
|
||||
```php
|
||||
// old
|
||||
$client->on('data', function (MessageInterface $message, Client $client) {
|
||||
// process an incoming message (raw message object)
|
||||
});
|
||||
|
||||
// new
|
||||
$client->on('data', function (MessageInterface $message) use ($client) {
|
||||
// process an incoming message (raw message object)
|
||||
});
|
||||
```
|
||||
|
||||
> Contains no other changes, so it's actually fully compatible with the v0.5.2 release.
|
||||
|
||||
## 0.5.2 (2016-05-20)
|
||||
|
||||
* Fix: Do not send empty SELECT statement when no database has been given
|
||||
(#35, #36 by @clue)
|
||||
|
||||
* Improve documentation, update dependencies and add first class support for PHP 7
|
||||
|
||||
## 0.5.1 (2015-01-12)
|
||||
|
||||
* Fix: Fix compatibility with react/promise v2.0 for monitor and PubSub commands.
|
||||
(#28)
|
||||
|
||||
## 0.5.0 (2014-11-12)
|
||||
|
||||
* Feature: Support PubSub commands (P)(UN)SUBSCRIBE and watching for "message",
|
||||
"subscribe" and "unsubscribe" events
|
||||
(#24)
|
||||
|
||||
* Feature: Support MONITOR command and watching for "monitor" events
|
||||
(#23)
|
||||
|
||||
* Improve documentation, update locked dependencies and add first class support for HHVM
|
||||
(#25, #26 and others)
|
||||
|
||||
## 0.4.0 (2014-08-25)
|
||||
|
||||
* BC break: The `Client` class has been renamed to `StreamingClient`.
|
||||
Added new `Client` interface.
|
||||
(#18 and #19)
|
||||
|
||||
* BC break: Rename `message` event to `data`.
|
||||
(#21)
|
||||
|
||||
* BC break: The `Factory` now accepts a `LoopInterface` as first argument.
|
||||
(#22)
|
||||
|
||||
* Fix: The `close` event will be emitted once when invoking the `Client::close()`
|
||||
method or when the underlying stream closes.
|
||||
(#20)
|
||||
|
||||
* Refactored code, improved testability, extended test suite and better code coverage.
|
||||
(#11, #18 and #20)
|
||||
|
||||
> Note: This is an intermediary release to ease upgrading to the imminent v0.5 release.
|
||||
|
||||
## 0.3.0 (2014-05-31)
|
||||
|
||||
* First tagged release
|
||||
|
||||
> Note: Starts at v0.3 because previous versions were not tagged. Leaving some
|
||||
> room in case they're going to be needed in the future.
|
||||
|
||||
## 0.0.0 (2013-07-05)
|
||||
|
||||
* Initial concept
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Christian Lück
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
Vendored
+660
@@ -0,0 +1,660 @@
|
||||
# clue/reactphp-redis
|
||||
|
||||
[](https://github.com/clue/reactphp-redis/actions)
|
||||
[](https://packagist.org/packages/clue/redis-react)
|
||||
|
||||
Async [Redis](https://redis.io/) client implementation, built on top of [ReactPHP](https://reactphp.org/).
|
||||
|
||||
[Redis](https://redis.io/) is an open source, advanced, in-memory key-value database.
|
||||
It offers a set of simple, atomic operations in order to work with its primitive data types.
|
||||
Its lightweight design and fast operation makes it an ideal candidate for modern application stacks.
|
||||
This library provides you a simple API to work with your Redis database from within PHP.
|
||||
It enables you to set and query its data or use its PubSub topics to react to incoming events.
|
||||
|
||||
* **Async execution of Commands** -
|
||||
Send any number of commands to Redis in parallel (automatic pipeline) and
|
||||
process their responses as soon as results come in.
|
||||
The Promise-based design provides a *sane* interface to working with async responses.
|
||||
* **Event-driven core** -
|
||||
Register your event handler callbacks to react to incoming events, such as an incoming PubSub message event.
|
||||
* **Lightweight, SOLID design** -
|
||||
Provides a thin abstraction that is [*just good enough*](https://en.wikipedia.org/wiki/Principle_of_good_enough)
|
||||
and does not get in your way.
|
||||
Future or custom commands and events require no changes to be supported.
|
||||
* **Good test coverage** -
|
||||
Comes with an automated tests suite and is regularly tested against versions as old as Redis v2.6 and newer.
|
||||
|
||||
**Table of Contents**
|
||||
|
||||
* [Support us](#support-us)
|
||||
* [Quickstart example](#quickstart-example)
|
||||
* [Usage](#usage)
|
||||
* [Commands](#commands)
|
||||
* [Promises](#promises)
|
||||
* [PubSub](#pubsub)
|
||||
* [API](#api)
|
||||
* [Factory](#factory)
|
||||
* [createClient()](#createclient)
|
||||
* [createLazyClient()](#createlazyclient)
|
||||
* [Client](#client)
|
||||
* [__call()](#__call)
|
||||
* [end()](#end)
|
||||
* [close()](#close)
|
||||
* [error event](#error-event)
|
||||
* [close event](#close-event)
|
||||
* [Install](#install)
|
||||
* [Tests](#tests)
|
||||
* [License](#license)
|
||||
|
||||
## Support us
|
||||
|
||||
We invest a lot of time developing, maintaining and updating our awesome
|
||||
open-source projects. You can help us sustain this high-quality of our work by
|
||||
[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Sponsors get
|
||||
numerous benefits in return, see our [sponsoring page](https://github.com/sponsors/clue)
|
||||
for details.
|
||||
|
||||
Let's take these projects to the next level together! 🚀
|
||||
|
||||
## Quickstart example
|
||||
|
||||
Once [installed](#install), you can use the following code to connect to your
|
||||
local Redis server and send some requests:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$factory = new Clue\React\Redis\Factory();
|
||||
$redis = $factory->createLazyClient('localhost:6379');
|
||||
|
||||
$redis->set('greeting', 'Hello world');
|
||||
$redis->append('greeting', '!');
|
||||
|
||||
$redis->get('greeting')->then(function ($greeting) {
|
||||
// Hello world!
|
||||
echo $greeting . PHP_EOL;
|
||||
});
|
||||
|
||||
$redis->incr('invocation')->then(function ($n) {
|
||||
echo 'This is invocation #' . $n . PHP_EOL;
|
||||
});
|
||||
|
||||
// end connection once all pending requests have been resolved
|
||||
$redis->end();
|
||||
```
|
||||
|
||||
See also the [examples](examples).
|
||||
|
||||
## Usage
|
||||
|
||||
### Commands
|
||||
|
||||
Most importantly, this project provides a [`Client`](#client) instance that
|
||||
can be used to invoke all [Redis commands](https://redis.io/commands) (such as `GET`, `SET`, etc.).
|
||||
|
||||
```php
|
||||
$redis->get($key);
|
||||
$redis->set($key, $value);
|
||||
$redis->exists($key);
|
||||
$redis->expire($key, $seconds);
|
||||
$redis->mget($key1, $key2, $key3);
|
||||
|
||||
$redis->multi();
|
||||
$redis->exec();
|
||||
|
||||
$redis->publish($channel, $payload);
|
||||
$redis->subscribe($channel);
|
||||
|
||||
$redis->ping();
|
||||
$redis->select($database);
|
||||
|
||||
// many more…
|
||||
```
|
||||
|
||||
Each method call matches the respective [Redis command](https://redis.io/commands).
|
||||
For example, the `$redis->get()` method will invoke the [`GET` command](https://redis.io/commands/get).
|
||||
|
||||
All [Redis commands](https://redis.io/commands) are automatically available as
|
||||
public methods via the magic [`__call()` method](#__call).
|
||||
Listing all available commands is out of scope here, please refer to the
|
||||
[Redis command reference](https://redis.io/commands).
|
||||
|
||||
Any arguments passed to the method call will be forwarded as command arguments.
|
||||
For example, the `$redis->set('name', 'Alice')` call will perform the equivalent of a
|
||||
`SET name Alice` command. It's safe to pass integer arguments where applicable (for
|
||||
example `$redis->expire($key, 60)`), but internally Redis requires all arguments to
|
||||
always be coerced to string values.
|
||||
|
||||
Each of these commands supports async operation and returns a [Promise](#promises)
|
||||
that eventually *fulfills* with its *results* on success or *rejects* with an
|
||||
`Exception` on error. See also the following section about [promises](#promises)
|
||||
for more details.
|
||||
|
||||
### Promises
|
||||
|
||||
Sending commands is async (non-blocking), so you can actually send multiple
|
||||
commands in parallel.
|
||||
Redis will respond to each command request with a response message, pending
|
||||
commands will be pipelined automatically.
|
||||
|
||||
Sending commands uses a [Promise](https://github.com/reactphp/promise)-based
|
||||
interface that makes it easy to react to when a command is completed
|
||||
(i.e. either successfully fulfilled or rejected with an error):
|
||||
|
||||
```php
|
||||
$redis->get($key)->then(function (?string $value) {
|
||||
var_dump($value);
|
||||
}, function (Exception $e) {
|
||||
echo 'Error: ' . $e->getMessage() . PHP_EOL;
|
||||
});
|
||||
```
|
||||
|
||||
### PubSub
|
||||
|
||||
This library is commonly used to efficiently transport messages using Redis'
|
||||
[Pub/Sub](https://redis.io/topics/pubsub) (Publish/Subscribe) channels. For
|
||||
instance, this can be used to distribute single messages to a larger number
|
||||
of subscribers (think horizontal scaling for chat-like applications) or as an
|
||||
efficient message transport in distributed systems (microservice architecture).
|
||||
|
||||
The [`PUBLISH` command](https://redis.io/commands/publish) can be used to
|
||||
send a message to all clients currently subscribed to a given channel:
|
||||
|
||||
```php
|
||||
$channel = 'user';
|
||||
$message = json_encode(array('id' => 10));
|
||||
$redis->publish($channel, $message);
|
||||
```
|
||||
|
||||
The [`SUBSCRIBE` command](https://redis.io/commands/subscribe) can be used to
|
||||
subscribe to a channel and then receive incoming PubSub `message` events:
|
||||
|
||||
```php
|
||||
$channel = 'user';
|
||||
$redis->subscribe($channel);
|
||||
|
||||
$redis->on('message', function ($channel, $payload) {
|
||||
// pubsub message received on given $channel
|
||||
var_dump($channel, json_decode($payload));
|
||||
});
|
||||
```
|
||||
|
||||
Likewise, you can use the same client connection to subscribe to multiple
|
||||
channels by simply executing this command multiple times:
|
||||
|
||||
```php
|
||||
$redis->subscribe('user.register');
|
||||
$redis->subscribe('user.join');
|
||||
$redis->subscribe('user.leave');
|
||||
```
|
||||
|
||||
Similarly, the [`PSUBSCRIBE` command](https://redis.io/commands/psubscribe) can
|
||||
be used to subscribe to all channels matching a given pattern and then receive
|
||||
all incoming PubSub messages with the `pmessage` event:
|
||||
|
||||
|
||||
```php
|
||||
$pattern = 'user.*';
|
||||
$redis->psubscribe($pattern);
|
||||
|
||||
$redis->on('pmessage', function ($pattern, $channel, $payload) {
|
||||
// pubsub message received matching given $pattern
|
||||
var_dump($channel, json_decode($payload));
|
||||
});
|
||||
```
|
||||
|
||||
Once you're in a subscribed state, Redis no longer allows executing any other
|
||||
commands on the same client connection. This is commonly worked around by simply
|
||||
creating a second client connection and dedicating one client connection solely
|
||||
for PubSub subscriptions and the other for all other commands.
|
||||
|
||||
The [`UNSUBSCRIBE` command](https://redis.io/commands/unsubscribe) and
|
||||
[`PUNSUBSCRIBE` command](https://redis.io/commands/punsubscribe) can be used to
|
||||
unsubscribe from active subscriptions if you're no longer interested in
|
||||
receiving any further events for the given channel and pattern subscriptions
|
||||
respectively:
|
||||
|
||||
```php
|
||||
$redis->subscribe('user');
|
||||
|
||||
Loop::addTimer(60.0, function () use ($redis) {
|
||||
$redis->unsubscribe('user');
|
||||
});
|
||||
```
|
||||
|
||||
Likewise, once you've unsubscribed the last channel and pattern, the client
|
||||
connection is no longer in a subscribed state and you can issue any other
|
||||
command over this client connection again.
|
||||
|
||||
Each of the above methods follows normal request-response semantics and return
|
||||
a [`Promise`](#promises) to await successful subscriptions. Note that while
|
||||
Redis allows a variable number of arguments for each of these commands, this
|
||||
library is currently limited to single arguments for each of these methods in
|
||||
order to match exactly one response to each command request. As an alternative,
|
||||
the methods can simply be invoked multiple times with one argument each.
|
||||
|
||||
Additionally, can listen for the following PubSub events to get notifications
|
||||
about subscribed/unsubscribed channels and patterns:
|
||||
|
||||
```php
|
||||
$redis->on('subscribe', function ($channel, $total) {
|
||||
// subscribed to given $channel
|
||||
});
|
||||
$redis->on('psubscribe', function ($pattern, $total) {
|
||||
// subscribed to matching given $pattern
|
||||
});
|
||||
$redis->on('unsubscribe', function ($channel, $total) {
|
||||
// unsubscribed from given $channel
|
||||
});
|
||||
$redis->on('punsubscribe', function ($pattern, $total) {
|
||||
// unsubscribed from matching given $pattern
|
||||
});
|
||||
```
|
||||
|
||||
When using the [`createLazyClient()`](#createlazyclient) method, the `unsubscribe`
|
||||
and `punsubscribe` events will be invoked automatically when the underlying
|
||||
connection is lost. This gives you control over re-subscribing to the channels
|
||||
and patterns as appropriate.
|
||||
|
||||
## API
|
||||
|
||||
### Factory
|
||||
|
||||
The `Factory` is responsible for creating your [`Client`](#client) instance.
|
||||
|
||||
```php
|
||||
$factory = new Clue\React\Redis\Factory();
|
||||
```
|
||||
|
||||
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
|
||||
pass the event loop instance to use for this object. You can use a `null` value
|
||||
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
|
||||
This value SHOULD NOT be given unless you're sure you want to explicitly use a
|
||||
given event loop instance.
|
||||
|
||||
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
|
||||
proxy servers etc.), you can explicitly pass a custom instance of the
|
||||
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
|
||||
|
||||
```php
|
||||
$connector = new React\Socket\Connector(array(
|
||||
'dns' => '127.0.0.1',
|
||||
'tcp' => array(
|
||||
'bindto' => '192.168.10.1:0'
|
||||
),
|
||||
'tls' => array(
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false
|
||||
)
|
||||
));
|
||||
|
||||
$factory = new Clue\React\Redis\Factory(null, $connector);
|
||||
```
|
||||
|
||||
#### createClient()
|
||||
|
||||
The `createClient(string $uri): PromiseInterface<Client,Exception>` method can be used to
|
||||
create a new [`Client`](#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).
|
||||
|
||||
```php
|
||||
$factory->createClient('localhost:6379')->then(
|
||||
function (Client $redis) {
|
||||
// client connected (and authenticated)
|
||||
},
|
||||
function (Exception $e) {
|
||||
// an error occurred while trying to connect (or authenticate) client
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
The method returns a [Promise](https://github.com/reactphp/promise) that
|
||||
will resolve with a [`Client`](#client)
|
||||
instance on success or will reject with an `Exception` if the URL is
|
||||
invalid or the connection or authentication fails.
|
||||
|
||||
The returned Promise is implemented in such a way that it can be
|
||||
cancelled when it is still pending. Cancelling a pending promise will
|
||||
reject its value with an Exception and will cancel the underlying TCP/IP
|
||||
connection attempt and/or Redis authentication.
|
||||
|
||||
```php
|
||||
$promise = $factory->createClient($uri);
|
||||
|
||||
Loop::addTimer(3.0, function () use ($promise) {
|
||||
$promise->cancel();
|
||||
});
|
||||
```
|
||||
|
||||
The `$redisUri` can be given in the
|
||||
[standard](https://www.iana.org/assignments/uri-schemes/prov/redis) form
|
||||
`[redis[s]://][:auth@]host[:port][/db]`.
|
||||
You can omit the URI scheme and port if you're connecting to the default port 6379:
|
||||
|
||||
```php
|
||||
// both are equivalent due to defaults being applied
|
||||
$factory->createClient('localhost');
|
||||
$factory->createClient('redis://localhost: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:
|
||||
|
||||
```php
|
||||
// all forms are equivalent
|
||||
$factory->createClient('redis://:h%40llo@localhost');
|
||||
$factory->createClient('redis://ignored:h%40llo@localhost');
|
||||
$factory->createClient('redis://localhost?password=h%40llo');
|
||||
```
|
||||
|
||||
You can optionally include a path that will be used to select (SELECT command) the right database:
|
||||
|
||||
```php
|
||||
// both forms are equivalent
|
||||
$factory->createClient('redis://localhost/2');
|
||||
$factory->createClient('redis://localhost?db=2');
|
||||
```
|
||||
|
||||
You can use the [standard](https://www.iana.org/assignments/uri-schemes/prov/rediss)
|
||||
`rediss://` URI scheme if you're using a secure TLS proxy in front of Redis:
|
||||
|
||||
```php
|
||||
$factory->createClient('rediss://redis.example.com:6340');
|
||||
```
|
||||
|
||||
You can use the `redis+unix://` URI scheme if your Redis instance is listening
|
||||
on a Unix domain socket (UDS) path:
|
||||
|
||||
```php
|
||||
$factory->createClient('redis+unix:///tmp/redis.sock');
|
||||
|
||||
// the URI MAY contain `password` and `db` query parameters as seen above
|
||||
$factory->createClient('redis+unix:///tmp/redis.sock?password=secret&db=2');
|
||||
|
||||
// the URI MAY contain authentication details as userinfo as seen above
|
||||
// should be used with care, also note that database can not be passed as path
|
||||
$factory->createClient('redis+unix://:secret@/tmp/redis.sock');
|
||||
```
|
||||
|
||||
This method respects PHP's `default_socket_timeout` setting (default 60s)
|
||||
as a timeout for establishing the 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:
|
||||
|
||||
```php
|
||||
$factory->createClient('localhost?timeout=0.5');
|
||||
```
|
||||
|
||||
#### createLazyClient()
|
||||
|
||||
The `createLazyClient(string $uri): Client` method can be used to
|
||||
create a new [`Client`](#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).
|
||||
|
||||
```php
|
||||
$redis = $factory->createLazyClient('localhost:6379');
|
||||
|
||||
$redis->incr('hello');
|
||||
$redis->end();
|
||||
```
|
||||
|
||||
This method immediately returns a "virtual" connection implementing the
|
||||
[`Client`](#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 having
|
||||
to 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 kept
|
||||
open for longer than the idle period.
|
||||
|
||||
While using PubSub channels (see `SUBSCRIBE` and `PSUBSCRIBE` commands), this client
|
||||
will 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-closed
|
||||
and no further commands can be enqueued. Similarly, calling `end()` on
|
||||
this 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 many
|
||||
simple use cases it may be easier to create a lazy connection.
|
||||
|
||||
The `$redisUri` can be given in the
|
||||
[standard](https://www.iana.org/assignments/uri-schemes/prov/redis) form
|
||||
`[redis[s]://][:auth@]host[:port][/db]`.
|
||||
You can omit the URI scheme and port if you're connecting to the default port 6379:
|
||||
|
||||
```php
|
||||
// both are equivalent due to defaults being applied
|
||||
$factory->createLazyClient('localhost');
|
||||
$factory->createLazyClient('redis://localhost: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:
|
||||
|
||||
```php
|
||||
// all forms are equivalent
|
||||
$factory->createLazyClient('redis://:h%40llo@localhost');
|
||||
$factory->createLazyClient('redis://ignored:h%40llo@localhost');
|
||||
$factory->createLazyClient('redis://localhost?password=h%40llo');
|
||||
```
|
||||
|
||||
You can optionally include a path that will be used to select (SELECT command) the right database:
|
||||
|
||||
```php
|
||||
// both forms are equivalent
|
||||
$factory->createLazyClient('redis://localhost/2');
|
||||
$factory->createLazyClient('redis://localhost?db=2');
|
||||
```
|
||||
|
||||
You can use the [standard](https://www.iana.org/assignments/uri-schemes/prov/rediss)
|
||||
`rediss://` URI scheme if you're using a secure TLS proxy in front of Redis:
|
||||
|
||||
```php
|
||||
$factory->createLazyClient('rediss://redis.example.com:6340');
|
||||
```
|
||||
|
||||
You can use the `redis+unix://` URI scheme if your Redis instance is listening
|
||||
on a Unix domain socket (UDS) path:
|
||||
|
||||
```php
|
||||
$factory->createLazyClient('redis+unix:///tmp/redis.sock');
|
||||
|
||||
// the URI MAY contain `password` and `db` query parameters as seen above
|
||||
$factory->createLazyClient('redis+unix:///tmp/redis.sock?password=secret&db=2');
|
||||
|
||||
// the URI MAY contain authentication details as userinfo as seen above
|
||||
// should be used with care, also note that database can not be passed as path
|
||||
$factory->createLazyClient('redis+unix://:secret@/tmp/redis.sock');
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```php
|
||||
$factory->createLazyClient('localhost?timeout=0.5');
|
||||
```
|
||||
|
||||
By default, this method will keep "idle" connections 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:
|
||||
|
||||
```php
|
||||
$factory->createLazyClient('localhost?idle=0.1');
|
||||
```
|
||||
|
||||
### Client
|
||||
|
||||
The `Client` is responsible for exchanging messages with Redis
|
||||
and keeps track of pending commands.
|
||||
|
||||
Besides defining a few methods, this interface also implements the
|
||||
`EventEmitterInterface` which allows you to react to certain events as documented below.
|
||||
|
||||
#### __call()
|
||||
|
||||
The `__call(string $name, string[] $args): PromiseInterface<mixed,Exception>` method can be used to
|
||||
invoke the given command.
|
||||
|
||||
This is a magic method that will be invoked when calling any Redis command on this instance.
|
||||
Each method call matches the respective [Redis command](https://redis.io/commands).
|
||||
For example, the `$redis->get()` method will invoke the [`GET` command](https://redis.io/commands/get).
|
||||
|
||||
```php
|
||||
$redis->get($key)->then(function (?string $value) {
|
||||
var_dump($value);
|
||||
}, function (Exception $e) {
|
||||
echo 'Error: ' . $e->getMessage() . PHP_EOL;
|
||||
});
|
||||
```
|
||||
|
||||
All [Redis commands](https://redis.io/commands) are automatically available as
|
||||
public methods via this magic `__call()` method.
|
||||
Listing all available commands is out of scope here, please refer to the
|
||||
[Redis command reference](https://redis.io/commands).
|
||||
|
||||
Any arguments passed to the method call will be forwarded as command arguments.
|
||||
For example, the `$redis->set('name', 'Alice')` call will perform the equivalent of a
|
||||
`SET name Alice` command. It's safe to pass integer arguments where applicable (for
|
||||
example `$redis->expire($key, 60)`), but internally Redis requires all arguments to
|
||||
always be coerced to string values.
|
||||
|
||||
Each of these commands supports async operation and returns a [Promise](#promises)
|
||||
that eventually *fulfills* with its *results* on success or *rejects* with an
|
||||
`Exception` on error. See also [promises](#promises) for more details.
|
||||
|
||||
#### end()
|
||||
|
||||
The `end():void` method can be used to
|
||||
soft-close the Redis connection once all pending commands are completed.
|
||||
|
||||
#### close()
|
||||
|
||||
The `close():void` method can be used to
|
||||
force-close the Redis connection and reject all pending commands.
|
||||
|
||||
#### error event
|
||||
|
||||
The `error` event will be emitted once a fatal error occurs, such as
|
||||
when the client connection is lost or is invalid.
|
||||
The event receives a single `Exception` argument for the error instance.
|
||||
|
||||
```php
|
||||
$redis->on('error', function (Exception $e) {
|
||||
echo 'Error: ' . $e->getMessage() . PHP_EOL;
|
||||
});
|
||||
```
|
||||
|
||||
This event will only be triggered for fatal errors and will be followed
|
||||
by closing the client connection. It is not to be confused with "soft"
|
||||
errors caused by invalid commands.
|
||||
|
||||
#### close event
|
||||
|
||||
The `close` event will be emitted once the client connection closes (terminates).
|
||||
|
||||
```php
|
||||
$redis->on('close', function () {
|
||||
echo 'Connection closed' . PHP_EOL;
|
||||
});
|
||||
```
|
||||
|
||||
See also the [`close()`](#close) method.
|
||||
|
||||
## Install
|
||||
|
||||
The recommended way to install this library is [through Composer](https://getcomposer.org/).
|
||||
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
|
||||
|
||||
This project follows [SemVer](https://semver.org/).
|
||||
This will install the latest supported version:
|
||||
|
||||
```bash
|
||||
$ composer require clue/redis-react:^2.8
|
||||
```
|
||||
|
||||
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
|
||||
|
||||
This project aims to run on any platform and thus does not require any PHP
|
||||
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
|
||||
HHVM.
|
||||
It's *highly recommended to use the latest supported PHP version* for this project.
|
||||
|
||||
## Tests
|
||||
|
||||
To run the test suite, you first need to clone this repo and then install all
|
||||
dependencies [through Composer](https://getcomposer.org/):
|
||||
|
||||
```bash
|
||||
$ composer install
|
||||
```
|
||||
|
||||
To run the test suite, go to the project root and run:
|
||||
|
||||
```bash
|
||||
$ vendor/bin/phpunit
|
||||
```
|
||||
|
||||
The test suite contains both unit tests and functional integration tests.
|
||||
The functional tests require access to a running Redis server instance
|
||||
and will be skipped by default.
|
||||
|
||||
If you don't have access to a running Redis server, you can also use a temporary `Redis` Docker image:
|
||||
|
||||
```bash
|
||||
$ docker run --net=host redis
|
||||
```
|
||||
|
||||
To now run the functional tests, you need to supply *your* login
|
||||
details in an environment variable like this:
|
||||
|
||||
```bash
|
||||
$ REDIS_URI=localhost:6379 vendor/bin/phpunit
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is released under the permissive [MIT license](LICENSE).
|
||||
|
||||
> Did you know that I offer custom development services and issuing invoices for
|
||||
sponsorships of releases and for contributions? Contact me (@clue) for details.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "clue/redis-react",
|
||||
"description": "Async Redis client implementation, built on top of ReactPHP.",
|
||||
"keywords": ["Redis", "database", "client", "async", "ReactPHP"],
|
||||
"homepage": "https://github.com/clue/reactphp-redis",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Christian Lück",
|
||||
"email": "christian@clue.engineering"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3",
|
||||
"clue/redis-protocol": "^0.3.2",
|
||||
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
|
||||
"react/event-loop": "^1.2",
|
||||
"react/promise": "^3.2 || ^2.0 || ^1.1",
|
||||
"react/promise-timer": "^1.11",
|
||||
"react/socket": "^1.16"
|
||||
},
|
||||
"require-dev": {
|
||||
"clue/block-react": "^1.5",
|
||||
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Clue\\React\\Redis\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Clue\\Tests\\React\\Redis\\": "tests/"
|
||||
}
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Clue\React\Redis;
|
||||
|
||||
use Evenement\EventEmitterInterface;
|
||||
use React\Promise\PromiseInterface;
|
||||
|
||||
/**
|
||||
* Simple interface for executing redis commands
|
||||
*
|
||||
* @event error(Exception $error)
|
||||
* @event close()
|
||||
*
|
||||
* @event message($channel, $message)
|
||||
* @event subscribe($channel, $numberOfChannels)
|
||||
* @event unsubscribe($channel, $numberOfChannels)
|
||||
*
|
||||
* @event pmessage($pattern, $channel, $message)
|
||||
* @event psubscribe($channel, $numberOfChannels)
|
||||
* @event punsubscribe($channel, $numberOfChannels)
|
||||
*/
|
||||
interface Client extends EventEmitterInterface
|
||||
{
|
||||
/**
|
||||
* Invoke the given command and return a Promise that will be fulfilled when the request has been replied to
|
||||
*
|
||||
* This is a magic method that will be invoked when calling any redis
|
||||
* command on this instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string[] $args
|
||||
* @return PromiseInterface Promise<mixed,Exception>
|
||||
*/
|
||||
public function __call($name, $args);
|
||||
|
||||
/**
|
||||
* end connection once all pending requests have been replied to
|
||||
*
|
||||
* @return void
|
||||
* @uses self::close() once all replies have been received
|
||||
* @see self::close() for closing the connection immediately
|
||||
*/
|
||||
public function end();
|
||||
|
||||
/**
|
||||
* close connection immediately
|
||||
*
|
||||
* This will emit the "close" event.
|
||||
*
|
||||
* @return void
|
||||
* @see self::end() for closing the connection once the client is idle
|
||||
*/
|
||||
public function close();
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
namespace Clue\React\Redis;
|
||||
|
||||
use Clue\Redis\Protocol\Factory as ProtocolFactory;
|
||||
use React\EventLoop\Loop;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\Timer\TimeoutException;
|
||||
use React\Socket\ConnectionInterface;
|
||||
use React\Socket\Connector;
|
||||
use React\Socket\ConnectorInterface;
|
||||
|
||||
class Factory
|
||||
{
|
||||
/** @var LoopInterface */
|
||||
private $loop;
|
||||
|
||||
/** @var ConnectorInterface */
|
||||
private $connector;
|
||||
|
||||
/** @var ProtocolFactory */
|
||||
private $protocol;
|
||||
|
||||
/**
|
||||
* @param ?LoopInterface $loop
|
||||
* @param ?ConnectorInterface $connector
|
||||
* @param ?ProtocolFactory $protocol (internal, should not usually be passed)
|
||||
*/
|
||||
public function __construct($loop = null, $connector = null, $protocol = null)
|
||||
{
|
||||
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
|
||||
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
|
||||
}
|
||||
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
|
||||
throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
|
||||
}
|
||||
if ($protocol !== null && !$protocol instanceof ProtocolFactory) { // manual type check to support legacy PHP < 7.1
|
||||
throw new \InvalidArgumentException('Argument #3 ($protocol) expected null|Clue\Redis\Protocol\Factory');
|
||||
}
|
||||
|
||||
$this->loop = $loop ?: Loop::get();
|
||||
$this->connector = $connector ?: new Connector(array(), $this->loop);
|
||||
$this->protocol = $protocol ?: new ProtocolFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Redis client connected to address of given redis instance
|
||||
*
|
||||
* @param string $uri Redis server URI to connect to
|
||||
* @return \React\Promise\PromiseInterface<Client,\Exception> Promise that will
|
||||
* be fulfilled with `Client` on success or rejects with `\Exception` on error.
|
||||
*/
|
||||
public function createClient($uri)
|
||||
{
|
||||
// support `redis+unix://` scheme for Unix domain socket (UDS) paths
|
||||
if (preg_match('/^(redis\+unix:\/\/(?:[^:]*:[^@]*@)?)(.+?)?$/', $uri, $match)) {
|
||||
$parts = parse_url($match[1] . 'localhost/' . $match[2]);
|
||||
} else {
|
||||
if (strpos($uri, '://') === false) {
|
||||
$uri = 'redis://' . $uri;
|
||||
}
|
||||
|
||||
$parts = parse_url($uri);
|
||||
}
|
||||
|
||||
$uri = preg_replace(array('/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'), '$1***$2', $uri);
|
||||
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('redis', 'rediss', 'redis+unix'))) {
|
||||
return \React\Promise\reject(new \InvalidArgumentException(
|
||||
'Invalid Redis URI given (EINVAL)',
|
||||
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22
|
||||
));
|
||||
}
|
||||
|
||||
$args = array();
|
||||
parse_str(isset($parts['query']) ? $parts['query'] : '', $args);
|
||||
|
||||
$authority = $parts['host'] . ':' . (isset($parts['port']) ? $parts['port'] : 6379);
|
||||
if ($parts['scheme'] === 'rediss') {
|
||||
$authority = 'tls://' . $authority;
|
||||
} elseif ($parts['scheme'] === 'redis+unix') {
|
||||
$authority = 'unix://' . substr($parts['path'], 1);
|
||||
unset($parts['path']);
|
||||
}
|
||||
$connecting = $this->connector->connect($authority);
|
||||
|
||||
$deferred = new Deferred(function ($_, $reject) use ($connecting, $uri) {
|
||||
// connection cancelled, start with rejecting attempt, then clean up
|
||||
$reject(new \RuntimeException(
|
||||
'Connection to ' . $uri . ' cancelled (ECONNABORTED)',
|
||||
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
|
||||
));
|
||||
|
||||
// either close successful connection or cancel pending connection attempt
|
||||
$connecting->then(function (ConnectionInterface $connection) {
|
||||
$connection->close();
|
||||
}, function () {
|
||||
// ignore to avoid reporting unhandled rejection
|
||||
});
|
||||
$connecting->cancel();
|
||||
});
|
||||
|
||||
$protocol = $this->protocol;
|
||||
$promise = $connecting->then(function (ConnectionInterface $stream) use ($protocol) {
|
||||
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer());
|
||||
}, function (\Exception $e) use ($uri) {
|
||||
throw new \RuntimeException(
|
||||
'Connection to ' . $uri . ' failed: ' . $e->getMessage(),
|
||||
$e->getCode(),
|
||||
$e
|
||||
);
|
||||
});
|
||||
|
||||
// use `?password=secret` query or `user:secret@host` password form URL
|
||||
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null);
|
||||
if (isset($args['password']) || isset($parts['pass'])) {
|
||||
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']);
|
||||
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) {
|
||||
return $redis->auth($pass)->then(
|
||||
function () use ($redis) {
|
||||
return $redis;
|
||||
},
|
||||
function (\Exception $e) use ($redis, $uri) {
|
||||
$redis->close();
|
||||
|
||||
$const = '';
|
||||
$errno = $e->getCode();
|
||||
if ($errno === 0) {
|
||||
$const = ' (EACCES)';
|
||||
$errno = $e->getCode() ?: (defined('SOCKET_EACCES') ? SOCKET_EACCES : 13);
|
||||
}
|
||||
|
||||
throw new \RuntimeException(
|
||||
'Connection to ' . $uri . ' failed during AUTH command: ' . $e->getMessage() . $const,
|
||||
$errno,
|
||||
$e
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// use `?db=1` query or `/1` path (skip first slash)
|
||||
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
|
||||
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1);
|
||||
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) {
|
||||
return $redis->select($db)->then(
|
||||
function () use ($redis) {
|
||||
return $redis;
|
||||
},
|
||||
function (\Exception $e) use ($redis, $uri) {
|
||||
$redis->close();
|
||||
|
||||
$const = '';
|
||||
$errno = $e->getCode();
|
||||
if ($errno === 0 && strpos($e->getMessage(), 'NOAUTH ') === 0) {
|
||||
$const = ' (EACCES)';
|
||||
$errno = defined('SOCKET_EACCES') ? SOCKET_EACCES : 13;
|
||||
} elseif ($errno === 0) {
|
||||
$const = ' (ENOENT)';
|
||||
$errno = defined('SOCKET_ENOENT') ? SOCKET_ENOENT : 2;
|
||||
}
|
||||
|
||||
throw new \RuntimeException(
|
||||
'Connection to ' . $uri . ' failed during SELECT command: ' . $e->getMessage() . $const,
|
||||
$errno,
|
||||
$e
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
$promise->then(array($deferred, 'resolve'), array($deferred, 'reject'));
|
||||
|
||||
// use timeout from explicit ?timeout=x parameter or default to PHP's default_socket_timeout (60)
|
||||
$timeout = isset($args['timeout']) ? (float) $args['timeout'] : (int) ini_get("default_socket_timeout");
|
||||
if ($timeout < 0) {
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) {
|
||||
if ($e instanceof TimeoutException) {
|
||||
throw new \RuntimeException(
|
||||
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds (ETIMEDOUT)',
|
||||
defined('SOCKET_ETIMEDOUT') ? SOCKET_ETIMEDOUT : 110
|
||||
);
|
||||
}
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Redis client connected to address of given redis instance
|
||||
*
|
||||
* @param string $target
|
||||
* @return Client
|
||||
*/
|
||||
public function createLazyClient($target)
|
||||
{
|
||||
return new LazyClient($target, $this, $this->loop);
|
||||
}
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
|
||||
namespace Clue\React\Redis;
|
||||
|
||||
use Evenement\EventEmitter;
|
||||
use React\Stream\Util;
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class LazyClient extends EventEmitter implements Client
|
||||
{
|
||||
private $target;
|
||||
/** @var Factory */
|
||||
private $factory;
|
||||
private $closed = false;
|
||||
private $promise;
|
||||
|
||||
private $loop;
|
||||
private $idlePeriod = 60.0;
|
||||
private $idleTimer;
|
||||
private $pending = 0;
|
||||
|
||||
private $subscribed = array();
|
||||
private $psubscribed = array();
|
||||
|
||||
/**
|
||||
* @param $target
|
||||
*/
|
||||
public function __construct($target, Factory $factory, LoopInterface $loop)
|
||||
{
|
||||
$args = array();
|
||||
\parse_str((string) \parse_url($target, \PHP_URL_QUERY), $args);
|
||||
if (isset($args['idle'])) {
|
||||
$this->idlePeriod = (float)$args['idle'];
|
||||
}
|
||||
|
||||
$this->target = $target;
|
||||
$this->factory = $factory;
|
||||
$this->loop = $loop;
|
||||
}
|
||||
|
||||
private function client()
|
||||
{
|
||||
if ($this->promise !== null) {
|
||||
return $this->promise;
|
||||
}
|
||||
|
||||
$self = $this;
|
||||
$pending =& $this->promise;
|
||||
$idleTimer=& $this->idleTimer;
|
||||
$subscribed =& $this->subscribed;
|
||||
$psubscribed =& $this->psubscribed;
|
||||
$loop = $this->loop;
|
||||
return $pending = $this->factory->createClient($this->target)->then(function (Client $redis) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
|
||||
// connection completed => remember only until closed
|
||||
$redis->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
|
||||
$pending = null;
|
||||
|
||||
// foward unsubscribe/punsubscribe events when underlying connection closes
|
||||
$n = count($subscribed);
|
||||
foreach ($subscribed as $channel => $_) {
|
||||
$self->emit('unsubscribe', array($channel, --$n));
|
||||
}
|
||||
$n = count($psubscribed);
|
||||
foreach ($psubscribed as $pattern => $_) {
|
||||
$self->emit('punsubscribe', array($pattern, --$n));
|
||||
}
|
||||
$subscribed = array();
|
||||
$psubscribed = array();
|
||||
|
||||
if ($idleTimer !== null) {
|
||||
$loop->cancelTimer($idleTimer);
|
||||
$idleTimer = null;
|
||||
}
|
||||
});
|
||||
|
||||
// keep track of all channels and patterns this connection is subscribed to
|
||||
$redis->on('subscribe', function ($channel) use (&$subscribed) {
|
||||
$subscribed[$channel] = true;
|
||||
});
|
||||
$redis->on('psubscribe', function ($pattern) use (&$psubscribed) {
|
||||
$psubscribed[$pattern] = true;
|
||||
});
|
||||
$redis->on('unsubscribe', function ($channel) use (&$subscribed) {
|
||||
unset($subscribed[$channel]);
|
||||
});
|
||||
$redis->on('punsubscribe', function ($pattern) use (&$psubscribed) {
|
||||
unset($psubscribed[$pattern]);
|
||||
});
|
||||
|
||||
Util::forwardEvents(
|
||||
$redis,
|
||||
$self,
|
||||
array(
|
||||
'message',
|
||||
'subscribe',
|
||||
'unsubscribe',
|
||||
'pmessage',
|
||||
'psubscribe',
|
||||
'punsubscribe',
|
||||
)
|
||||
);
|
||||
|
||||
return $redis;
|
||||
}, function (\Exception $e) use (&$pending) {
|
||||
// connection failed => discard connection attempt
|
||||
$pending = null;
|
||||
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
|
||||
public function __call($name, $args)
|
||||
{
|
||||
if ($this->closed) {
|
||||
return \React\Promise\reject(new \RuntimeException(
|
||||
'Connection closed (ENOTCONN)',
|
||||
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
|
||||
));
|
||||
}
|
||||
|
||||
$that = $this;
|
||||
return $this->client()->then(function (Client $redis) use ($name, $args, $that) {
|
||||
$that->awake();
|
||||
return \call_user_func_array(array($redis, $name), $args)->then(
|
||||
function ($result) use ($that) {
|
||||
$that->idle();
|
||||
return $result;
|
||||
},
|
||||
function ($error) use ($that) {
|
||||
$that->idle();
|
||||
throw $error;
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public function end()
|
||||
{
|
||||
if ($this->promise === null) {
|
||||
$this->close();
|
||||
}
|
||||
|
||||
if ($this->closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
$that = $this;
|
||||
return $this->client()->then(function (Client $redis) use ($that) {
|
||||
$redis->on('close', function () use ($that) {
|
||||
$that->close();
|
||||
});
|
||||
$redis->end();
|
||||
});
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if ($this->closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->closed = true;
|
||||
|
||||
// either close active connection or cancel pending connection attempt
|
||||
if ($this->promise !== null) {
|
||||
$this->promise->then(function (Client $redis) {
|
||||
$redis->close();
|
||||
}, function () {
|
||||
// ignore to avoid reporting unhandled rejection
|
||||
});
|
||||
if ($this->promise !== null) {
|
||||
$this->promise->cancel();
|
||||
$this->promise = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->idleTimer !== null) {
|
||||
$this->loop->cancelTimer($this->idleTimer);
|
||||
$this->idleTimer = null;
|
||||
}
|
||||
|
||||
$this->emit('close');
|
||||
$this->removeAllListeners();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function awake()
|
||||
{
|
||||
++$this->pending;
|
||||
|
||||
if ($this->idleTimer !== null) {
|
||||
$this->loop->cancelTimer($this->idleTimer);
|
||||
$this->idleTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function idle()
|
||||
{
|
||||
--$this->pending;
|
||||
|
||||
if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
|
||||
$idleTimer =& $this->idleTimer;
|
||||
$promise =& $this->promise;
|
||||
$idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) {
|
||||
$promise->then(function (Client $redis) {
|
||||
$redis->close();
|
||||
});
|
||||
$promise = null;
|
||||
$idleTimer = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
namespace Clue\React\Redis;
|
||||
|
||||
use Clue\Redis\Protocol\Factory as ProtocolFactory;
|
||||
use Clue\Redis\Protocol\Model\ErrorReply;
|
||||
use Clue\Redis\Protocol\Model\ModelInterface;
|
||||
use Clue\Redis\Protocol\Model\MultiBulkReply;
|
||||
use Clue\Redis\Protocol\Parser\ParserException;
|
||||
use Clue\Redis\Protocol\Parser\ParserInterface;
|
||||
use Clue\Redis\Protocol\Serializer\SerializerInterface;
|
||||
use Evenement\EventEmitter;
|
||||
use React\Promise\Deferred;
|
||||
use React\Stream\DuplexStreamInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class StreamingClient extends EventEmitter implements Client
|
||||
{
|
||||
private $stream;
|
||||
private $parser;
|
||||
private $serializer;
|
||||
private $requests = array();
|
||||
private $ending = false;
|
||||
private $closed = false;
|
||||
|
||||
private $subscribed = 0;
|
||||
private $psubscribed = 0;
|
||||
|
||||
/**
|
||||
* @param DuplexStreamInterface $stream
|
||||
* @param ?ParserInterface $parser
|
||||
* @param ?SerializerInterface $serializer
|
||||
*/
|
||||
public function __construct(DuplexStreamInterface $stream, $parser = null, $serializer = null)
|
||||
{
|
||||
// manual type checks to support legacy PHP < 7.1
|
||||
assert($parser === null || $parser instanceof ParserInterface);
|
||||
assert($serializer === null || $serializer instanceof SerializerInterface);
|
||||
|
||||
if ($parser === null || $serializer === null) {
|
||||
$factory = new ProtocolFactory();
|
||||
if ($parser === null) {
|
||||
$parser = $factory->createResponseParser();
|
||||
}
|
||||
if ($serializer === null) {
|
||||
$serializer = $factory->createSerializer();
|
||||
}
|
||||
}
|
||||
|
||||
$that = $this;
|
||||
$stream->on('data', function($chunk) use ($parser, $that) {
|
||||
try {
|
||||
$models = $parser->pushIncoming($chunk);
|
||||
} catch (ParserException $error) {
|
||||
$that->emit('error', array(new \UnexpectedValueException(
|
||||
'Invalid data received: ' . $error->getMessage() . ' (EBADMSG)',
|
||||
defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG : 77,
|
||||
$error
|
||||
)));
|
||||
$that->close();
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($models as $data) {
|
||||
try {
|
||||
$that->handleMessage($data);
|
||||
} catch (\UnderflowException $error) {
|
||||
$that->emit('error', array($error));
|
||||
$that->close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$stream->on('close', array($this, 'close'));
|
||||
|
||||
$this->stream = $stream;
|
||||
$this->parser = $parser;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
public function __call($name, $args)
|
||||
{
|
||||
$request = new Deferred();
|
||||
$promise = $request->promise();
|
||||
|
||||
$name = strtolower($name);
|
||||
|
||||
// special (p)(un)subscribe commands only accept a single parameter and have custom response logic applied
|
||||
static $pubsubs = array('subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
|
||||
|
||||
if ($this->ending) {
|
||||
$request->reject(new \RuntimeException(
|
||||
'Connection ' . ($this->closed ? 'closed' : 'closing'). ' (ENOTCONN)',
|
||||
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
|
||||
));
|
||||
} elseif (count($args) !== 1 && in_array($name, $pubsubs)) {
|
||||
$request->reject(new \InvalidArgumentException(
|
||||
'PubSub commands limited to single argument (EINVAL)',
|
||||
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22
|
||||
));
|
||||
} elseif ($name === 'monitor') {
|
||||
$request->reject(new \BadMethodCallException(
|
||||
'MONITOR command explicitly not supported (ENOTSUP)',
|
||||
defined('SOCKET_ENOTSUP') ? SOCKET_ENOTSUP : (defined('SOCKET_EOPNOTSUPP') ? SOCKET_EOPNOTSUPP : 95)
|
||||
));
|
||||
} else {
|
||||
$this->stream->write($this->serializer->getRequestMessage($name, $args));
|
||||
$this->requests []= $request;
|
||||
}
|
||||
|
||||
if (in_array($name, $pubsubs)) {
|
||||
$that = $this;
|
||||
$subscribed =& $this->subscribed;
|
||||
$psubscribed =& $this->psubscribed;
|
||||
|
||||
$promise->then(function ($array) use ($that, &$subscribed, &$psubscribed) {
|
||||
$first = array_shift($array);
|
||||
|
||||
// (p)(un)subscribe messages are to be forwarded
|
||||
$that->emit($first, $array);
|
||||
|
||||
// remember number of (p)subscribe topics
|
||||
if ($first === 'subscribe' || $first === 'unsubscribe') {
|
||||
$subscribed = $array[1];
|
||||
} else {
|
||||
$psubscribed = $array[1];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $promise;
|
||||
}
|
||||
|
||||
public function handleMessage(ModelInterface $message)
|
||||
{
|
||||
if (($this->subscribed !== 0 || $this->psubscribed !== 0) && $message instanceof MultiBulkReply) {
|
||||
$array = $message->getValueNative();
|
||||
$first = array_shift($array);
|
||||
|
||||
// pub/sub messages are to be forwarded and should not be processed as request responses
|
||||
if (in_array($first, array('message', 'pmessage'))) {
|
||||
$this->emit($first, $array);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->requests) {
|
||||
throw new \UnderflowException(
|
||||
'Unexpected reply received, no matching request found (ENOMSG)',
|
||||
defined('SOCKET_ENOMSG') ? SOCKET_ENOMSG : 42
|
||||
);
|
||||
}
|
||||
|
||||
$request = array_shift($this->requests);
|
||||
assert($request instanceof Deferred);
|
||||
|
||||
if ($message instanceof ErrorReply) {
|
||||
$request->reject($message);
|
||||
} else {
|
||||
$request->resolve($message->getValueNative());
|
||||
}
|
||||
|
||||
if ($this->ending && !$this->requests) {
|
||||
$this->close();
|
||||
}
|
||||
}
|
||||
|
||||
public function end()
|
||||
{
|
||||
$this->ending = true;
|
||||
|
||||
if (!$this->requests) {
|
||||
$this->close();
|
||||
}
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if ($this->closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->ending = true;
|
||||
$this->closed = true;
|
||||
|
||||
$remoteClosed = $this->stream->isReadable() === false && $this->stream->isWritable() === false;
|
||||
$this->stream->close();
|
||||
|
||||
$this->emit('close');
|
||||
|
||||
// reject all remaining requests in the queue
|
||||
while ($this->requests) {
|
||||
$request = array_shift($this->requests);
|
||||
assert($request instanceof Deferred);
|
||||
|
||||
if ($remoteClosed) {
|
||||
$request->reject(new \RuntimeException(
|
||||
'Connection closed by peer (ECONNRESET)',
|
||||
defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104
|
||||
));
|
||||
} else {
|
||||
$request->reject(new \RuntimeException(
|
||||
'Connection closing (ECONNABORTED)',
|
||||
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user