Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit a36b84f

Browse files
Randomize sleep time
1 parent 5d106b5 commit a36b84f

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"php": ">=5.5.9",
2222
"illuminate/contracts": "5.1.*|5.2.*",
2323
"illuminate/support": "5.1.*|5.2.*",
24-
"predis/predis": "^1.0"
24+
"predis/predis": "^1.0",
25+
"paragonie/random_compat": "^1.1"
2526
},
2627
"require-dev": {
2728
"graham-campbell/testbench": "^3.1",
@@ -42,7 +43,7 @@
4243
},
4344
"extra": {
4445
"branch-alias": {
45-
"dev-master": "1.0-dev"
46+
"dev-master": "1.1-dev"
4647
}
4748
},
4849
"minimum-stability": "dev",

src/Lock.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,18 @@ final class Lock
5656
protected $play;
5757

5858
/**
59-
* The reattempt interval in microseconds.
59+
* The minimum reattempt interval in microseconds.
6060
*
6161
* @var int
6262
*/
63-
protected $interval;
63+
protected $min;
64+
65+
/**
66+
* The maximum reattempt interval in microseconds.
67+
*
68+
* @var int
69+
*/
70+
protected $max;
6471

6572
/**
6673
* The maximum number of trys to acquire the lock.
@@ -101,7 +108,8 @@ public function __construct(ClientInterface $redis, $name, $timeout, $play, $int
101108
$this->name = $name;
102109
$this->timeout = $timeout;
103110
$this->play = $play;
104-
$this->interval = $interval * 1000;
111+
$this->min = $interval * 500;
112+
$this->max = $interval * 1500;
105113
$this->trys = $trys;
106114
}
107115

@@ -142,7 +150,7 @@ public function acquire()
142150
return false;
143151
}
144152

145-
usleep($this->interval);
153+
usleep(random_int($this->min, $this->max));
146154
}
147155
}
148156

0 commit comments

Comments
 (0)