PHP 8.2.31
Preview: lock-helper.php Size: 2.84 KB
/home/nshryvcy/blissfulnepal.com/wp-content/plugins/wordpress-seo/src/helpers/lock-helper.php

<?php

namespace Yoast\WP\SEO\Helpers;

use Yoast\WP\SEO\Exceptions\Locking\Lock_Timeout_Exception;
use Yoast\WP\SEO\Expiring_Store\Application\Expiring_Store;

/**
 * Distributed exclusive lock backed by the expiring store.
 *
 * Acquires a network-scoped lock in the expiring store table, executes the
 * callback, and releases the lock in a finally block.
 *
 * The lock has a TTL so that it self-expires if the owning process crashes without
 * releasing it. Other processes retry with a configurable delay between attempts.
 */
class Lock_Helper {

	/**
	 * The expiring store.
	 *
	 * @var Expiring_Store
	 */
	private $store;

	/**
	 * The constructor.
	 *
	 * @param Expiring_Store $store The expiring store.
	 */
	public function __construct( Expiring_Store $store ) {
		$this->store = $store;
	}

	/**
	 * Acquires a lock, executes the callback, and releases the lock.
	 *
	 * @template T
	 *
	 * @param string        $lock_key                 The lock key.
	 * @param callable(): T $callback                 The callback to execute while holding the lock.
	 * @param int           $ttl_in_seconds           The lock TTL in seconds. Defaults to 30.
	 * @param int           $max_attempts             The maximum number of acquisition attempts. Defaults to 5.
	 * @param int           $retry_delay_microseconds The delay between attempts in microseconds. Defaults to 20000 (20ms).
	 *
	 * @return T The callback's return value.
	 * @throws Lock_Timeout_Exception When the lock cannot be acquired within the allowed attempts.
	 */
	public function execute( string $lock_key, callable $callback, int $ttl_in_seconds = 30, int $max_attempts = 5, int $retry_delay_microseconds = 20_000 ) {
		$this->acquire( $lock_key, $ttl_in_seconds, $max_attempts, $retry_delay_microseconds );

		try {
			return $callback();
		}
		finally {
			$this->store->delete_for_multisite( $lock_key );
		}
	}

	/**
	 * Attempts to acquire the lock with retries.
	 *
	 * @param string $lock_key                 The lock key.
	 * @param int    $ttl_in_seconds           The lock TTL in seconds.
	 * @param int    $max_attempts             The maximum number of attempts.
	 * @param int    $retry_delay_microseconds The delay between attempts in microseconds.
	 *
	 * @return void
	 * @throws Lock_Timeout_Exception When all attempts are exhausted.
	 */
	private function acquire( string $lock_key, int $ttl_in_seconds, int $max_attempts, int $retry_delay_microseconds ): void {
		for ( $attempt = 1; $attempt <= $max_attempts; $attempt++ ) {
			if ( $this->store->persist_if_absent_for_multisite( $lock_key, true, $ttl_in_seconds ) ) {
				return;
			}

			if ( $attempt < $max_attempts ) {
				\usleep( $retry_delay_microseconds );
			}
		}

		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- This is an exception message, not output.
		throw new Lock_Timeout_Exception( $lock_key, $max_attempts );
	}
}

Directory Contents

Dirs: 3 × Files: 48

Name Size Perms Modified Actions
- drwxr-xr-x 2026-06-03 14:57:42
Edit Download
schema DIR
- drwxr-xr-x 2026-06-03 14:57:42
Edit Download
twitter DIR
- drwxr-xr-x 2026-06-03 14:57:42
Edit Download
1.24 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.51 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.24 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
5.25 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.32 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.07 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
8.10 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
655 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
15.63 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
3.13 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
793 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
5.36 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
681 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
11.69 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.36 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
716 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
9.24 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
6.75 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
12.69 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.65 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.84 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.91 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.01 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
4.65 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
5.68 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.60 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
5.42 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
7.18 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.37 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.08 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.73 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
326 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.74 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
3.40 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
694 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.03 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.78 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
3.52 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
566 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
10.73 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.19 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
4.98 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
8.13 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
3.93 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
2.51 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
1.27 KB lrw-r--r-- 2026-06-03 14:57:42
Edit Download
512 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download
937 B lrw-r--r-- 2026-06-03 14:57:42
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).