PHPackages                             chrisharrison/lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. chrisharrison/lock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

chrisharrison/lock
==================

Avoid simultaneous execution of PHP code by first gaining a lock.

4.0.0(6y ago)02.8k11MITPHPPHP ^7.2

Since Jun 8Pushed 6y ago1 watchersCompare

[ Source](https://github.com/chrisharrison/lock)[ Packagist](https://packagist.org/packages/chrisharrison/lock)[ RSS](/packages/chrisharrison-lock/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (4)Dependencies (3)Versions (6)Used By (1)

lock
====

[](#lock)

Avoid simultaneous execution of PHP code by first gaining a lock.

[![Build Status](https://camo.githubusercontent.com/92bfb8602665f521148d8a289430620f6082fb19cef4f685d09b579b1a3ebd06/68747470733a2f2f7472617669732d63692e6f72672f63687269736861727269736f6e2f6c6f636b2e737667)](https://travis-ci.org/chrisharrison/lock)[![Version](https://camo.githubusercontent.com/f3cc9afc8d2cb298b9cf0b0860db314148ed94503e148bbdfafe88429f580322/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63687269736861727269736f6e2f6c6f636b2e737667)](https://packagist.org/packages/chrisharrison/lock)

Installation
------------

[](#installation)

Through Composer, obviously:

```
composer require chrisharrison/lock

```

Why?
----

[](#why)

If you have code that will potentially cause an unwanted race condition when two parallel processed run it at the same time, you may want to introduce a lock so that only one process can execute that code while others wait.

Usage
-----

[](#usage)

First create a `LockGuard`:

```
$lockGuard = new LockGuard(
    $maxAttempts,
    $attemptIntervalSeconds,
    $lockDriver,
    $lockInspector
);

```

- `$maxAttempts`: `int` *Maximum number of attempts to gain a lock before it gives up.*
- `$attemptIntervalSeconds`: `int` *Number of seconds between attempts to gain a lock*
- `$lockDriver`: `LockDriver` *An instance of a class which deals with persisting the lock to whatever storage mechanism*
- `$lockInspector`: `LockInspector` *An instance of a class which deals with testing validity of a lock*

Once you've create a `LockGuard` you can use it to protect code within a lock:

```
$flag = false;

$uniqueProcessId = '';
$lockUntil = DateTimeImmutable::createFromFormat('U', time()+300); // In 5 mins time

$didExecute = $lockGuard->protect('uniq-process-id', $lockUnitl, function () use (&$flag) {
  $flag = true;
});

```

The above will attempt to set `$flag` to `true`.

If there is a lock that hasn't expired and was not created by the same process (identified by `$uniqueProcessId`) then the code will execute and the method will return `true`. Else `false`. As soon as the code has been successfully executed, the lock will be released. This happens even if the `$lockUntil` time has not been reached. However if the code hasn't completed after the `$lockUntil` time has been reached then the lock will expire and other processes can execute again. This is to mitigate situations where a lock is never released.

### Typical usage

[](#typical-usage)

```
$lockPath = 'lock.json';

$maxAttempts = 5;
$attemptIntervalSeconds = 3;
$lockDriver = new FilesystemLockDriver($lockPath);
$lockInspector = new DefaultLockInspector;

$lockGuard = new LockGuard(
    $maxAttempts,
    $attemptIntervalSeconds,
    $lockDriver,
    $lockInspector
);

```

The `FilesystemLockDriver` persists the lock as JSON to a file using the local filesystem. You could create other `LockDriver`s that use other methods, such as [FlySystem](https://flysystem.thephpleague.com/docs/) to make use of S3.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~72 days

Total

4

Last Release

2314d ago

Major Versions

1.0.0 → 2.0.02019-06-09

2.0.0 → 3.0.02019-06-09

3.0.0 → 4.0.02020-01-13

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/296186?v=4)[Chris Harrison](/maintainers/chrisharrison)[@chrisharrison](https://github.com/chrisharrison)

---

Top Contributors

[![chrisharrison](https://avatars.githubusercontent.com/u/296186?v=4)](https://github.com/chrisharrison "chrisharrison (9 commits)")[![centralhawk3](https://avatars.githubusercontent.com/u/5446552?v=4)](https://github.com/centralhawk3 "centralhawk3 (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/chrisharrison-lock/health.svg)

```
[![Health](https://phpackages.com/badges/chrisharrison-lock/health.svg)](https://phpackages.com/packages/chrisharrison-lock)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
