2017 © Pedro Peláez
 

library mongo-lock

Distributed multi-reader lock using MongoDB

image

gaillard/mongo-lock

Distributed multi-reader lock using MongoDB

  • Tuesday, August 26, 2014
  • by gaillard
  • Repository
  • 2 Watchers
  • 3 Stars
  • 293 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

mongo-lock-php

Build Status, (*1)

Distributed multi-reader lock using MongoDB, (*2)

Requirements

Requires PHP 5.4.0 (or later)., (*3)

Installation

To add the library as a local, per-project dependency use Composer!, (*4)

{
    "require": {
        "gaillard/mongo-lock": "~1.0"
    }
}

Example

$writer = function($value) {
    $db = (new \MongoClient())->selectDB('locksExample');
    $data = $db->selectCollection('data');
    $locker = new Locker($db->selectCollection('locks'), 0);

    while (true) {
        $locker->writeLock('theId', 1000);

        $data->update(['_id' => 1], ['_id' => 1, 'key' => $value], ['upsert' => true]);
        $data->update(['_id' => 2], ['_id' => 2, 'key' => $value], ['upsert' => true]);
        $data->update(['_id' => 3], ['_id' => 3, 'key' => $value], ['upsert' => true]);
        $data->update(['_id' => 4], ['_id' => 4, 'key' => $value], ['upsert' => true]);

        $locker->writeUnlock('theId');
    }
};

$reader = function() {
    $db = (new \MongoClient())->selectDB('locksExample');
    $data = $db->selectCollection('data');
    $locker = new Locker($db->selectCollection('locks'), 100000);

    while (true) {
        $readerId = $locker->readLock('theId', 1000);

        foreach ($data->find()->sort(['_id' => 1]) as $doc) {
            echo "{$doc['key']} ";
        }

        echo "\n";

        $locker->readUnlock('theId', $readerId);

        usleep(100000);
    }
};

$writerOnePid = pcntl_fork();
if ($writerOnePid === 0) {
    $writer('pie');
    exit;
}

$writerTwoPid = pcntl_fork();
if ($writerTwoPid === 0) {
    $writer('cake');
    exit;
}

$readerOnePid = pcntl_fork();
if ($readerOnePid === 0) {
    $reader();
    exit;
}

$readerTwoPid = pcntl_fork();
if ($readerTwoPid === 0) {
    $reader();
    exit;
}

sleep(4);

posix_kill($writerOnePid, SIGTERM);
posix_kill($writerTwoPid, SIGTERM);
posix_kill($readerOnePid, SIGTERM);
posix_kill($readerTwoPid, SIGTERM);

prints something similiar too, (*5)

pie pie pie pie
pie pie pie pie pie
pie pie pie
pie pie pie pie pie pie pie pie

cake cake cake cake cake cake cake
cake
pie pie pie pie
cake cake cake cake
pie pie pie pie
cake cake cake cake

You'll notice that all the pies and cakes are always seperated by a newline. That is because there are no readers with a lock during writing. That also indicates both writers not writing at the same time. Sometimes when running the example there are more or less than four printed before a newline. That is the case when the two readers have a lock at the same time., (*6)

Contributing

If you would like to contribute, please use the build process for any changes and after the build passes, send a pull request on github!, (*7)

./build.php

The Versions

26/08 2014

dev-master

9999999-dev

Distributed multi-reader lock using MongoDB

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-mongo ~1.3

 

The Development Requires

mongo lock distributed

26/08 2014

v2.0.1

2.0.1.0

Distributed multi-reader lock using MongoDB

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-mongo ~1.3

 

The Development Requires

mongo lock distributed

30/07 2014

v2.0.0

2.0.0.0

Distributed multi-reader lock using MongoDB

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-mongo ~1.3

 

The Development Requires

mongo lock distributed

29/07 2014

v1.0.1

1.0.1.0

Distributed multi-reader lock using MongoDB

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-mongo ~1.3

 

The Development Requires

mongo lock distributed

29/07 2014

v1.0.0

1.0.0.0

Distributed multi-reader lock using MongoDB

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-mongo ~1.3

 

The Development Requires

mongo lock distributed