Search code examples
phpmultithreadinglockingblocking

How to create php blocking mechanism on concurrent requests?


Say I have an application recieving concurrent requests. User A and B send requests same time, I need to process requests in a queue.

I need something like this:

function processRequests() {
  if(locked()) {
    wait();
  }

  $this->lock();
  ...process...
  $this->unlock();
}

Is there any packages or patterns helping to solve this problem?

PLEASE DON'T OFFER ANY "MESSAGE QUEUE SERVER" SOLUTIONS!


Solution

  • Using PHP's Semaphore functions, you can implement a simple locking system which will try to acquire a lock based on a key which identifies the resource you want to lock. The sem_acquire function will block and wait until the semaphore can be acquired:

    $sm = sem_get(getmyinode() + hexdec(substr(md5("identifier to lock"), 24)));
    
    if (sem_acquire($sm)) {
    
        ...process...
    
        sem_release($sm);
        sem_remove($sm);
    
    } else {
        throw new \Exception('unable to acquire semaphore');
    }