¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | |
| | | package org.springblade.core.redis.ratelimiter; |
| | | |
| | | |
| | | import org.springblade.core.tool.function.CheckedSupplier; |
| | | import org.springblade.core.tool.utils.Exceptions; |
| | | |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * RateLimiter éæµ Client |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public interface RateLimiterClient { |
| | | |
| | | /** |
| | | * æå¡æ¯å¦è¢«éæµ |
| | | * |
| | | * @param key èªå®ä¹çkeyï¼è¯·ä¿è¯å¯ä¸ |
| | | * @param max æ¯æçæå¤§è¯·æ± |
| | | * @param ttl æ¶é´,åä½é»è®¤ä¸ºç§ï¼secondsï¼ |
| | | * @return æ¯å¦å
许 |
| | | */ |
| | | default boolean isAllowed(String key, long max, long ttl) { |
| | | return this.isAllowed(key, max, ttl, TimeUnit.SECONDS); |
| | | } |
| | | |
| | | /** |
| | | * æå¡æ¯å¦è¢«éæµ |
| | | * |
| | | * @param key èªå®ä¹çkeyï¼è¯·ä¿è¯å¯ä¸ |
| | | * @param max æ¯æçæå¤§è¯·æ± |
| | | * @param ttl æ¶é´ |
| | | * @param timeUnit æ¶é´åä½ |
| | | * @return æ¯å¦å
许 |
| | | */ |
| | | boolean isAllowed(String key, long max, long ttl, TimeUnit timeUnit); |
| | | |
| | | /** |
| | | * æå¡éæµï¼è¢«éå¶æ¶æåº RateLimiterException å¼å¸¸ï¼éè¦èªè¡å¤çå¼å¸¸ |
| | | * |
| | | * @param key èªå®ä¹çkeyï¼è¯·ä¿è¯å¯ä¸ |
| | | * @param max æ¯æçæå¤§è¯·æ± |
| | | * @param ttl æ¶é´ |
| | | * @param supplier Supplier 彿°å¼ |
| | | * @return 彿°æ§è¡ç»æ |
| | | */ |
| | | default <T> T allow(String key, long max, long ttl, CheckedSupplier<T> supplier) { |
| | | return allow(key, max, ttl, TimeUnit.SECONDS, supplier); |
| | | } |
| | | |
| | | /** |
| | | * æå¡éæµï¼è¢«éå¶æ¶æåº RateLimiterException å¼å¸¸ï¼éè¦èªè¡å¤çå¼å¸¸ |
| | | * |
| | | * @param key èªå®ä¹çkeyï¼è¯·ä¿è¯å¯ä¸ |
| | | * @param max æ¯æçæå¤§è¯·æ± |
| | | * @param ttl æ¶é´ |
| | | * @param timeUnit æ¶é´åä½ |
| | | * @param supplier Supplier 彿°å¼ |
| | | * @param <T> |
| | | * @return 彿°æ§è¡ç»æ |
| | | */ |
| | | default <T> T allow(String key, long max, long ttl, TimeUnit timeUnit, CheckedSupplier<T> supplier) { |
| | | boolean isAllowed = this.isAllowed(key, max, ttl, timeUnit); |
| | | if (isAllowed) { |
| | | try { |
| | | return supplier.get(); |
| | | } catch (Throwable e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | throw new RateLimiterException(key, max, ttl, timeUnit); |
| | | } |
| | | } |