Created
March 19, 2024 06:14
-
-
Save alexjosesilva/d25ebcc4ad6b17c2ffa702e11c84dbec to your computer and use it in GitHub Desktop.
O cliente envia uma solicitação ao servidor. O servidor verifica a taxa de solicitações do cliente. Se estiver dentro do limite, o servidor processa a solicitação normalmente
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.util.concurrent.RateLimiter; | |
public class RateLimitingExample { | |
private static final RateLimiter rateLimiter = RateLimiter.create(10); // 10 solicitações por segundo | |
public static void main(String[] args) { | |
for (int i = 0; i < 20; i++) { | |
if (rateLimiter.tryAcquire()) { | |
System.out.println("Solicitação " + (i + 1) + ": Processada"); | |
} else { | |
System.out.println("Solicitação " + (i + 1) + ": Limite de taxa excedido"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment