@@ -47,44 +47,40 @@ public function onOpen(Server $server, Request $request)
47
47
$ action = $ req ['action ' ] ?? '' ;
48
48
switch ($ action ) {
49
49
case 'api_excel ' : // api_excel 列表完成率
50
- $ user_id = $ userInfo ['id ' ];
51
- $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
52
- // go(function() use ($server, $request, $user_id) {
53
- while (true ) {
54
- // 创建协程 - 睡眠操作影响 worker 进程
55
- // Coroutine::sleep(5);
56
- sleep (5 );
57
- $ state = ApiExcel::where ('state ' , 1 )->first ();
58
- if (!$ state ) {
59
- $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
60
- return '' ;
50
+ while (true ) {
51
+ $ user_id = $ userInfo ['id ' ];
52
+ $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
53
+ sleep (5 );
54
+ $ state = ApiExcel::where ('state ' , 1 )->first ();
55
+ if (!$ state ) {
56
+ $ server ->push ($ request ->fd , $ this ->apiExcel ($ user_id ));
57
+ break ;
58
+ }
59
+ // 每个用户 fd 限制请求次数
60
+ $ redisKey = 'websocket_fd_ ' .$ request ->fd ;
61
+ if (empty ($ this ->redis )) {
62
+ $ this ->redis = Redis::connection ();
63
+ }
64
+ // 如果获取不到 redis 实例,使用总计数次数
65
+ if ($ this ->redis ) {
66
+ $ count = $ this ->redis ->incr ($ redisKey );
67
+ if ($ count == 1 ) {
68
+ // 设置过期时间
69
+ $ this ->redis ->expire ($ redisKey , 6000 );
61
70
}
62
- // 每个用户 fd 限制请求次数
63
- $ redisKey = 'websocket_fd_ ' .$ request ->fd ;
64
- if (empty ($ this ->redis )) {
65
- $ this ->redis = Redis::connection ();
71
+ if ($ count > 20000 ) { // 防止刷单的安全拦截
72
+ break ; // 超出就跳出循环
66
73
}
67
- // 如果获取不到 redis 实例,使用总计数次数
68
- if ($ this ->redis ) {
69
- $ count = $ this ->redis ->incr ($ redisKey );
70
- if ($ count == 1 ) {
71
- // 设置过期时间
72
- $ this ->redis ->expire ($ redisKey , 600 );
73
- }
74
- if ($ count > 20000 ) { // 防止刷单的安全拦截
75
- return '' ; // 超出就跳出循环
76
- }
77
- } else {
78
- $ count_fd = 'count_ ' .$ request ->fd ;
79
- $ this ->incrKey ($ count_fd );
80
- // 单fd超过 1000 次跳出循环
81
- if ($ this ->$ count_fd > 1000 ) {
82
- unset($ this ->$ count_fd );
83
- return '' ;
84
- }
74
+ } else {
75
+ $ count_fd = 'count_ ' .$ request ->fd ;
76
+ $ this ->incrKey ($ count_fd );
77
+ // 单fd超过 1000 次跳出循环
78
+ if ($ this ->$ count_fd > 1000 ) {
79
+ unset($ this ->$ count_fd );
80
+ break ;
85
81
}
86
82
}
87
- // });
83
+ }
88
84
}
89
85
return '' ;
90
86
0 commit comments