Skip to content

Commit

Permalink
continue the code
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryshao committed May 29, 2023
1 parent f442646 commit c8c6ec8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,21 @@ public class ServerConfig extends Config {
.doc("The core thread size of the built-in web server")
.version("0.1.0")
.intConf()
.createWithDefault(
Math.min(Runtime.getRuntime().availableProcessors() * 2, 100));
.createWithDefault(Math.min(Runtime.getRuntime().availableProcessors() * 2, 100));

public static final ConfigEntry<Integer> WEBSERVER_MAX_THREADS =
new ConfigBuilder("graviton.server.webserver.maxThreads")
.doc("The max thread size of the built-in web server")
.version("0.1.0")
.intConf()
.createWithDefault(
Math.max(Runtime.getRuntime().availableProcessors() * 4, 400));
.createWithDefault(Math.max(Runtime.getRuntime().availableProcessors() * 4, 400));

public static final ConfigEntry<Long> WEBSERVER_STOP_IDLE_TIMEOUT =
new ConfigBuilder("graviton.server.webserver.stopIdleTimeout")
.doc("The stop idle timeout of the built-in web server")
.version("0.1.0")
.longConf()
.createWithDefault(30* 1000L);
.createWithDefault(30 * 1000L);

public static final ConfigEntry<Integer> WEBSERVER_REQUEST_HEADER_SIZE =
new ConfigBuilder("graviton.server.webserver.requestHeaderSize")
Expand All @@ -57,7 +55,6 @@ public class ServerConfig extends Config {
.intConf()
.createWithDefault(128 * 1024);


public ServerConfig(boolean loadDefaults) {
super(loadDefaults);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
import com.datastrato.graviton.server.GravitonServerException;
import com.datastrato.graviton.server.ServerConfig;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.net.BindException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.servlet.Servlet;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.BindException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class JettyServer {

private static final Logger LOG = LoggerFactory.getLogger(JettyServer.class);
Expand All @@ -29,8 +30,9 @@ public final class JettyServer {

private int httpPort;

public JettyServer() {
}
private ServletContextHandler servletContextHandler;

public JettyServer() {}

public synchronized void initialize(Config config) {
int coreThreads = config.get(ServerConfig.WEBSERVER_CORE_THREADS);
Expand All @@ -51,35 +53,27 @@ public synchronized void initialize(Config config) {
// Create and set Http ServerConnector
int reqHeaderSize = config.get(ServerConfig.WEBSERVER_REQUEST_HEADER_SIZE);
int respHeaderSize = config.get(ServerConfig.WEBSERVER_RESPONSE_HEADER_SIZE);
ServerConnector httpConnector =
createHttpServerConnector(server, reqHeaderSize, respHeaderSize);

host = config.get(ServerConfig.WEBSERVER_HOST);
httpPort = config.get(ServerConfig.WEBSERVER_PORT);
httpConnector.setHost(host);
httpConnector.setPort(httpPort);
httpConnector.setReuseAddress(true);

ServerConnector httpConnector =
createHttpServerConnector(server, reqHeaderSize, respHeaderSize, host, httpPort);
server.addConnector(httpConnector);

// TODO. Create and set https connector

ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(DefaultServlet.class, "/");

HandlerCollection handlers = new HandlerCollection();
handlers.addHandler(servletContextHandler);
// TODO. Create and set https connector @jerry

server.setHandler(handlers);
// Initialize ServletContextHandler
initializeServletContextHandler(server);
}

public synchronized void start() throws GravitonServerException {
try {
server.start();
} catch (BindException e) {
LOG.error("Failed to start web server on host {} port {}, which is already in use.",
host, httpPort, e);
LOG.error(
"Failed to start web server on host {} port {}, which is already in use.",
host,
httpPort,
e);
throw new GravitonServerException("Failed to start web server.", e);

} catch (Exception e) {
Expand Down Expand Up @@ -122,40 +116,57 @@ public synchronized void stop() {
}
}

private ExecutorThreadPool createThreadPool(int coreThreads, int maxThreads) {
return new ExecutorThreadPool(
new ThreadPoolExecutor(
coreThreads,
maxThreads,
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("jetty-webserver-%d")
.build())
);
public void addServlet(Servlet servlet, String pathSpec) {
servletContextHandler.addServlet(new ServletHolder(servlet), pathSpec);
}

private ServerConnector creatorServerConnector(
Server server, ConnectionFactory[] connectionFactories) {
Scheduler serverExecutor =
new ScheduledExecutorScheduler("graviton-webserver-JettyScheduler", true);
private void initializeServletContextHandler(Server server) {
this.servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(DefaultServlet.class, "/");

ServerConnector connector = new ServerConnector(
server, null, serverExecutor, null, -1, -1, connectionFactories);
HandlerCollection handlers = new HandlerCollection();
handlers.addHandler(servletContextHandler);

return connector;
server.setHandler(handlers);
}

private ServerConnector createHttpServerConnector(Server server, int reqHeaderSize, int respHeaderSize) {
private ServerConnector createHttpServerConnector(
Server server, int reqHeaderSize, int respHeaderSize, String host, int port) {
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setRequestHeaderSize(reqHeaderSize);
httpConfig.setResponseHeaderSize(respHeaderSize);
httpConfig.setSendServerVersion(true);

HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConfig);
ServerConnector connector =
creatorServerConnector(server, new ConnectionFactory[] {httpConnectionFactory});
connector.setHost(host);
connector.setPort(port);
connector.setReuseAddress(true);

return connector;
}

private ServerConnector creatorServerConnector(
Server server, ConnectionFactory[] connectionFactories) {
Scheduler serverExecutor =
new ScheduledExecutorScheduler("graviton-webserver-JettyScheduler", true);

return new ServerConnector(server, null, serverExecutor, null, -1, -1, connectionFactories);
}

return creatorServerConnector(server, new ConnectionFactory[] {httpConnectionFactory});
private ExecutorThreadPool createThreadPool(int coreThreads, int maxThreads) {
return new ExecutorThreadPool(
new ThreadPoolExecutor(
coreThreads,
maxThreads,
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("jetty-webserver-%d")
.build()));
}
}

0 comments on commit c8c6ec8

Please sign in to comment.