1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<?php
final class ITSEC_Mutex { /** @var int */ private $id;
/** @var string */ private $name;
/** @var int */ private $expires;
/** * ITSEC_Mutex constructor. * * @param int $id * @param string $name * @param int $expires */ private function __construct( $id, $name, $expires ) { $this->id = $id; $this->name = $name; $this->expires = $expires; }
/** * Get a mutex. * * @param string $name Name of the mutex. * @param int $ttl How long to hold the mutex for. * * @return ITSEC_Mutex|null Returns null if the mutex is already being held. */ public static function get( $name, $ttl = 30 ) { global $wpdb;
$expires_at = time() + $ttl;
while ( ! $id = self::create( $name, $expires_at ) ) { $existing = $wpdb->get_row( $wpdb->prepare( "SELECT `mutex_id`, `mutex_expires` FROM `{$wpdb->base_prefix}itsec_mutexes` WHERE `mutex_name` = %s", $name ), ARRAY_A );
if ( ! $existing ) { continue; }
if ( $existing['mutex_expires'] < time() ) { self::delete_by_id( $existing['mutex_id'] ); continue; }
return null; }
return new self( $wpdb->insert_id, $name, $expires_at ); }
/** * Release the mutex back. */ public function release() { self::delete_by_id( $this->get_id() ); }
/** * Has the mutex expired. * * @return bool */ public function is_expired() { return $this->get_expires() < time(); }
/** * Checks if this mutex still exists. * * @return bool */ public function exists() { global $wpdb;
return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT `mutex_id` FROM {$wpdb->base_prefix}itsec_mutexes WHERE `mutex_id` = %d", $this->get_id() ) ); }
/** * Get the mutex's id. * * @return int */ public function get_id() { return $this->id; }
/** * Get the name of the mutex. * * @return string */ public function get_name() { return $this->name; }
/** * Get the epoch the mutex expires at. * * @return int */ public function get_expires() { return $this->expires; }
/** * Delete a mutex by it's id. * * @param string $id */ private static function delete_by_id( $id ) { global $wpdb;
$wpdb->delete( $wpdb->base_prefix . 'itsec_mutexes', [ 'mutex_id' => $id, ] ); }
/** * Create a mutex record. * * @param string $name * @param int $expires_at * * @return int The mutex id, or 0 if it could not be acquired. */ private static function create( $name, $expires_at ) { global $wpdb;
$r = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `{$wpdb->base_prefix}itsec_mutexes` (`mutex_name`, `mutex_expires`) VALUES (%s, %s) /* LOCK */", $name, $expires_at ) );
if ( ! $r || ! $wpdb->insert_id ) { return 0; }
return (int) $wpdb->insert_id; } }
|