Skip to content

Commit 5acf349

Browse files
committed
fire reject event before closing the conn.
1 parent fe9a4e8 commit 5acf349

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

zuul-core/src/main/java/com/netflix/netty/common/throttle/RejectionUtils.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@
3636
import io.netty.handler.codec.http.HttpVersion;
3737
import io.netty.handler.codec.http.LastHttpContent;
3838
import io.netty.util.ReferenceCountUtil;
39-
40-
import javax.annotation.Nullable;
4139
import java.nio.charset.StandardCharsets;
4240
import java.util.Collections;
4341
import java.util.Map;
4442
import java.util.concurrent.TimeUnit;
43+
import javax.annotation.Nullable;
4544

4645
/**
4746
* A collection of rejection related utilities useful for failing requests. These are tightly coupled with the channel
@@ -56,9 +55,9 @@ public final class RejectionUtils {
5655
/**
5756
* Closes the connection without sending a response, and fires a {@link RequestRejectedEvent} back up the pipeline.
5857
*
59-
* @param nfStatus the status to use for metric reporting
60-
* @param reason the reason for rejecting the request. This is not sent back to the client.
61-
* @param request the request that is being rejected.
58+
* @param nfStatus the status to use for metric reporting
59+
* @param reason the reason for rejecting the request. This is not sent back to the client.
60+
* @param request the request that is being rejected.
6261
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
6362
* sent up the pipeline.
6463
*/
@@ -68,6 +67,10 @@ public static void rejectByClosingConnection(
6867
String reason,
6968
HttpRequest request,
7069
@Nullable Integer injectedLatencyMillis) {
70+
71+
// Notify other handlers before closing the conn.
72+
notifyHandlers(ctx, nfStatus, REJECT_CLOSING_STATUS, reason, request);
73+
7174
if (injectedLatencyMillis != null && injectedLatencyMillis > 0) {
7275
// Delay closing the connection for configured time.
7376
ctx.executor()
@@ -83,23 +86,20 @@ public static void rejectByClosingConnection(
8386
CurrentPassport.fromChannel(ctx.channel()).add(PassportState.SERVER_CH_REJECTING);
8487
ctx.close();
8588
}
86-
87-
// Notify other handlers that we've rejected this request.
88-
notifyHandlers(ctx, nfStatus, REJECT_CLOSING_STATUS, reason, request);
8989
}
9090

9191
/**
9292
* Sends a rejection response back to the client, and fires a {@link RequestRejectedEvent} back up the pipeline.
9393
*
94-
* @param ctx the channel handler processing the request
95-
* @param nfStatus the status to use for metric reporting
96-
* @param reason the reason for rejecting the request. This is not sent back to the client.
97-
* @param request the request that is being rejected.
94+
* @param ctx the channel handler processing the request
95+
* @param nfStatus the status to use for metric reporting
96+
* @param reason the reason for rejecting the request. This is not sent back to the client.
97+
* @param request the request that is being rejected.
9898
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
9999
* sent up the pipeline.
100-
* @param rejectedCode the HTTP code to send back to the client.
101-
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
102-
* @param rejectionHeaders additional HTTP headers to add to the rejection response
100+
* @param rejectedCode the HTTP code to send back to the client.
101+
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
102+
* @param rejectionHeaders additional HTTP headers to add to the rejection response
103103
*/
104104
public static void sendRejectionResponse(
105105
ChannelHandlerContext ctx,
@@ -152,16 +152,16 @@ public static void allowThenClose(ChannelHandlerContext ctx) {
152152
* Throttle either by sending rejection response message, or by closing the connection now, or just drop the
153153
* message. Only call this if ThrottleResult.shouldThrottle() returned {@code true}.
154154
*
155-
* @param ctx the channel handler processing the request
156-
* @param msg the request that is being rejected.
157-
* @param rejectionType the type of rejection
158-
* @param nfStatus the status to use for metric reporting
159-
* @param reason the reason for rejecting the request. This is not sent back to the client.
155+
* @param ctx the channel handler processing the request
156+
* @param msg the request that is being rejected.
157+
* @param rejectionType the type of rejection
158+
* @param nfStatus the status to use for metric reporting
159+
* @param reason the reason for rejecting the request. This is not sent back to the client.
160160
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
161161
* sent up the pipeline.
162-
* @param rejectedCode the HTTP code to send back to the client.
163-
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
164-
* @param rejectionHeaders additional HTTP headers to add to the rejection response
162+
* @param rejectedCode the HTTP code to send back to the client.
163+
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
164+
* @param rejectionHeaders additional HTTP headers to add to the rejection response
165165
*/
166166
public static void handleRejection(
167167
ChannelHandlerContext ctx,
@@ -214,15 +214,15 @@ public static void handleRejection(
214214
/**
215215
* Switches on the rejection type to decide how to reject the request and or close the conn.
216216
*
217-
* @param ctx the channel handler processing the request
218-
* @param rejectionType the type of rejection
219-
* @param nfStatus the status to use for metric reporting
220-
* @param reason the reason for rejecting the request. This is not sent back to the client.
221-
* @param request the request that is being rejected.
217+
* @param ctx the channel handler processing the request
218+
* @param rejectionType the type of rejection
219+
* @param nfStatus the status to use for metric reporting
220+
* @param reason the reason for rejecting the request. This is not sent back to the client.
221+
* @param request the request that is being rejected.
222222
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
223223
* sent up the pipeline.
224-
* @param rejectedCode the HTTP code to send back to the client.
225-
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
224+
* @param rejectedCode the HTTP code to send back to the client.
225+
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
226226
*/
227227
public static void reject(
228228
ChannelHandlerContext ctx,
@@ -248,16 +248,16 @@ public static void reject(
248248
/**
249249
* Switches on the rejection type to decide how to reject the request and or close the conn.
250250
*
251-
* @param ctx the channel handler processing the request
252-
* @param rejectionType the type of rejection
253-
* @param nfStatus the status to use for metric reporting
254-
* @param reason the reason for rejecting the request. This is not sent back to the client.
255-
* @param request the request that is being rejected.
251+
* @param ctx the channel handler processing the request
252+
* @param rejectionType the type of rejection
253+
* @param nfStatus the status to use for metric reporting
254+
* @param reason the reason for rejecting the request. This is not sent back to the client.
255+
* @param request the request that is being rejected.
256256
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
257257
* sent up the pipeline.
258-
* @param rejectedCode the HTTP code to send back to the client.
259-
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
260-
* @param rejectionHeaders additional HTTP headers to add to the rejection response
258+
* @param rejectedCode the HTTP code to send back to the client.
259+
* @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
260+
* @param rejectionHeaders additional HTTP headers to add to the rejection response
261261
*/
262262
public static void reject(
263263
ChannelHandlerContext ctx,
@@ -306,7 +306,7 @@ private static boolean closeConnectionAfterReject(Channel channel) {
306306
if (channel.hasAttr(HAProxyMessageChannelHandler.ATTR_HAPROXY_VERSION)) {
307307
return HAProxyProtocolVersion.V2
308308
== channel.attr(HAProxyMessageChannelHandler.ATTR_HAPROXY_VERSION)
309-
.get();
309+
.get();
310310
} else {
311311
return false;
312312
}
@@ -330,5 +330,6 @@ private static FullHttpResponse createRejectionResponse(
330330
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, body, headers, EmptyHttpHeaders.INSTANCE);
331331
}
332332

333-
private RejectionUtils() {}
333+
private RejectionUtils() {
334+
}
334335
}

0 commit comments

Comments
 (0)