| 
 | 1 | +/*  | 
 | 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one  | 
 | 3 | + * or more contributor license agreements. Licensed under the "Elastic License  | 
 | 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side  | 
 | 5 | + * Public License v 1"; you may not use this file except in compliance with, at  | 
 | 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public  | 
 | 7 | + * License v3.0 only", or the "Server Side Public License, v 1".  | 
 | 8 | + */  | 
 | 9 | + | 
 | 10 | +package org.elasticsearch.http.netty4;  | 
 | 11 | + | 
 | 12 | +import io.netty.channel.ChannelHandlerContext;  | 
 | 13 | +import io.netty.channel.ChannelInboundHandlerAdapter;  | 
 | 14 | +import io.netty.handler.codec.http.HttpContent;  | 
 | 15 | +import io.netty.handler.codec.http.HttpRequest;  | 
 | 16 | + | 
 | 17 | +import org.elasticsearch.tasks.Task;  | 
 | 18 | + | 
 | 19 | +/**  | 
 | 20 | + * Inbound channel handler that enrich leaking buffers information from HTTP request.  | 
 | 21 | + * It helps to detect which handler is leaking buffers. Especially integration tests that run with  | 
 | 22 | + * paranoid leak detector that samples all buffers for leaking. Supplying informative opaque-id in  | 
 | 23 | + * integ test helps to narrow down problem (for example test name).  | 
 | 24 | + */  | 
 | 25 | +public class Netty4LeakDetectionHandler extends ChannelInboundHandlerAdapter {  | 
 | 26 | + | 
 | 27 | +    private String info;  | 
 | 28 | + | 
 | 29 | +    @Override  | 
 | 30 | +    public void channelRead(ChannelHandlerContext ctx, Object msg) {  | 
 | 31 | +        if (msg instanceof HttpRequest request) {  | 
 | 32 | +            var opaqueId = request.headers().get(Task.X_OPAQUE_ID_HTTP_HEADER);  | 
 | 33 | +            info = "method: " + request.method() + "; uri: " + request.uri() + "; x-opaque-id: " + opaqueId;  | 
 | 34 | +        }  | 
 | 35 | +        if (msg instanceof HttpContent content) {  | 
 | 36 | +            content.touch(info);  | 
 | 37 | +        }  | 
 | 38 | +        ctx.fireChannelRead(msg);  | 
 | 39 | +    }  | 
 | 40 | +}  | 
0 commit comments