24
24
import org .slf4j .LoggerFactory ;
25
25
26
26
import com .netflix .spectator .api .Counter ;
27
+ import com .netflix .spectator .api .Timer ;
27
28
28
29
import io .netty .channel .ChannelHandlerContext ;
29
30
import io .netty .channel .ChannelInboundHandlerAdapter ;
@@ -37,24 +38,29 @@ public class HttpHeadersTimeoutHandler {
37
38
38
39
private static final AttributeKey <ScheduledFuture <Void >> HTTP_HEADERS_READ_TIMEOUT_FUTURE =
39
40
AttributeKey .newInstance ("httpHeadersReadTimeoutFuture" );
41
+ private static final AttributeKey <Long > HTTP_HEADERS_READ_START_TIME =
42
+ AttributeKey .newInstance ("httpHeadersReadStartTime" );
40
43
41
44
public static class InboundHandler extends ChannelInboundHandlerAdapter {
42
45
private final BooleanSupplier httpHeadersReadTimeoutEnabledSupplier ;
43
46
private final IntSupplier httpHeadersReadTimeoutSupplier ;
44
47
45
48
private final Counter httpHeadersReadTimeoutCounter ;
49
+ private final Timer httpHeadersReadTimer ;
46
50
47
51
private boolean closed = false ;
48
52
49
- public InboundHandler (BooleanSupplier httpHeadersReadTimeoutEnabledSupplier , IntSupplier httpHeadersReadTimeoutSupplier , Counter httpHeadersReadTimeoutCounter ) {
53
+ public InboundHandler (BooleanSupplier httpHeadersReadTimeoutEnabledSupplier , IntSupplier httpHeadersReadTimeoutSupplier , Counter httpHeadersReadTimeoutCounter , Timer httpHeadersReadTimer ) {
50
54
this .httpHeadersReadTimeoutEnabledSupplier = httpHeadersReadTimeoutEnabledSupplier ;
51
55
this .httpHeadersReadTimeoutSupplier = httpHeadersReadTimeoutSupplier ;
52
56
this .httpHeadersReadTimeoutCounter = httpHeadersReadTimeoutCounter ;
57
+ this .httpHeadersReadTimer = httpHeadersReadTimer ;
53
58
}
54
59
55
60
@ Override
56
61
public void channelActive (ChannelHandlerContext ctx ) throws Exception {
57
62
try {
63
+ ctx .channel ().attr (HTTP_HEADERS_READ_START_TIME ).set (System .nanoTime ());
58
64
if (!httpHeadersReadTimeoutEnabledSupplier .getAsBoolean ())
59
65
return ;
60
66
int timeout = httpHeadersReadTimeoutSupplier .getAsInt ();
@@ -84,12 +90,13 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
84
90
public void channelRead (ChannelHandlerContext ctx , Object msg ) throws Exception {
85
91
try {
86
92
if (msg instanceof HttpMessage ) {
93
+ httpHeadersReadTimer .record (System .nanoTime () - ctx .channel ().attr (HTTP_HEADERS_READ_START_TIME ).get (), TimeUnit .NANOSECONDS );
87
94
ScheduledFuture <Void > future = ctx .channel ().attr (HTTP_HEADERS_READ_TIMEOUT_FUTURE ).get ();
88
95
if (future != null ) {
89
96
future .cancel (false );
90
- ctx .pipeline ().remove (this );
91
97
LOG .debug ("[{}] Removing HTTP headers read timeout handler" , ctx .channel ().id ());
92
98
}
99
+ ctx .pipeline ().remove (this );
93
100
}
94
101
} finally {
95
102
super .channelRead (ctx , msg );
0 commit comments