PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Lock/LockBackendAbstract.php

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