1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http2;
17
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.util.concurrent.Ticker;
20 import io.netty.util.internal.logging.InternalLogger;
21 import io.netty.util.internal.logging.InternalLoggerFactory;
22
23 import java.util.concurrent.TimeUnit;
24
25
26 final class Http2MaxRstFrameListener extends Http2FrameListenerDecorator {
27 private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2MaxRstFrameListener.class);
28 private static final Http2Exception RST_FRAME_RATE_EXCEEDED = Http2Exception.newStatic(Http2Error.ENHANCE_YOUR_CALM,
29 "Maximum number of RST frames reached",
30 Http2Exception.ShutdownHint.HARD_SHUTDOWN, Http2MaxRstFrameListener.class, "onRstStreamRead(..)");
31
32 private final long nanosPerWindow;
33 private final int maxRstFramesPerWindow;
34 private final Ticker ticker;
35 private long lastRstFrameNano;
36 private int receivedRstInWindow;
37
38 Http2MaxRstFrameListener(Http2FrameListener listener, int maxRstFramesPerWindow, int secondsPerWindow,
39 Ticker ticker) {
40 super(listener);
41 this.maxRstFramesPerWindow = maxRstFramesPerWindow;
42 this.nanosPerWindow = TimeUnit.SECONDS.toNanos(secondsPerWindow);
43 this.ticker = ticker;
44 this.lastRstFrameNano = ticker.nanoTime() - nanosPerWindow;
45 }
46
47 @Override
48 public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode) throws Http2Exception {
49 long currentNano = ticker.nanoTime();
50 if (currentNano - lastRstFrameNano >= nanosPerWindow) {
51 lastRstFrameNano = currentNano;
52 receivedRstInWindow = 1;
53 } else {
54 receivedRstInWindow++;
55 if (receivedRstInWindow > maxRstFramesPerWindow) {
56 logger.debug("{} Maximum number {} of RST frames reached within {} seconds, " +
57 "closing connection with {} error", ctx.channel(), maxRstFramesPerWindow,
58 TimeUnit.NANOSECONDS.toSeconds(nanosPerWindow), RST_FRAME_RATE_EXCEEDED.error(),
59 RST_FRAME_RATE_EXCEEDED);
60 throw RST_FRAME_RATE_EXCEEDED;
61 }
62 }
63 super.onRstStreamRead(ctx, streamId, errorCode);
64 }
65 }