{"id":37011811,"url":"https://github.com/hokoo/wp-lock","last_synced_at":"2026-01-14T01:01:16.235Z","repository":{"id":243019181,"uuid":"796217873","full_name":"hokoo/wp-lock","owner":"hokoo","description":"Safety of the multi-process for WordPress","archived":false,"fork":false,"pushed_at":"2024-08-29T08:42:38.000Z","size":85,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-11T14:53:06.311Z","etag":null,"topics":["rece-condition","threads","wordpress","wp-lock"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hokoo.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-05T09:48:42.000Z","updated_at":"2024-08-29T08:42:41.000Z","dependencies_parsed_at":"2024-06-06T09:22:59.024Z","dependency_job_id":null,"html_url":"https://github.com/hokoo/wp-lock","commit_stats":null,"previous_names":["hokoo/wp-lock"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/hokoo/wp-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hokoo%2Fwp-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hokoo%2Fwp-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hokoo%2Fwp-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hokoo%2Fwp-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hokoo","download_url":"https://codeload.github.com/hokoo/wp-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hokoo%2Fwp-lock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28407636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T00:40:43.272Z","status":"ssl_error","status_checked_at":"2026-01-14T00:40:42.636Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["rece-condition","threads","wordpress","wp-lock"],"created_at":"2026-01-14T01:01:13.704Z","updated_at":"2026-01-14T01:01:15.957Z","avatar_url":"https://github.com/hokoo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `WP Lock`\n[![PHPUnit Tests](https://github.com/hokoo/wp-lock/actions/workflows/phpunit.yml/badge.svg)](https://github.com/hokoo/wp-lock/actions/workflows/phpunit.yml)\n\n\u003e This was previously a fork from original repo [soulseekah/wp-lock](https://github.com/soulseekah/wp-lock) by Gennady Kovshenin.\n\u003e Now here is the standalone repo with such changes as:\n\u003e - Custom database table is used as the WPDB locks' storage that allows to manage locks in simple and convenient way.\n\u003e - Fast: making one lock takes up to one single query instead of 11 and more.\n\u003e - Readable: the code is well documented and easy to understand.\n\n\n## WP Lock's Preconditions\n\nConsider the following user balance topup function that is susceptible to a race condition:\n\n```php\n// Top-up function that is not thread-safe\npublic function topup_user_balance( $user_id, $topup ) {\n\t$balance = get_user_meta( $user_id, 'balance', true );\n\t$balance = $balance + $topup;\n\tupdate_user_meta( $user_id, 'balance', $balance );\n\treturn $balance;\n}\n```\n\nTry to call the above code 100 times in 16 threads. The balance will be less than it is supposed to be.\n\n\nThe code below is thread safe.\n\n```php\n// A thread-safe version of the above topup function.\npublic function topup_user_balance( $user_id, $topup ) {\n\t$user_balance_lock = new WP_Lock( \"$user_id:meta:balance\" );\n\t$user_balance_lock-\u003eacquire( WP_Lock::WRITE );\n\n\t$balance = get_user_meta( $user_id, 'balance', true );\n\t$balance = $balance + $topup;\n\tupdate_user_meta( $user_id, 'balance', $balance );\n\n\t$user_balance_lock-\u003erelease();\n\n\treturn $balance;\n}\n```\n\n## Usage\n\nRequire via Composer `composer require hokoo/wp-lock` in your plugin.\n\nDon't forget to include the Composer autoloader in your plugin and declare using of the `WP_Lock` class.\n\n```php\nuse iTRON\\WP_Lock;\nrequire 'vendor/autoload.php';\n```\nAcquire a read blocking lock without a timeout.\n\n```php\n$lock = new WP_Lock\\WP_Lock( 'my-lock' );\n\n$lock-\u003eacquire( WP_Lock::READ, true, 0 );\n// do something, and then\n$lock-\u003erelease();\n```\n\n```php\npublic function acquire( $level = self::WRITE, $blocking = true, $expiration = 30 )\n```\n\n### Lock levels\n\n- `WP_Lock::READ` - other processes can acquire READ but not WRITE until the original lock is released. A shared read lock.\n- `WP_Lock::WRITE` (default) - other processes can't acquire READ or WRITE locks until the original lock is released. An exclusive read-write lock\n\n### Blocking policy\n* Blocking means that the process will wait here until the lock is obtained (5 microseconds spinlock).\n* Non-blocking lock acquisition does not wait for the other locks to be released, but returns false immediately if the lock is already acquired by another process. So, it should be checked for success.\n\n```php\nif ( $lock-\u003eacquire( WP_Lock::READ, false, 0 ) ) {\n    // do something, and then\n    $lock-\u003erelease();\n}\n```\n### Timeout policy\n\n* 0-second timeout means that the lock should be released, otherwise it will be released automatically after the php process is finished.\n* Non-zero timeout means that the lock might not be released manually, and then it will be done automatically after the specified number of seconds.\n\n## Lock Existence Check\n\nYou may also want to check if a lock is acquired by another process without actually trying to acquire it.\n\n```php\n$another_lock = new WP_Lock\\WP_Lock( 'my-lock' );\n\nif ( $another_lock-\u003eacquire( WP_Lock::READ, false, 0 ) ) {\n    $lock-\u003elock_exists( WP_Lock::READ ); // true\n    $lock-\u003elock_exists( WP_Lock::WRITE ); // false\n    \n    $another_lock-\u003erelease();\n}\n\nif ( $another_lock-\u003eacquire( WP_Lock::WRITE, false, 0 ) ) {\n    $lock-\u003elock_exists( WP_Lock::READ ); // true\n    $lock-\u003elock_exists( WP_Lock::WRITE ); // true\n    \n    $another_lock-\u003erelease();\n}\n```\n\n## Caveats\n\nIn highly concurrent setups you may get Deadlock errors from MySQL. This is normal. The library handles these gracefully and retries the query as needed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhokoo%2Fwp-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhokoo%2Fwp-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhokoo%2Fwp-lock/lists"}