LockBackendAbstract.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Drupal\Core\Lock;
  3. /**
  4. * Non backend related common methods implementation for lock backends.
  5. *
  6. * @ingroup lock
  7. */
  8. abstract class LockBackendAbstract implements LockBackendInterface {
  9. /**
  10. * Current page lock token identifier.
  11. *
  12. * @var string
  13. */
  14. protected $lockId;
  15. /**
  16. * Existing locks for this page.
  17. *
  18. * @var array
  19. */
  20. protected $locks = [];
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function wait($name, $delay = 30) {
  25. // Pause the process for short periods between calling
  26. // lock_may_be_available(). This prevents hitting the database with constant
  27. // database queries while waiting, which could lead to performance issues.
  28. // However, if the wait period is too long, there is the potential for a
  29. // large number of processes to be blocked waiting for a lock, especially
  30. // if the item being rebuilt is commonly requested. To address both of these
  31. // concerns, begin waiting for 25ms, then add 25ms to the wait period each
  32. // time until it reaches 500ms. After this point polling will continue every
  33. // 500ms until $delay is reached.
  34. // $delay is passed in seconds, but we will be using usleep(), which takes
  35. // microseconds as a parameter. Multiply it by 1 million so that all
  36. // further numbers are equivalent.
  37. $delay = (int) $delay * 1000000;
  38. // Begin sleeping at 25ms.
  39. $sleep = 25000;
  40. while ($delay > 0) {
  41. // This function should only be called by a request that failed to get a
  42. // lock, so we sleep first to give the parallel request a chance to finish
  43. // and release the lock.
  44. usleep($sleep);
  45. // After each sleep, increase the value of $sleep until it reaches
  46. // 500ms, to reduce the potential for a lock stampede.
  47. $delay = $delay - $sleep;
  48. $sleep = min(500000, $sleep + 25000, $delay);
  49. if ($this->lockMayBeAvailable($name)) {
  50. // No longer need to wait.
  51. return FALSE;
  52. }
  53. }
  54. // The caller must still wait longer to get the lock.
  55. return TRUE;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getLockId() {
  61. if (!isset($this->lockId)) {
  62. $this->lockId = uniqid(mt_rand(), TRUE);
  63. }
  64. return $this->lockId;
  65. }
  66. }