Skip to content

Commit

Permalink
Merge pull request #239 from jekh/server-group-refactor
Browse files Browse the repository at this point in the history
ServerGroup refactor
  • Loading branch information
jekh committed Sep 26, 2015
2 parents 6f09262 + c2b2842 commit 47611ba
Show file tree
Hide file tree
Showing 13 changed files with 977 additions and 334 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/littleshoot/proxy/HttpProxyServer.java
Expand Up @@ -14,13 +14,14 @@ public interface HttpProxyServer {
/**
* <p>
* Clone the existing server, with a port 1 higher and everything else the
* same.
* same. If the proxy was started with port 0 (JVM-assigned port), the cloned proxy will also use a JVM-assigned
* port.
* </p>
*
* <p>
* The new server will share event loops with the original server. The event
* loops will use whatever name was given to the first server in the clone
* group.
* group. The server group will not terminate until the original server and all clones terminate.
* </p>
*
* @return a bootstrap that allows customizing and starting the cloned
Expand Down
@@ -1,5 +1,7 @@
package org.littleshoot.proxy;

import org.littleshoot.proxy.impl.ThreadPoolConfiguration;

import java.net.InetSocketAddress;

/**
Expand Down Expand Up @@ -309,4 +311,11 @@ HttpProxyServerBootstrap withConnectTimeout(
*/
HttpProxyServer start();

/**
* Set the configuration parameters for the proxy's thread pools.
*
* @param configuration thread pool configuration
* @return proxy server bootstrap for chaining
*/
HttpProxyServerBootstrap withThreadPoolConfiguration(ThreadPoolConfiguration configuration);
}

This file was deleted.

@@ -0,0 +1,12 @@
package org.littleshoot.proxy;

/**
* This exception indicates that the system was asked to use a TransportProtocol that it didn't know how to handle.
*/
public class UnknownTransportProtocolException extends RuntimeException {
private static final long serialVersionUID = 1L;

public UnknownTransportProtocolException(TransportProtocol transportProtocol) {
super(String.format("Unknown TransportProtocol: %1$s", transportProtocol));
}
}
@@ -0,0 +1,52 @@
package org.littleshoot.proxy.impl;

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

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
* A ThreadFactory that adds LittleProxy-specific information to the threads' names.
*/
public class CategorizedThreadFactory implements ThreadFactory {
private static final Logger log = LoggerFactory.getLogger(CategorizedThreadFactory.class);

private final String name;
private final String category;
private final int uniqueServerGroupId;

private AtomicInteger threadCount = new AtomicInteger(0);

/**
* Exception handler for proxy threads. Logs the name of the thread and the exception that was caught.
*/
private static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("Uncaught throwable in thread: {}", t.getName(), e);
}
};


/**
* @param name the user-supplied name of this proxy
* @param category the type of threads this factory is creating (acceptor, client-to-proxy worker, proxy-to-server worker)
* @param uniqueServerGroupId a unique number for the server group creating this thread factory, to differentiate multiple proxy instances with the same name
*/
public CategorizedThreadFactory(String name, String category, int uniqueServerGroupId) {
this.category = category;
this.name = name;
this.uniqueServerGroupId = uniqueServerGroupId;
}

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, name + "-" + uniqueServerGroupId + "-" + category + "-" + threadCount.getAndIncrement());

t.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER);

return t;
}

}

0 comments on commit 47611ba

Please sign in to comment.