From 30a3e7fcdeda35b56ed25c70db711cb7d8e575dd Mon Sep 17 00:00:00 2001
From: Steve Worley <steven.worley@acquia.com>
Date: Thu, 10 Nov 2016 09:25:07 +1000
Subject: [PATCH] Updates locking mechanism

---
 rules.module | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/rules.module b/rules.module
index 038288e..7fba073 100644
--- a/rules.module
+++ b/rules.module
@@ -363,11 +363,17 @@ function &rules_get_cache($cid = 'data') {
       $cache[$cid] = $get->data;
     }
     else {
+
+      // Allow sites to tune stampede protection.
+      $stampede_protection = variable_get('rules_lock_protection', TRUE);
+      $stamped_timeout = variable_get('rules_lock_timeout', 30);
+
       // Prevent stampeding by ensuring the cache is rebuilt just once at the
       // same time.
-      while (!lock_acquire(__FUNCTION__ . $cid . $cid_suffix, 60)) {
+      $locked = !lock_acquire(__FUNCTION__ . $cid . $cid_suffix, intval($stamped_timeout));
+      if ((bool) $stampede_protection && $locked) {
         // Now wait until the lock is released.
-        lock_wait(__FUNCTION__ . $cid . $cid_suffix, 30);
+        lock_wait(__FUNCTION__ . $cid . $cid_suffix, intval($stamped_timeout));
         // If the lock is released it's likely the cache was rebuild. Thus check
         // again if we can fetch it from the persistent cache.
         if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
@@ -375,6 +381,7 @@ function &rules_get_cache($cid = 'data') {
           return $cache[$cid];
         }
       }
+
       if ($cid === 'data') {
         // There is no 'data' cache so we need to rebuild it. Make sure subsequent
         // cache gets of the main 'data' cache during rebuild get the interim
-- 
2.7.4 (Apple Git-66)

