-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full Sync: Don't allow more than one request to enqueue (#14039)
- Loading branch information
Showing
4 changed files
with
202 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* Lock class. | ||
* | ||
* @package automattic/jetpack-sync | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Sync; | ||
|
||
/** | ||
* Lock class | ||
*/ | ||
class Lock { | ||
/** | ||
* Prefix of the blog lock transient. | ||
* | ||
* @access public | ||
* | ||
* @var string | ||
*/ | ||
const LOCK_PREFIX = 'jp_sync_lock_'; | ||
|
||
/** | ||
* Default Lifetime of the lock. | ||
* | ||
* @access public | ||
* | ||
* @var int | ||
*/ | ||
const LOCK_TRANSIENT_EXPIRY = 15; // Seconds. | ||
|
||
/** | ||
* Attempt to lock. | ||
* | ||
* @access public | ||
* | ||
* @param string $name lock name. | ||
* @param int $expiry lock duration in seconds. | ||
* | ||
* @return boolean True if succeeded, false otherwise. | ||
*/ | ||
public function attempt( $name, $expiry = self::LOCK_TRANSIENT_EXPIRY ) { | ||
$name = self::LOCK_PREFIX . $name; | ||
$locked_time = get_option( $name ); | ||
if ( $locked_time ) { | ||
if ( microtime( true ) < $locked_time ) { | ||
return false; | ||
} | ||
} | ||
update_option( $name, microtime( true ) + $expiry ); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Remove the lock. | ||
* | ||
* @access public | ||
* | ||
* @param string $name lock name. | ||
*/ | ||
public function remove( $name ) { | ||
delete_option( self::LOCK_PREFIX . $name ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.