yangys
2024-03-31 2969df3e404db3cd116f27db1495e523ce05bf86
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
package com.qianwen.core.redis.ratelimiter;
 
import java.util.concurrent.TimeUnit;
import com.qianwen.core.tool.function.CheckedSupplier;
import com.qianwen.core.tool.utils.Exceptions;
 
/* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/ratelimiter/RateLimiterClient.class */
public interface RateLimiterClient {
    boolean isAllowed(String key, long max, long ttl, TimeUnit timeUnit);
 
    default boolean isAllowed(String key, long max, long ttl) {
        return isAllowed(key, max, ttl, TimeUnit.SECONDS);
    }
 
    default <T> T allow(String key, long max, long ttl, CheckedSupplier<T> supplier) {
        return (T) allow(key, max, ttl, TimeUnit.SECONDS, supplier);
    }
 
    default <T> T allow(String key, long max, long ttl, TimeUnit timeUnit, CheckedSupplier<T> supplier) {
        boolean isAllowed = isAllowed(key, max, ttl, timeUnit);
        if (isAllowed) {
            try {
                return (T) supplier.get();
            } catch (Throwable e) {
                throw Exceptions.unchecked(e);
            }
        }
        throw new RateLimiterException(key, max, ttl, timeUnit);
    }
}