@@ -63,10 +63,6 @@ class Throttle
63
63
protected $ max_requests = 0 ; // 规定时间内允许的最大请求次数
64
64
protected $ expire = 0 ; // 规定时间
65
65
protected $ remaining = 0 ; // 规定时间内还能请求的次数
66
- /**
67
- * @var ThrottleAbstract|null
68
- */
69
- protected $ driver_class = null ;
70
66
71
67
/**
72
68
* Throttle constructor.
@@ -98,24 +94,23 @@ protected function allowRequest(Request $request): bool
98
94
[$ max_requests , $ duration ] = $ this ->parseRate ($ this ->config ['visit_rate ' ]);
99
95
100
96
$ micronow = microtime (true );
101
- $ now = (int ) $ micronow ;
102
97
103
- $ this -> driver_class = Container::getInstance ()->invokeClass ($ this ->config ['driver_name ' ]);
104
- if (!$ this -> driver_class instanceof ThrottleAbstract) {
98
+ $ driver = Container::getInstance ()->invokeClass ($ this ->config ['driver_name ' ]);
99
+ if (!( $ driver instanceof ThrottleAbstract) ) {
105
100
throw new \TypeError ('The throttle driver must extends ' . ThrottleAbstract::class);
106
101
}
107
- $ allow = $ this -> driver_class ->allowRequest ($ key , $ micronow , $ max_requests , $ duration , $ this ->cache );
102
+ $ allow = $ driver ->allowRequest ($ key , $ micronow , $ max_requests , $ duration , $ this ->cache );
108
103
109
104
if ($ allow ) {
110
105
// 允许访问
111
- $ this ->now = $ now ;
106
+ $ this ->now = ( int ) $ micronow ;
112
107
$ this ->expire = $ duration ;
113
108
$ this ->max_requests = $ max_requests ;
114
- $ this ->remaining = $ max_requests - $ this -> driver_class ->getCurRequests ();
109
+ $ this ->remaining = $ max_requests - $ driver ->getCurRequests ();
115
110
return true ;
116
111
}
117
112
118
- $ this ->wait_seconds = $ this -> driver_class ->getWaitSeconds ();
113
+ $ this ->wait_seconds = $ driver ->getWaitSeconds ();
119
114
return false ;
120
115
}
121
116
@@ -165,7 +160,7 @@ protected function getCacheKey(Request $request): ?string
165
160
166
161
if ($ key === true ) {
167
162
$ key = $ request ->ip ();
168
- } elseif (false !== strpos ($ key , '__ ' )) {
163
+ } elseif (is_string ( $ key ) && false !== strpos ($ key , '__ ' )) {
169
164
$ key = str_replace (['__CONTROLLER__ ' , '__ACTION__ ' , '__IP__ ' ], [$ request ->controller (), $ request ->action (), $ request ->ip ()], $ key );
170
165
}
171
166
0 commit comments