{"id":18818724,"url":"https://github.com/chrisharrison/lock","last_synced_at":"2025-10-11T19:33:17.851Z","repository":{"id":56952476,"uuid":"190903731","full_name":"chrisharrison/lock","owner":"chrisharrison","description":"Avoid simultaneous execution of PHP code by first gaining a lock.","archived":false,"fork":false,"pushed_at":"2020-01-13T11:15:12.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-11T19:33:15.332Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrisharrison.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-08T15:26:14.000Z","updated_at":"2020-01-13T11:12:46.000Z","dependencies_parsed_at":"2022-08-21T03:40:21.606Z","dependency_job_id":null,"html_url":"https://github.com/chrisharrison/lock","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/chrisharrison/lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Flock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Flock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Flock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Flock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisharrison","download_url":"https://codeload.github.com/chrisharrison/lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Flock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279008425,"owners_count":26084460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T00:18:05.422Z","updated_at":"2025-10-11T19:33:17.824Z","avatar_url":"https://github.com/chrisharrison.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lock\n\nAvoid simultaneous execution of PHP code by first gaining a lock.\n\n\n[![Build Status](https://travis-ci.org/chrisharrison/lock.svg)](https://travis-ci.org/chrisharrison/lock)\n[![Version](https://img.shields.io/packagist/v/chrisharrison/lock.svg)](https://packagist.org/packages/chrisharrison/lock)\n\n## Installation ##\n\nThrough Composer, obviously:\n\n```\ncomposer require chrisharrison/lock\n```\n\n## Why? ##\n\nIf 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.\n\n## Usage ##\n\nFirst create a `LockGuard`:\n```$php\n$lockGuard = new LockGuard(\n    $maxAttempts,\n    $attemptIntervalSeconds,\n    $lockDriver,\n    $lockInspector\n);\n```\n\n* `$maxAttempts`: `int` _Maximum number of attempts to gain a lock before it gives up._\n* `$attemptIntervalSeconds`: `int` _Number of seconds between attempts to gain a lock_\n* `$lockDriver`: `LockDriver` _An instance of a class which deals with persisting the lock to whatever storage mechanism_\n* `$lockInspector`: `LockInspector` _An instance of a class which deals with testing validity of a lock_\n\nOnce you've create a `LockGuard` you can use it to protect code within a lock:\n\n```$php\n$flag = false;\n\n$uniqueProcessId = '\u003cANY-UNIQUE-STRING\u003e';\n$lockUntil = DateTimeImmutable::createFromFormat('U', time()+300); // In 5 mins time\n\n$didExecute = $lockGuard-\u003eprotect('uniq-process-id', $lockUnitl, function () use (\u0026$flag) {\n  $flag = true;\n});\n```\n\nThe above will attempt to set `$flag` to `true`.\n\nIf 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`.\nAs 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.\n\n### Typical usage ###\n\n```$php\n$lockPath = 'lock.json';\n\n$maxAttempts = 5;\n$attemptIntervalSeconds = 3;\n$lockDriver = new FilesystemLockDriver($lockPath);\n$lockInspector = new DefaultLockInspector;\n\n$lockGuard = new LockGuard(\n    $maxAttempts,\n    $attemptIntervalSeconds,\n    $lockDriver,\n    $lockInspector\n);\n```\n\nThe `FilesystemLockDriver` persists the lock as JSON to a file using the local filesystem.\nYou could create other `LockDriver`s that use other methods, such as [FlySystem](https://flysystem.thephpleague.com/docs/) to make use of S3.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisharrison%2Flock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisharrison%2Flock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisharrison%2Flock/lists"}