@domain.js/main 0.3.14 → 0.3.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,9 +6,19 @@ interface Cnf {
|
|
|
6
6
|
}
|
|
7
7
|
export declare const Deps: readonly ["redis"];
|
|
8
8
|
interface Deps {
|
|
9
|
-
redis: Pick<Redis, "hincrby" | "expire">;
|
|
9
|
+
redis: Pick<Redis, "hincrby" | "expire" | "hget" | "hdel">;
|
|
10
10
|
}
|
|
11
11
|
export declare function Main(cnf: Cnf, deps: Deps): {
|
|
12
|
-
|
|
12
|
+
control: (field: string, ms: number, limit: number) => Promise<number>;
|
|
13
|
+
check: (field: string, ms: number, limit: number) => Promise<number>;
|
|
14
|
+
incr: (field: string, ms: number) => Promise<number>;
|
|
15
|
+
generate: (field: string, ms: number, limit: number) => {
|
|
16
|
+
/** 全流程控制,会自动累加次数 */
|
|
17
|
+
control(): Promise<number>;
|
|
18
|
+
/** 检测是否超限 */
|
|
19
|
+
check(): Promise<number>;
|
|
20
|
+
/** 仅做累加 */
|
|
21
|
+
incr(): Promise<number>;
|
|
22
|
+
};
|
|
13
23
|
};
|
|
14
24
|
export {};
|
|
@@ -6,23 +6,85 @@ function Main(cnf, deps) {
|
|
|
6
6
|
const { redis } = deps;
|
|
7
7
|
const { frequency: { key: KEY }, } = cnf;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
9
|
+
* 计算应该的key
|
|
10
|
+
* @param ms 频率控制长度,单位毫秒
|
|
11
|
+
* @param now 当前时间戳 毫秒值
|
|
12
|
+
*/
|
|
13
|
+
const getKey = (ms, now = Date.now()) => {
|
|
14
|
+
const t = Math.floor(now / ms);
|
|
15
|
+
return `${KEY}-${ms}-${t}`;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 计算过期时间
|
|
19
|
+
* @param ms 频率控制长度,单位毫秒
|
|
20
|
+
* @param now 当前时间戳 毫秒值
|
|
21
|
+
*/
|
|
22
|
+
const expire = (ms, now = Date.now()) => {
|
|
23
|
+
const t = Math.floor(now / ms);
|
|
24
|
+
return Math.floor((ms * (t + 1) - now) / 1000);
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* 频次控制,超过显示过抛出异常
|
|
28
|
+
* @param field 控频字段key
|
|
11
29
|
* @param ms 控频周期长度 毫秒
|
|
12
30
|
* @param limit 控频极限次数
|
|
13
31
|
*/
|
|
14
|
-
const
|
|
32
|
+
const control = async (field, ms, limit) => {
|
|
15
33
|
const now = Date.now();
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const val = await redis.hincrby(key, filed, 1);
|
|
34
|
+
const key = getKey(ms, now);
|
|
35
|
+
const val = await redis.hincrby(key, field, 1);
|
|
19
36
|
if (val === 1)
|
|
20
|
-
await redis.expire(key,
|
|
37
|
+
await redis.expire(key, expire(ms, now));
|
|
21
38
|
if (val > limit)
|
|
22
39
|
throw Error("Too many request");
|
|
40
|
+
return val;
|
|
23
41
|
};
|
|
24
|
-
|
|
25
|
-
|
|
42
|
+
/**
|
|
43
|
+
* 检测频次控制,超过显示过抛出异常
|
|
44
|
+
* @param filed 控频字段key
|
|
45
|
+
* @param ms 控频周期长度 毫秒
|
|
46
|
+
* @param limit 控频极限次数
|
|
47
|
+
*/
|
|
48
|
+
const check = async (field, ms, limit) => {
|
|
49
|
+
const key = getKey(ms);
|
|
50
|
+
const val = await redis.hget(key, field);
|
|
51
|
+
const value = Number(val) | 0;
|
|
52
|
+
if (value > limit)
|
|
53
|
+
throw Error("Too many request");
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* 控频计数+1
|
|
58
|
+
* @param filed 控频字段key
|
|
59
|
+
* @param ms 控频周期长度 毫秒
|
|
60
|
+
*/
|
|
61
|
+
const incr = async (field, ms) => {
|
|
62
|
+
const key = getKey(ms);
|
|
63
|
+
const val = await redis.hincrby(key, field, 1);
|
|
64
|
+
if (val === 1)
|
|
65
|
+
await redis.expire(key, expire(ms));
|
|
66
|
+
return val;
|
|
26
67
|
};
|
|
68
|
+
/**
|
|
69
|
+
* 构造一个控频对象
|
|
70
|
+
* @param filed 控频字段key
|
|
71
|
+
* @param ms 控频周期长度 毫秒
|
|
72
|
+
* @param limit 控频极限次数
|
|
73
|
+
*/
|
|
74
|
+
const generate = (field, ms, limit) => ({
|
|
75
|
+
/** 全流程控制,会自动累加次数 */
|
|
76
|
+
control() {
|
|
77
|
+
return control(field, ms, limit);
|
|
78
|
+
},
|
|
79
|
+
/** 检测是否超限 */
|
|
80
|
+
check() {
|
|
81
|
+
return check(field, ms, limit);
|
|
82
|
+
},
|
|
83
|
+
/** 仅做累加 */
|
|
84
|
+
incr() {
|
|
85
|
+
return incr(field, ms);
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
return { control, check, incr, generate };
|
|
27
89
|
}
|
|
28
90
|
exports.Main = Main;
|