-
Notifications
You must be signed in to change notification settings - Fork 0
/
throttle.plugin.php
72 lines (64 loc) · 1.82 KB
/
throttle.plugin.php
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
<?php
class Throttle extends Plugin {
const MAX_LOAD = 1.0;
const KILL_LOAD = 3.0;
private $load = null;
private function current_load()
{
static $load;
if(!isset($load)) {
$uptime = `uptime`;
$loads = substr($uptime, strrpos($uptime, ':') + 1);
preg_match('/[0-9]+\.[0-9]+/', $loads, $match);
$load = floatval($match[0]);
}
return $load;
}
public function action_init()
{
//Utils::debug(self::current_load());
}
public function filter_default_rewrite_rules($rules)
{
if($this->current_load() > self::KILL_LOAD) {
foreach($rules as $key => $rule) {
if(strpos($rule['build_str'], 'admin') !== false) {
$rules[$key]['handler'] = 'UserThemeHandler';
$rules[$key]['action'] = 'display_throttle';
}
}
if(Options::get('throttle') == '') {
EventLog::log(sprintf(_t('Kill - Load is %s'), $this->current_load()));
Options::set('throttle', 'kill');
}
}
elseif($this->current_load() > self::MAX_LOAD) {
foreach($rules as $key => $rule) {
if($rule['name'] == 'search') {
unset($rules[$key]);
}
}
$rules[] = array(
'name' => 'search',
'parse_regex' => '%^search(?:/(?P<criteria>[^/]+))?(?:/page/(?P<page>\d+))?/?$%i',
'build_str' => 'search(/{$criteria})(/page/{$page})',
'handler' => 'UserThemeHandler',
'action' => 'display_throttle',
'priority' => 8,
'description' => 'Searches posts'
);
if(Options::get('throttle') == '') {
EventLog::log(sprintf(_t('Restrict - Load is %s'), $this->current_load()));
Options::set('throttle', 'restrict');
}
}
else {
if(Options::get('throttle') != '') {
EventLog::log(sprintf(_t('Normal - Load is %s'), $this->current_load()));
Options::set('throttle', '');
}
}
return $rules;
}
}
?>