{"id":20235338,"url":"https://github.com/phphleb/conductor","last_synced_at":"2025-06-21T05:37:25.152Z","repository":{"id":57040460,"uuid":"433421918","full_name":"phphleb/conductor","owner":"phphleb","description":"Mutexes for Framework HLEB2","archived":false,"fork":false,"pushed_at":"2024-11-24T22:07:44.000Z","size":58,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-26T02:58:43.435Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"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/phphleb.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-30T12:26:45.000Z","updated_at":"2025-03-03T20:50:31.000Z","dependencies_parsed_at":"2022-08-23T22:00:11.300Z","dependency_job_id":null,"html_url":"https://github.com/phphleb/conductor","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/phphleb/conductor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phphleb%2Fconductor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phphleb%2Fconductor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phphleb%2Fconductor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phphleb%2Fconductor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phphleb","download_url":"https://codeload.github.com/phphleb/conductor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phphleb%2Fconductor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261070675,"owners_count":23105465,"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","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-14T08:15:58.885Z","updated_at":"2025-06-21T05:37:20.137Z","avatar_url":"https://github.com/phphleb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":" \n### Use of mutexes in a project (including projects based on the HLEB2 framework)\n\n\n![PHP](https://img.shields.io/badge/PHP-^8.2-blue) [![License: MIT](https://img.shields.io/badge/License-MIT%20(Free)-brightgreen.svg)](https://github.com/phphleb/hleb/blob/master/LICENSE)\n\n\nThe use of mutexes is worthwhile in cases, when access to any code is to be locked, until it is executed in the current process or the set locking time period expires. \nFor example, repetitive simultaneous API requests can cause a parallel recording one and the same value into the data base. In order to avoid such event, a section of the code responsible for recording is to be transformed by mutex methods. There are only three such mutex methods: `acquire`, `release` and `unlock`.\n  \n ### FileMutex\n```php\nuse \\Phphleb\\Conductor\\FileMutex;\n\n$mutex = new FileMutex();\nif ($mutex-\u003eacquire('mutex-name', 20)) { // Start blocking\n       try {\n       // Custom code.\n\n       } catch (\\Throwable $e) {\n          $mutex-\u003eunlock('mutex-name'); // Force unlock\n          throw $e;\n       }\n   } else {\n       throw new \\Exception('Custom error text.');\n   }\nif (!$mutex-\u003erelease('mutex-name')) { // End of blocking\n   // Rolling back transactions\n}\n\n```\n\nWhen setting the time period for locking (the second argument `acquire` in seconds), it should be taken into account that, if an active process is unable to unlock the mutex on its own, other processes from the **non-sequential** queue that have addressed to this code will continue working only after this time period has expired. That is why they will be completed, in a case of a long delay, at the web server level by timeout waiting for a response from the script. \n\n#### Installation in a project based on the framework HLEB\n\n ```bash\ncomposer require phphleb/conductor\n```\nCreate a console commands `php console mutex/db-stat`, `php console mutex/predis-stat` and `php console mutex/file-stat` to get statistics on active mutexes:\n ```bash\nphp console phphleb/conductor add\n\ncomposer dump-autoload\n ```\n#### Installation in another project\n\nUsing Composer (or copy the files into the **vendor** folder of the project):\n ```bash\n $ composer require phphleb/conductor\n```\n\nOwn configuration (to be installed once):\n\n```php\nuse \\Phphleb\\Conductor\\FileMutex;\n\n$config = new MainConfig(); // implements FileConfigInterface, BaseConfigInterface\n$mutex = new FileMutex($config);\n\n```\n\nFiles of a mutex type are usually applied only for one backend server; otherwise, you can try to synchronize the folder with the tag files of mutexes.\nHowever, if it is possible, it will be better to use mutexes based on storing the tags in the data base.\n\n\n ### DbMutex\n \nThe locks with the stored status in the data bases – similar implementation of mutexes. The same methods – `acquire`, `release` и `unlock` – are used, as well as connecting your own configuration. \nThe difference lies in the class used for initializing the mutex.\n\n\n```php\nuse \\Phphleb\\Conductor\\DbMutex;\n\n$mutex = new DbMutex();\n\n```\n\nBy default, the configuration settings are taken from the `mutex.db.type` (config/database.php).\n\nSupported  __MySQL__ / __MariaDB__ / __PostgreSQL__.\n\n\n ### PredisMutex\n \nRedis is connected in the same way.\n \n  ```bash\ncomposer require phphleb/hredis\n ```\nRequires [predis/predis](https://github.com/predis/predis) library.\n\n ```php\n use \\Phphleb\\Conductor\\PredisMutex;\n \n $mutex = new PredisMutex();\n \n ```\n\nThe configuration will be loaded from the `redis.db.type` setting.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphphleb%2Fconductor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphphleb%2Fconductor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphphleb%2Fconductor/lists"}