Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyRateLimitHandler.java
Go to the documentation of this file.
1package dev.rafex.ether.http.jetty12;
2
3/*-
4 * #%L
5 * ether-http-jetty12
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.time.Clock;
30import java.util.concurrent.ConcurrentHashMap;
31
32import org.eclipse.jetty.server.Handler;
33import org.eclipse.jetty.server.Request;
34import org.eclipse.jetty.server.Response;
35import org.eclipse.jetty.util.Callback;
36
37import dev.rafex.ether.http.jetty12.response.JettyApiErrorResponses;
38import dev.rafex.ether.http.security.proxy.TrustedProxyPolicy;
39import dev.rafex.ether.http.security.ratelimit.RateLimitPolicy;
40
41final class JettyRateLimitHandler extends Handler.Wrapper {
42
43 private final RateLimitPolicy policy;
44 private final TrustedProxyPolicy trustedProxyPolicy;
45 private final JettyApiErrorResponses errorResponses;
46 private final Clock clock;
47 private final ConcurrentHashMap<String, WindowCounter> counters = new ConcurrentHashMap<>();
48
49 JettyRateLimitHandler(final Handler next, final RateLimitPolicy policy, final TrustedProxyPolicy trustedProxyPolicy,
50 final JettyApiErrorResponses errorResponses) {
51 this(next, policy, trustedProxyPolicy, errorResponses, Clock.systemUTC());
52 }
53
54 JettyRateLimitHandler(final Handler next, final RateLimitPolicy policy, final TrustedProxyPolicy trustedProxyPolicy,
55 final JettyApiErrorResponses errorResponses, final Clock clock) {
56 super(next);
57 this.policy = policy;
58 this.trustedProxyPolicy = trustedProxyPolicy;
59 this.errorResponses = errorResponses;
60 this.clock = clock;
61 }
62
63 @Override
64 public boolean handle(final Request request, final Response response, final Callback callback) throws Exception {
65 final var decision = register(request);
66 response.getHeaders().put("RateLimit-Limit", Integer.toString(decision.limit()));
67 response.getHeaders().put("RateLimit-Remaining", Integer.toString(decision.remaining()));
68 response.getHeaders().put("RateLimit-Reset", Long.toString(decision.resetAtEpochSecond()));
69
70 if (!decision.allowed()) {
71 errorResponses.error(response, callback, 429, "too_many_requests", "rate_limit_exceeded",
72 "request rate limit exceeded", request.getHttpURI().getPath());
73 return true;
74 }
75
76 return super.handle(request, response, callback);
77 }
78
79 private Decision register(final Request request) {
80 final long nowEpochSecond = clock.instant().getEpochSecond();
81 final long windowSeconds = Math.max(1, policy.windowSeconds());
82 final long windowStart = (nowEpochSecond / windowSeconds) * windowSeconds;
83 final long resetAt = windowStart + windowSeconds;
84 final int limit = Math.max(0, policy.maxRequests()) + Math.max(0, policy.burst());
85 final String key = keyFor(request);
86
87 final var counter = counters.compute(key, (ignored, existing) -> {
88 if (existing == null || existing.windowStartEpochSecond != windowStart) {
89 return new WindowCounter(windowStart, 1);
90 }
91 existing.count++;
92 return existing;
93 });
94
95 final boolean allowed = counter.count <= limit;
96 final int remaining = Math.max(0, limit - counter.count);
97 return new Decision(allowed, limit, remaining, resetAt);
98 }
99
100 private String keyFor(final Request request) {
101 if (policy.scope() == RateLimitPolicy.Scope.GLOBAL) {
102 return "global";
103 }
104 final var clientIp = JettyRequestIpResolver.resolve(request, trustedProxyPolicy);
105 return clientIp == null || clientIp.isBlank() ? "unknown" : clientIp;
106 }
107
108 private record Decision(boolean allowed, int limit, int remaining, long resetAtEpochSecond) {
109 }
110
111 private static final class WindowCounter {
112 private final long windowStartEpochSecond;
113 private int count;
114
115 private WindowCounter(final long windowStartEpochSecond, final int count) {
116 this.windowStartEpochSecond = windowStartEpochSecond;
117 this.count = count;
118 }
119 }
120}
record TrustedProxyPolicy(List< String > trustedSources, boolean trustForwardedHeader, boolean forwardedOnly, boolean preferRightMostForwardedFor)
Política para configurar proxy de confianza en servidores.
record RateLimitPolicy(Scope scope, int maxRequests, int windowSeconds, int burst, int maxConcurrentRequests)
Política de control de tasa (rate limiting) para prevenir abusos.