Skip to content

Provides extra (in terms of "additional", "extraordinary", "special" and "unusual") decorators built upon connection-manager.

License

Notifications You must be signed in to change notification settings

clue-labs/reactphp-connection-manager-extra

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clue/connection-manager-extra Build Status

This project provides extra (in terms of "additional", "extraordinary", "special" and "unusual") decorators built upon react/socket-client.

Introduction

If you're not already familar with react/socket-client, think of it as an async (non-blocking) version of fsockopen() or stream_socket_client(). I.e. before you can send and receive data to/from a remote server, you first have to establish a connection - which takes its time because it involves several steps. In order to be able to establish several connections at the same time, react/socket-client provides a simple API to establish simple connections in an async (non-blocking) way.

This project includes several classes that extend this base functionality by implementing the same simple ConnectorInterface. This interface provides a single promise-based method create($host, $ip) which can be used to easily notify when the connection is successfully established or the Connector gives up and the connection fails.

$connector->create('www.google.com', 80)->then(function ($stream) {
    echo 'connection successfully established';
    $stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
    $stream->end();
}, function ($exception) {
    echo 'connection attempt failed: ' . $exception->getMessage();
});

Because everything uses the same simple API, the resulting Connector classes can be easily interchanged and be used in places that expect the normal ConnectorInterface. This can be used to stack them into each other, like using timeouts for TCP connections, delaying SSL/TLS connections, retrying failed connection attemps, randomly picking a Connector or any combination thereof.

Usage

This section lists all this libraries' features along with some examples. The examples assume you've installed this library and already set up a SocketClient/Connector instance $connector.

All classes are located in the ConnectionManager\Extra namespace.

Repeat

The ConnectionManagerRepeat($connector, $repeat) retries connecting to the given location up to a maximum of $repeat times when the connection fails.

$connectorRepeater = new \ConnectionManager\Extra\ConnectionManagerRepeat($connector, 3);
$connectorRepeater->create('www.google.com', 80)->then(function ($stream) {
    echo 'connection successfully established';
    $stream->close();
});

Timeout

The ConnectionManagerTimeout($connector, $timeout) sets a maximum $timeout in seconds on when to give up waiting for the connection to complete.

Delay

The ConnectionManagerDelay($connector, $delay) sets a fixed initial $delay in seconds before actually trying to connect. (Not to be confused with ConnectionManagerTimeout which sets a maximum timeout.)

Reject

The ConnectionManagerReject() simply rejects every single connection attempt. This is particularly useful for the below ConnectionManagerSelective to reject connection attempts to only certain destinations (for example blocking advertisements or harmful sites).

Swappable

The ConnectionManagerSwappable($connector) is a simple decorator for other ConnectionManagers to simplify exchanging the actual ConnectionManager during runtime (->setConnectionManager($connector)).

Consecutive

The ConnectionManagerConsecutive($connectors) establishs connections by trying to connect through any of the given ConnectionManagers in consecutive order until the first one succeeds.

Random

The ConnectionManagerRandom($connectors) works much like ConnectionManagerConsecutive but instead of using a fixed order, it always uses a randomly shuffled order.

Selective

The ConnectionManagerSelective() manages several Connectors and forwards connection through either of those besed on lists similar to to firewall or networking access control lists (ACLs).

This allows fine-grained control on how to handle outgoing connections, like rejecting advertisements, delaying HTTP requests, or forwarding HTTPS connection through a foreign country.

$connectorSelective->addConnectionManagerFor($connector, $targetHost, $targetPort, $priority);

Install

The recommended way to install this library is through composer. New to composer?

{
    "require": {
        "clue/connection-manager-extra": "0.3.*"
    }
}

License

MIT

About

Provides extra (in terms of "additional", "extraordinary", "special" and "unusual") decorators built upon connection-manager.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%