<commit_before>package com.barchart.netty.guice.http;

import io.netty.channel.ChannelHandler;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.net.SocketAddress;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.barchart.netty.common.util.AddressUtil;
import com.barchart.netty.server.HandlerFactory;
import com.barchart.netty.server.Servers;
import com.barchart.netty.server.http.HttpServer;
import com.barchart.netty.server.http.error.DefaultErrorHandler;
import com.barchart.netty.server.http.error.ErrorHandler;
import com.barchart.netty.server.http.request.RequestHandler;
import com.barchart.netty.server.util.SingleHandlerFactory;
import com.barchart.util.guice.Activate;
import com.barchart.util.guice.Component;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.typesafe.config.Config;

@Component("com.barchart.netty.guice.http")
public class HttpServiceProvider implements HttpService {

	private final Logger log = LoggerFactory.getLogger(getClass());

	private final HttpServer server;

	// limit number of boss to 1 per cpu, number of workers 2 per cpu
	private final EventLoopGroup bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
	private final EventLoopGroup workerGroup = new NioEventLoopGroup();

	@Inject
	@Named("#")
	private Config config;

	protected HttpServiceProvider() {
		server = Servers.createHttpServer()
				.group(bossGroup)
				.childGroup(workerGroup)
				.errorHandler(errorHandler());
	}

	@Inject(optional = true)
	protected void httpServices(final Set<HttpRequestHandler> services) {
		for (final HttpRequestHandler hs : services) {
			registerHandler(hs.path(), new SingleHandlerFactory<RequestHandler>(hs));
		}
	}

	@Inject(optional = true)
	protected void webSocketServices(final Set<WebSocketRequestHandler> services) {
		for (final WebSocketRequestHandler ws : services) {
			registerWebSocket(ws.path(), new SingleHandlerFactory<ChannelHandler>(ws));
		}
	}


	@Activate
	public void activate() throws Exception {

		if (config.hasPath("max-request-size")) {
			server.maxRequestSize(config.getInt("max-request-size"));
		}

		if (config.hasPath("max-connections")) {
			server.maxConnections(config.getInt("max-connections"));
		}

		String requestLogger = null;
		if (config.hasPath("request-logger")) {
			requestLogger = config.getString("request-logger");
		}

		String errorLogger = null;
		if (config.hasPath("error-logger")) {
			errorLogger = config.getString("error-logger");
		}

		server.logger(new SLF4JRequestLogger(requestLogger, errorLogger));

		if (config.hasPath("local-address")) {

			final SocketAddress address = AddressUtil.parseAddress(config.getString("local-address"));

			log.debug("Starting HTTP server on " + address);
			server.listen(address).sync();

		} else {

			log.debug("Starting HTTP server on *:8080");
			server.listen(8080).sync();

		}

	}

	public void deactivate() {

		if (server.running()) {

			log.debug("Stopping HTTP server");

			try {
				server.kill().sync();
				bossGroup.shutdownGracefully().sync();
				workerGroup.shutdownGracefully().sync();
			} catch (final Throwable t) {
				log.error("Could not shutdown HTTP server", t);
			}

		} else {
			log.debug("HTTP server not running!");
		}

	}

	@Override
	public void registerHandler(final String path, final HandlerFactory<RequestHandler> factory) {
		server.requestHandler(path, factory);
		log.debug("Registered handler for " + path + ": " + factory);
	}

	@Override
	public void registerWebSocket(final String path, final HandlerFactory<? extends ChannelHandler> factory) {
		server.webSocketHandler(path, factory);
		log.debug("Registered websocket service for " + path + ": " + factory);
	}

	@Override
	public void unregisterHandler(final String path) {
		server.removeRequestHandler(path);
		server.removeWebSocketHandler(path);
		log.debug("Unregistered handler for " + path);<commit_msg>Fixed shutdown sequence<commit_after>package com.barchart.netty.guice.http;

import io.netty.channel.ChannelHandler;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.net.SocketAddress;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.barchart.netty.common.util.AddressUtil;
import com.barchart.netty.server.HandlerFactory;
import com.barchart.netty.server.Servers;
import com.barchart.netty.server.http.HttpServer;
import com.barchart.netty.server.http.error.DefaultErrorHandler;
import com.barchart.netty.server.http.error.ErrorHandler;
import com.barchart.netty.server.http.request.RequestHandler;
import com.barchart.netty.server.util.SingleHandlerFactory;
import com.barchart.util.guice.Activate;
import com.barchart.util.guice.Component;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.typesafe.config.Config;

@Component(HttpServiceProvider.TYPE)
public class HttpServiceProvider implements HttpService {

	public static final String TYPE = "com.barchart.netty.guice.http";

	private final Logger log = LoggerFactory.getLogger(getClass());

	private final HttpServer server;

	// limit number of boss to 1 per cpu, number of workers 2 per cpu
	private final EventLoopGroup bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
	private final EventLoopGroup workerGroup = new NioEventLoopGroup();

	@Inject
	@Named("#")
	private Config config;

	protected HttpServiceProvider() {
		server = Servers.createHttpServer()
				.group(bossGroup)
				.childGroup(workerGroup)
				.errorHandler(errorHandler());
	}

	@Inject(optional = true)
	protected void httpServices(final Set<HttpRequestHandler> services) {
		for (final HttpRequestHandler hs : services) {
			registerHandler(hs.path(), new SingleHandlerFactory<RequestHandler>(hs));
		}
	}

	@Inject(optional = true)
	protected void webSocketServices(final Set<WebSocketRequestHandler> services) {
		for (final WebSocketRequestHandler ws : services) {
			registerWebSocket(ws.path(), new SingleHandlerFactory<ChannelHandler>(ws));
		}
	}


	@Activate
	public void activate() throws Exception {

		if (config.hasPath("max-request-size")) {
			server.maxRequestSize(config.getInt("max-request-size"));
		}

		if (config.hasPath("max-connections")) {
			server.maxConnections(config.getInt("max-connections"));
		}

		String requestLogger = null;
		if (config.hasPath("request-logger")) {
			requestLogger = config.getString("request-logger");
		}

		String errorLogger = null;
		if (config.hasPath("error-logger")) {
			errorLogger = config.getString("error-logger");
		}

		server.logger(new SLF4JRequestLogger(requestLogger, errorLogger));

		if (config.hasPath("local-address")) {

			final SocketAddress address = AddressUtil.parseAddress(config.getString("local-address"));

			log.debug("Starting HTTP server on " + address);
			server.listen(address).sync();

		} else {

			log.debug("Starting HTTP server on *:8080");
			server.listen(8080).sync();

		}

	}

	public void deactivate() {

		if (server.running()) {

			log.debug("Stopping HTTP server");

			try {
				server.shutdown().sync();
			} catch (final Throwable t) {
				log.error("Could not shutdown HTTP server", t);
			}

		} else {
			log.debug("HTTP server not running!");
		}

	}

	@Override
	public void registerHandler(final String path, final HandlerFactory<RequestHandler> factory) {
		server.requestHandler(path, factory);
		log.debug("Registered handler for " + path + ": " + factory);
	}

	@Override
	public void registerWebSocket(final String path, final HandlerFactory<? extends ChannelHandler> factory) {
		server.webSocketHandler(path, factory);
		log.debug("Registered websocket service for " + path + ": " + factory);
	}

	@Override
	public void unregisterHandler(final String path) {
		server.removeRequestHandler(path);
		server.removeWebSocketHandler(path);
		log.debug("Unregistered handler for " + path);
{'fraction_non_alphanumeric': 0.12247191011235956, 'fraction_numerical': 0.0032459425717852684, 'mean_word_length': 5.0827638572513285, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 15, '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': '142711', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2397, 'n_words': 499}