-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyControl.php
More file actions
executable file
·225 lines (182 loc) · 6.54 KB
/
FrequencyControl.php
File metadata and controls
executable file
·225 lines (182 loc) · 6.54 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
class FrequencyControl{
private static $client;//memcache客户端连接
private static $retry_lock_num = 5;//加锁重试次数
private static $retry_lock_time = 1000;//加锁重试间隔时间
private static $time_offset = 0;//时差偏移
private static $rules = array();//规则
const LOCK_SUFFIX = '_$lock';//加锁后缀
const TAIL_SUFFIX = '_$tail';//游标后缀
const TYPE_FIXED_PERIOD = 'fixed';//固定周期
const TYPE_ACTIVE_PERIOD = 'active';//活动周期
private function __construct(){
}
/**
* 初始化
* @param $config
*/
public static function initialize($config){
self::$time_offset = intval(date('Z'));
self::$client = new Memcached();
foreach(array('retry_lock_num', 'retry_lock_time', 'rules', 'separator') as $v){
if(isset($config[$v])){
self::$$v = $config[$v];
}
}
foreach($config['servers'] as $item){
$server = array();
$server[] = $item['hostname'];
$server[] = $item['port'];
$server[] = $item['weight'];
$servers[] = $server;
}
self::$client->addServers($servers);
}
/**
* 加载配置
* @param $acts
* @param $fields
* @return array
*/
private static function get_config($acts, $fields){
$results = array();
if(!is_array($acts)){
$acts = array($acts);
}
foreach($acts as $act){
if(!isset(self::$rules[$act])) continue;
foreach(self::$rules[$act] as $rule){
if(isset($fields[$rule['field']])){
if(in_array($fields[$rule['field']], $rule['white'])){
//continue;
return array();
}
$temp['key'] = sprintf('%s_%s_%s_%s_%s:%s',
__CLASS__,
$act,
$rule['field'],
$rule['type'],
$rule['period'],
$fields[$rule['field']]
);
$temp['type'] = $rule['type'];
$temp['period'] = $rule['period'];
$temp['limit_num'] = $rule['limit_num'];
$results[] = $temp;
}
}
}
return $results;
}
/**
* 检查请求
* @param $acts string|array 行为,可以是字符串,也可以是一个数组
* @param $fields
* @param bool $do_request
* @return integer
*/
public static function check($acts, $fields, $do_request = false){
$items = self::get_config($acts, $fields);
if(empty($items)){
return 0;
}
$time = self::_check($items);
if($time){
return $time;
}
if($do_request){
self::_request($items);
}
return 0;
}
private static function _check($items){
$time_array = array();
foreach($items as $item){
switch($item['type']){
case self::TYPE_ACTIVE_PERIOD:
if(!self::lock($item['key'])){
$time_array[] = 1;
continue;
}
$tail = intval(self::$client->get($item['key'] . self::TAIL_SUFFIX));
$store_time = self::$client->get($item['key'] . '_' . $tail);
self::unlock($item['key']);
if($store_time){
$time_array[] = $store_time + $item['period'] - time();
}
break;
case self::TYPE_FIXED_PERIOD:
$time = time() + self::$time_offset;//加时区修正
$tail = ceil($time / $item['period']);
if(intval(self::$client->get($item['key'] . '_' . $tail)) >= $item['limit_num']){
$time_array[] = $tail * $item['period'] - $time;
}
break;
}
}
if(empty($time_array)) return 0;
$time = max($time_array);
return $time;
}
/**
* 记录请求
*
* @param $acts 行为,可以是字符串,也可以是一个数组
* @param $fields array('field1'=>'value1', 'field2'=>'value2')
* @return bool
*/
public static function request($acts, $fields){
$items = self::get_config($acts, $fields);
if(!empty($items)){
self::_request($items);
}
return true;
}
private static function _request($items){
foreach($items as $item){
switch($item['type']){
case self::TYPE_ACTIVE_PERIOD:
if(!self::lock($item['key'])){
continue;
}
$tail = intval(self::$client->get($item['key'] . self::TAIL_SUFFIX));
self::$client->set($item['key'] . '_' . $tail, time(), $item['period']);
if(++$tail >= $item['limit_num']){
$tail = 0;
}
self::$client->set($item['key'] . self::TAIL_SUFFIX, $tail, $item['period']);
self::unlock($item['key']);
break;
case self::TYPE_FIXED_PERIOD:
$time = time() + self::$time_offset;//加时区修正
$tail = ceil($time / $item['period']);
if(!self::$client->increment($item['key'] . '_' . $tail)){//这里极端情况下可能会整数溢出
self::$client->set($item['key'] . '_' . $tail, 1, $item['period']);
}
break;
}
}
}
/**
* 加锁
* @param $key
* @return bool
*/
private static function lock($key){
$t = 0;
while(!self::$client->add($key . self::LOCK_SUFFIX, 1, 1)){
if($t++ >= self::$retry_lock_num){//尝试等待N次
return false;
}
usleep(self::$retry_lock_time);
}
return true;
}
/**
* 解锁
* @param $key
*/
private static function unlock($key){
self::$client->delete($key . self::LOCK_SUFFIX);
}
}