1
1
package org .lowcoder .api .framework .filter ;
2
2
3
- import com .google .common .util .concurrent .RateLimiter ;
4
- import jakarta .annotation .PostConstruct ;
5
- import lombok .Getter ;
6
- import lombok .extern .slf4j .Slf4j ;
3
+ import static java .util .Collections .emptyMap ;
4
+ import static org .lowcoder .api .framework .filter .FilterOrder .THROTTLING ;
5
+ import static org .lowcoder .sdk .exception .BizError .REQUEST_THROTTLED ;
6
+ import static org .lowcoder .sdk .util .ExceptionUtils .ofError ;
7
+
8
+ import java .util .Map ;
9
+ import java .util .concurrent .ConcurrentHashMap ;
10
+ import java .util .function .Supplier ;
11
+
12
+ import javax .annotation .Nonnull ;
13
+
7
14
import org .lowcoder .sdk .config .dynamic .ConfigCenter ;
8
15
import org .springframework .beans .factory .annotation .Autowired ;
9
16
import org .springframework .beans .factory .annotation .Value ;
17
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
10
18
import org .springframework .core .Ordered ;
11
19
import org .springframework .http .server .reactive .ServerHttpRequest ;
12
20
import org .springframework .stereotype .Component ;
13
21
import org .springframework .web .server .ServerWebExchange ;
14
22
import org .springframework .web .server .WebFilter ;
15
23
import org .springframework .web .server .WebFilterChain ;
16
- import reactor .core .publisher .Mono ;
17
-
18
- import javax .annotation .Nonnull ;
19
- import java .util .Map ;
20
- import java .util .concurrent .ConcurrentHashMap ;
21
- import java .util .function .Supplier ;
22
24
23
- import static java .util .Collections .emptyMap ;
24
- import static org .lowcoder .api .framework .filter .FilterOrder .THROTTLING ;
25
- import static org .lowcoder .sdk .exception .BizError .REQUEST_THROTTLED ;
26
- import static org .lowcoder .sdk .util .ExceptionUtils .ofError ;
25
+ import com .google .common .util .concurrent .RateLimiter ;
27
26
27
+ import jakarta .annotation .PostConstruct ;
28
+ import lombok .Getter ;
29
+ import lombok .extern .slf4j .Slf4j ;
30
+ import reactor .core .publisher .Mono ;
28
31
32
+ @ ConditionalOnExpression ("${default.api-rate-limit:0} > 0" )
29
33
@ SuppressWarnings ("UnstableApiUsage" )
30
34
@ Slf4j
31
35
@ Component
32
36
public class ThrottlingFilter implements WebFilter , Ordered {
33
37
34
- @ Value ("${default.apiRateLimit:50 }" )
38
+ @ Value ("${default.api-rate-limit }" )
35
39
private int defaultApiRateLimit ;
36
40
37
41
@@ -44,6 +48,7 @@ public class ThrottlingFilter implements WebFilter, Ordered {
44
48
@ PostConstruct
45
49
private void init () {
46
50
urlRateLimiter = configCenter .threshold ().ofMap ("urlRateLimiter" , String .class , Integer .class , emptyMap ());
51
+ log .info ("API rate limit filter enabled with default rate limit set to: {} requests per second" );
47
52
}
48
53
49
54
@ Nonnull
0 commit comments