<commit_before>package no.found.elasticsearch.transport.netty;

import org.elasticsearch.common.netty.buffer.ChannelBuffer;
import org.elasticsearch.common.netty.buffer.ChannelBuffers;
import org.elasticsearch.common.netty.channel.*;
import org.elasticsearch.common.netty.util.Timeout;
import org.elasticsearch.common.netty.util.Timer;
import org.elasticsearch.common.netty.util.TimerTask;
import org.elasticsearch.common.unit.TimeValue;

import java.util.concurrent.TimeUnit;

public class ConnectionKeepAliveHandler extends SimpleChannelHandler implements LifeCycleAwareChannelHandler {
    private final Timer timer;
    private final TimeValue keepAliveInterval;
    ChannelBuffer keepAliveBuffer = ChannelBuffers.copiedBuffer(new byte[]{'F', 'K', 0, 0, 0, 0});

    public ConnectionKeepAliveHandler(Timer timer, TimeValue keepAliveInterval) {
        this.timer = timer;
        this.keepAliveInterval = keepAliveInterval;
    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelDisconnected(ctx, e);
    }

    private void addTimeoutTask(ChannelHandlerContext ctx) {
        timer.newTimeout(new KeepAliveTimerTask(ctx), 2, TimeUnit.SECONDS);
    }

    private long lastWrite;

    @Override
    synchronized public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        super.writeRequested(ctx, e);
        lastWrite = System.currentTimeMillis();
    }

    @Override
    public void beforeAdd(ChannelHandlerContext channelHandlerContext) throws Exception {
    }

    @Override
    synchronized public void afterAdd(ChannelHandlerContext channelHandlerContext) throws Exception {
        lastWrite = System.currentTimeMillis();
        addTimeoutTask(channelHandlerContext);
    }

    @Override
    public void beforeRemove(ChannelHandlerContext channelHandlerContext) throws Exception {
    }

    @Override
    public void afterRemove(ChannelHandlerContext channelHandlerContext) throws Exception {
    }

    class KeepAliveTimerTask implements TimerTask {
        private final ChannelHandlerContext ctx;

        public KeepAliveTimerTask(ChannelHandlerContext ctx) {
            this.ctx = ctx;
        }

        @Override
        public void run(Timeout timeout) throws Exception {
            if (timeout.isCancelled() || !ctx.getChannel().isConnected()) return;

            ctx.getPipeline().execute(new Runnable() {
                @Override
                public void run() {
                    send(ctx, new DownstreamMessageEvent(ctx.getChannel(), Channels.future(ctx.getChannel()), keepAliveBuffer, ctx.getChannel().getRemoteAddress()));
                    addTimeoutTask(ctx);
                }
            });
        }
    }

    synchronized private void send(ChannelHandlerContext ctx, DownstreamMessageEvent downstreamMessageEvent) {
        long now = System.currentTimeMillis();

        if(now - lastWrite > keepAliveInterval.millis()) {
            lastWrite = now;
            ctx.sendDownstream(downstreamMessageEvent);
        }
    }
}
<commit_msg>Removed unneccessary override<commit_after>package no.found.elasticsearch.transport.netty;

import org.elasticsearch.common.netty.buffer.ChannelBuffer;
import org.elasticsearch.common.netty.buffer.ChannelBuffers;
import org.elasticsearch.common.netty.channel.*;
import org.elasticsearch.common.netty.util.Timeout;
import org.elasticsearch.common.netty.util.Timer;
import org.elasticsearch.common.netty.util.TimerTask;
import org.elasticsearch.common.unit.TimeValue;

import java.util.concurrent.TimeUnit;

public class ConnectionKeepAliveHandler extends SimpleChannelHandler implements LifeCycleAwareChannelHandler {
    private final Timer timer;
    private final TimeValue keepAliveInterval;
    ChannelBuffer keepAliveBuffer = ChannelBuffers.copiedBuffer(new byte[]{'F', 'K', 0, 0, 0, 0});

    public ConnectionKeepAliveHandler(Timer timer, TimeValue keepAliveInterval) {
        this.timer = timer;
        this.keepAliveInterval = keepAliveInterval;
    }


    private void addTimeoutTask(ChannelHandlerContext ctx) {
        timer.newTimeout(new KeepAliveTimerTask(ctx), 2, TimeUnit.SECONDS);
    }

    private long lastWrite;

    @Override
    synchronized public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        super.writeRequested(ctx, e);
        lastWrite = System.currentTimeMillis();
    }

    @Override
    public void beforeAdd(ChannelHandlerContext channelHandlerContext) throws Exception {
    }

    @Override
    synchronized public void afterAdd(ChannelHandlerContext channelHandlerContext) throws Exception {
        lastWrite = System.currentTimeMillis();
        addTimeoutTask(channelHandlerContext);
    }

    @Override
    public void beforeRemove(ChannelHandlerContext channelHandlerContext) throws Exception {
    }

    @Override
    public void afterRemove(ChannelHandlerContext channelHandlerContext) throws Exception {
    }

    class KeepAliveTimerTask implements TimerTask {
        private final ChannelHandlerContext ctx;

        public KeepAliveTimerTask(ChannelHandlerContext ctx) {
            this.ctx = ctx;
        }

        @Override
        public void run(Timeout timeout) throws Exception {
            if (timeout.isCancelled() || !ctx.getChannel().isConnected()) return;

            ctx.getPipeline().execute(new Runnable() {
                @Override
                public void run() {
                    send(ctx, new DownstreamMessageEvent(ctx.getChannel(), Channels.future(ctx.getChannel()), keepAliveBuffer, ctx.getChannel().getRemoteAddress()));
                    addTimeoutTask(ctx);
                }
            });
        }
    }

    synchronized private void send(ChannelHandlerContext ctx, DownstreamMessageEvent downstreamMessageEvent) {
        long now = System.currentTimeMillis();

        if(now - lastWrite > keepAliveInterval.millis()) {
            lastWrite = now;
            ctx.sendDownstream(downstreamMessageEvent);
        }
    }
}

{'fraction_non_alphanumeric': 0.08166283273085771, 'fraction_numerical': 0.001643115346697338, 'mean_word_length': 3.357193987115247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4673923', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1534, 'n_words': 335}