Skip to content

Commit ae15be9

Browse files
committed
refactor sse limit rate config
Signed-off-by: neo <[email protected]>
1 parent 6511c50 commit ae15be9

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

core-ng/src/main/java/core/framework/internal/web/HTTPHandlerContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class HTTPHandlerContext {
1313
public final RequestBeanReader requestBeanReader = new RequestBeanReader();
1414
public final ResponseBeanWriter responseBeanWriter = new ResponseBeanWriter();
1515
public final RateControl rateControl = new RateControl();
16-
public boolean limitRate;
16+
public boolean limitRate; // TODO: simplify this design?
1717
@Nullable
1818
public IPv4AccessControl accessControl;
1919
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
package core.framework.internal.web.sse;
22

33
import core.framework.web.Request;
4+
import core.framework.web.rate.LimitRate;
45
import core.framework.web.sse.Channel;
56
import core.framework.web.sse.ChannelListener;
67

7-
import java.lang.annotation.Annotation;
88
import java.lang.reflect.Method;
99

1010
class ChannelSupport<T> {
1111
final ChannelListener<T> listener;
1212
final ServerSentEventContextImpl<T> context;
1313
final ServerSentEventBuilder<T> builder;
14-
final Method targetMethod;
14+
final LimitRate limitRate;
1515

1616
ChannelSupport(ChannelListener<T> listener, Class<T> eventClass, ServerSentEventContextImpl<T> context) {
1717
this.listener = listener;
1818
this.context = context;
1919
builder = new ServerSentEventBuilder<>(eventClass);
20+
limitRate = limitRate(listener);
21+
}
22+
23+
private LimitRate limitRate(ChannelListener<T> listener) {
2024
try {
21-
this.targetMethod = listener.getClass().getMethod("onConnect", Request.class, Channel.class, String.class);
25+
Method targetMethod = listener.getClass().getMethod("onConnect", Request.class, Channel.class, String.class);
26+
LimitRate limitRate = targetMethod.getDeclaredAnnotation(LimitRate.class);
27+
if (limitRate == null)
28+
limitRate = listener.getClass().getDeclaredAnnotation(LimitRate.class);
29+
return limitRate;
2230
} catch (NoSuchMethodException e) {
2331
throw new Error("failed to get listener.onConnect method", e);
2432
}
2533
}
26-
27-
<V extends Annotation> V annotation(Class<V> annotationClass) {
28-
V annotation = targetMethod.getDeclaredAnnotation(annotationClass);
29-
if (annotation == null)
30-
annotation = listener.getClass().getDeclaredAnnotation(annotationClass);
31-
return annotation;
32-
}
3334
}

core-ng/src/main/java/core/framework/internal/web/sse/ServerSentEventHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import core.framework.internal.web.session.ReadOnlySession;
1212
import core.framework.internal.web.session.SessionManager;
1313
import core.framework.util.Strings;
14-
import core.framework.web.rate.LimitRate;
1514
import core.framework.web.sse.ChannelListener;
1615
import io.undertow.server.HttpHandler;
1716
import io.undertow.server.HttpServerExchange;
@@ -137,9 +136,8 @@ void handle(HttpServerExchange exchange, StreamSinkChannel sink) {
137136
}
138137

139138
void limitRate(RateControl rateControl, ChannelSupport<Object> support, String clientIP) {
140-
LimitRate limitRate = support.annotation(LimitRate.class);
141-
if (limitRate != null) {
142-
String group = limitRate.value();
139+
if (support.limitRate != null) {
140+
String group = support.limitRate.value();
143141
rateControl.validateRate(group, clientIP);
144142
}
145143
}

0 commit comments

Comments
 (0)