Giter VIP home page Giter VIP logo

reactphp-locker's Introduction

Locker

Async locker for ReactPHP

CircleCI

Table of Contents

Quickstart example

ReactPHP lockers are a bit different. Because they are built on top a non-blocking and asynchronous library, they have the power of being really reactive. That means that the performance increases so much, and no pulling is done in any locker adapter.

This is a small example that would properly work if all the resource lockers are alive in the same PHP thread.

$loop = React\EventLoop\Factory::create();
$adapter = new InMemoryAdapter($loop);
$locker = LockerFactory::create($adapter, 'res1');

$promise = $locker
    ->enqueue()
    ->then(function(Locker $locker) {
        // Do whatever
    })
    ->release();

Only one requester will have the possibility to work with the same resource at the same time, and all other resource clients will be just waiting, without asking the resource once and again.

Enqueue Timeout

Your resource clients can easily forget about them by using some timeout. If the resource is not a must, or simply you want to throw an exception when the resource is not been available for a while, then this is the place for this.

$promise = $locker
    ->enqueue(10)
    ->then(function(Locker $locker) {
        // Do whatever
    })
    ->release();

Adapters

You can select different adapters, depending on the tecnologies you work with, but as well, depending on the scope of your resource. It has no sense to work with a in-memory resource if your scope is a cluster of servers.

InMemory Adapter

The scope is the PHP thread

$adapter = new InMemoryAdapter($loop);
$locker = LockerFactory::create($adapter, 'res1');

Redis Adapter

This adapter can work really well for a cluster of services. Because of the way this adapter works and the behavior of the Redis library implementation for ReactPHP, this adapter needs two connections to work properly.

$Factory = new Factory();
$client1 = $factory->createLazyClient('localhost');
$client2 = $factory->createLazyClient('localhost');

$adapter = new RedisAdapter($loop, $client1, $client2);
$locker = LockerFactory::create($adapter, 'res1');

Install

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

This project follows SemVer. This will install the latest supported version:

$ composer require mmoreram/react-locker:dev-master

This library requires PHP7.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit

License

This project is released under the permissive MIT license.

reactphp-locker's People

Contributors

mmoreram avatar nanosector avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.