Skip to content

PHP SyncReaderWriter Issues #5

Description

@tztztztz

Hello,

I'm using PHP SyncReaderWriter model for cache synchronization that is, the Alghoritm:

  1. Write Lock:
$sync = new \SyncReaderWriter( $lockKey );
$result = $sync->readlock( $this->lockTimeoutMilliseconds );
  1. Check if value exists in the cache and is not expired

  2. If it exists and it's valid, unlock and return value:

if ($result) { $sync->readunlock(); }
return $value;
  1. If it not exists and it's not valid unlock readlock, lock writelock:
$sync->readunlock();

$sync = new \SyncReaderWriter( $lockKey );
$result = $sync->writelock( $this->lockTimeoutMilliseconds );
  1. Check if value has been created in meantime by other process, if it is so, unlock writelock, and return value
if ($result) { $sync->writeunlock(); }
return $value;
  1. If it's still not created, create value, put into cache, unlock writelock, return value:
if ($result) { $sync->writeunlock(); }
return $value;

Problem is, sometimes, it looks like a lock is not released and hangs forever until I reboot linux machine, restarting php-fpm doesn't unlocking it.

There are no fatal errors noticed in PHP error log.

There is one thing maybe I'm doing wrong:

If the lock fails, like after 5 sec of waiting, I'm not releasing the lock (Steps 3, 5 or 6) but just getting value from the cache and returning it, or trying to writelock nad create the value.

I'm releasing the lock only if acquired:

if ($result) { $sync->writeunlock(); }

Should I always release it even if acquiring failed:

$sync->writeunlock(); // Releasing the lock even if acquiring failed? ($result != true)

Should I always try to release the lock even if I failed to acquire it, or it's just something else here?

I'm running pretty busy system/website where there are many visitors and tens of thousands items in the cache, so synchronisation is heavly used, but generates a lot of hanging locks, so finally i had to switch synchronisation off in favor of cache slamming until I find solution to the problem, or other way to synchronise it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions