@@ -32,6 +32,8 @@ public function __construct($identifier, Client $client, $ttl = 10000, LoggerInt
32
32
$ this ->ttl = $ ttl ;
33
33
$ this ->logger = $ logger ?: new NullLogger ;
34
34
$ this ->id = mt_rand ();
35
+ register_shutdown_function ($ closure = $ this ->releaseClosure ());
36
+ pcntl_signal (SIGINT , $ closure );
35
37
}
36
38
37
39
public function acquire ()
@@ -48,18 +50,32 @@ public function acquire()
48
50
49
51
public function release ()
50
52
{
51
- $ script = <<<LUA
52
- if redis.call("get", KEYS[1]) == ARGV[1] then
53
- return redis.call("del", KEYS[1])
54
- end
55
- LUA ;
56
- if ($ this ->client ->eval ($ script , 1 , $ this ->identifier , $ this ->id )) {
57
- $ this ->logger ->debug ("lock released on {identifier} " , ["identifier " => $ this ->identifier ]);
58
- }
53
+ $ closure = $ this ->releaseClosure ();
54
+ $ closure ();
59
55
}
60
56
61
57
public function __destruct ()
62
58
{
63
59
$ this ->release ();
64
60
}
61
+
62
+ private function releaseClosure ()
63
+ {
64
+ $ client = $ this ->client ;
65
+ $ id = $ this ->id ;
66
+ $ identifier = $ this ->identifier ;
67
+ $ logger = $ this ->logger ;
68
+
69
+ $ closure = function () use ($ client , $ identifier , $ id , $ logger ) {
70
+ $ script = <<<LUA
71
+ if redis.call("get", KEYS[1]) == ARGV[1] then
72
+ return redis.call("del", KEYS[1])
73
+ end
74
+ LUA ;
75
+ if ($ client ->eval ($ script , 1 , $ identifier , $ id )) {
76
+ $ logger ->debug ("lock released on {identifier} " , ["identifier " => $ identifier ]);
77
+ }
78
+ };
79
+ return $ closure ->bindTo (null );
80
+ }
65
81
}
0 commit comments