public function acquireLock($sessionId, $lockTimeMS, $acquireTimeoutMS)
    {
        // Get the time in MS
        $currentTime = microtime(true);
        $giveUpTime = $currentTime + ($acquireTimeoutMS * 0.001);

        $lockFilename = $this->generateFilenameForLockFile($sessionId); 

        do {
            //Re-open the lock file every loop, to prevent issues where another
            //process deletes it.
            $fileHandle = fopen($lockFilename, 'c+');
            //'c+' means "Open the file for writing only. If the file does not exist,
            // it is created. If it exists, it is neither truncated (as opposed to 'w'),
            // nor the call to this function fails (as is the case with 'x').
            $locked = flock($fileHandle, LOCK_EX|LOCK_NB);
            if ($locked) {
                //$touchTime = time() + intval(ceil($lockTimeMS / 1000));
                $expireTime = microtime(true) + ($lockTimeMS * 0.0001);
                file_put_contents(
                    $lockFilename,
                    sprintf("%f", $expireTime)
                );
                //touch($lockFilename, $touchTime);
                return $fileHandle;
            }
            
            //We did not acquire a lock - check to see if the lock has expired            
            $contents = file_get_contents($lockFilename);
            $existingExpireTime = floatval($contents);
            // TODO - we could do a basic sanity test on the lock data
            // however, what we be the correct action to take?
//            if ($existingExpireTime < 1447040641 ||
//                $existingExpireTime > 2551578313) {
//                //lock contents are rubbish
//                //TODO - decide what to do.
//            }

            if (microtime(true) > $existingExpireTime) {
                unlink($lockFilename);
            }
            else {
                // sleep 1ms to avoid churning CPU
                usleep(1000);
            }
        } while($giveUpTime > microtime(true));

        throw new FailedToAcquireLockException(
            "FileDriver failed to acquire lock."
        );
    }
public function acquireLock($sessionId, $lockTimeMS, $acquireTimeoutMS)
    {
        // Get the time in MS
        $currentTime = microtime(true);
        $giveUpTime = $currentTime + ($acquireTimeoutMS * 0.001);

        $lockFilename = $this->generateFilenameForLockFile($sessionId); 

        do {
            //Re-open the lock file every loop, to prevent issues where another
            //process deletes it.
            $fileHandle = fopen($lockFilename, 'c+');
            //'c+' means "Open the file for writing only. If the file does not exist,
            // it is created. If it exists, it is neither truncated (as opposed to 'w'),
            // nor the call to this function fails (as is the case with 'x').
            $locked = flock($fileHandle, LOCK_EX|LOCK_NB);
            if ($locked) {
                //$touchTime = time() + intval(ceil($lockTimeMS / 1000));
                $expireTime = microtime(true) + ($lockTimeMS * 0.0001);
                file_put_contents(
                    $lockFilename,
                    sprintf("%f", $expireTime)
                );
                //touch($lockFilename, $touchTime);
                return $fileHandle;
            }
            
            //We did not acquire a lock - check to see if the lock has expired            
            $contents = file_get_contents($lockFilename);
            $existingExpireTime = floatval($contents);
            // TODO - we could do a basic sanity test on the lock data
            // however, what we be the correct action to take?
//            if ($existingExpireTime < 1447040641 ||
//                $existingExpireTime > 2551578313) {
//                //lock contents are rubbish
//                //TODO - decide what to do.
//            }

            if (microtime(true) > $existingExpireTime) {
                unlink($lockFilename);
            }
            else {
                // sleep 1ms to avoid churning CPU
                usleep(1000);
            }
        } while($giveUpTime > microtime(true));

        throw new FailedToAcquireLockException(
            "FileDriver failed to acquire lock."
        );
    }
public function acquireLock ( $ sessionId , $ lockTimeMS , $ acquireTimeoutMS ) { // Get the time in MS $ currentTime = microtime ( true ) ; $ giveUpTime = $ currentTime + ( $ acquireTimeoutMS * 0.001 ) ; $ lockFilename = $ this -> generateFilenameForLockFile ( $ sessionId ) ; do { //Re-open the lock file every loop, to prevent issues where another //process deletes it. $ fileHandle = fopen ( $ lockFilename , 'c+' ) ; //'c+' means "Open the file for writing only. If the file does not exist, // it is created. If it exists, it is neither truncated (as opposed to 'w'), // nor the call to this function fails (as is the case with 'x'). $ locked = flock ( $ fileHandle , LOCK_EX | LOCK_NB ) ; if ( $ locked ) { //$touchTime = time() + intval(ceil($lockTimeMS / 1000)); $ expireTime = microtime ( true ) + ( $ lockTimeMS * 0.0001 ) ; file_put_contents ( $ lockFilename , sprintf ( "%f" , $ expireTime ) ) ; //touch($lockFilename, $touchTime); return $ fileHandle ; } //We did not acquire a lock - check to see if the lock has expired             $ contents = file_get_contents ( $ lockFilename ) ; $ existingExpireTime = floatval ( $ contents ) ; // TODO - we could do a basic sanity test on the lock data // however, what we be the correct action to take? //            if ($existingExpireTime < 1447040641 || //                $existingExpireTime > 2551578313) { //                //lock contents are rubbish //                //TODO - decide what to do. //            } if ( microtime ( true ) > $ existingExpireTime ) { unlink ( $ lockFilename ) ; } else { // sleep 1ms to avoid churning CPU usleep ( 1000 ) ; } } while ( $ giveUpTime > microtime ( true ) ) ; throw new FailedToAcquireLockException ( "FileDriver failed to acquire lock." ) ; }
Acquire a lock for the session
@param $fileHandle
@param $filename
@param $lockTimeMS
@param $acquireTimeoutMS
@return bool
@throws FailedToAcquireLockException
Acquire a lock for the session