Implement high performance HTTP api for plugins with jetty.

This commit is contained in:
md_5
2013-03-20 18:52:26 +11:00
parent 692610cd7e
commit 8827feacfb
6 changed files with 65 additions and 11 deletions

View File

@@ -11,7 +11,6 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
import net.md_5.bungee.config.Configuration;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.util.Calendar;
@@ -22,6 +21,10 @@ import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -45,6 +48,8 @@ import net.md_5.bungee.config.YamlConfig;
import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.packet.DefinedPacket;
import net.md_5.bungee.packet.PacketFAPluginMessage;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
/**
* Main BungeeCord proxy class.
@@ -69,9 +74,10 @@ public class BungeeCord extends ProxyServer
*/
public final Configuration config = new Configuration();
/**
* Thread pool.
* Thread pools.
*/
public final MultithreadEventLoopGroup eventLoops = new NioEventLoopGroup( 0, new ThreadFactoryBuilder().setNameFormat( "Netty IO Thread - %1$d" ).build() );
public final ScheduledExecutorService executors = new ScheduledThreadPoolExecutor( 8, new ThreadFactoryBuilder().setNameFormat( "Bungee Pool Thread #%1$d" ).build() );
public final MultithreadEventLoopGroup eventLoops = new NioEventLoopGroup( 8, new ThreadFactoryBuilder().setNameFormat( "Netty IO Thread #%1$d" ).build() );
/**
* locations.yml save thread.
*/
@@ -106,6 +112,8 @@ public class BungeeCord extends ProxyServer
private final File pluginsFolder = new File( "plugins" );
@Getter
private final TaskScheduler scheduler = new BungeeScheduler();
@Getter
private final HttpClient httpClient = new HttpClient();
{
@@ -170,11 +178,14 @@ public class BungeeCord extends ProxyServer
* Start this proxy instance by loading the configuration, plugins and
* starting the connect thread.
*
* @throws IOException
* @throws Exception
*/
@Override
public void start() throws IOException
public void start() throws Exception
{
httpClient.setExecutor( executors );
httpClient.start();
httpClient.GET( "http://isup.me/" );
pluginsFolder.mkdir();
pluginManager.loadPlugins( pluginsFolder );
config.load();
@@ -238,6 +249,16 @@ public class BungeeCord extends ProxyServer
{
this.isRunning = false;
try
{
getLogger().info( "Stopping HTTP client" );
httpClient.stop();
} catch ( Exception ex )
{
getLogger().severe( "Could not stop HTTP client" );
}
executors.shutdown();
stopListeners();
getLogger().info( "Closing pending connections" );

View File

@@ -1,15 +1,13 @@
package net.md_5.bungee.scheduler;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import gnu.trove.TCollections;
import gnu.trove.iterator.TIntObjectIterator;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.api.scheduler.TaskScheduler;
@@ -19,7 +17,6 @@ public class BungeeScheduler implements TaskScheduler
private final AtomicInteger taskCounter = new AtomicInteger();
private final TIntObjectMap<BungeeTask> tasks = TCollections.synchronizedMap( new TIntObjectHashMap<BungeeTask>() );
private final ScheduledExecutorService executors = new ScheduledThreadPoolExecutor( 0, new ThreadFactoryBuilder().setNameFormat( "Bungee Scheduler Thread - %1$d" ).build() );
@Override
public void cancel(int id)
@@ -60,13 +57,13 @@ public class BungeeScheduler implements TaskScheduler
@Override
public ScheduledTask schedule(Plugin owner, Runnable task, long delay, TimeUnit unit)
{
return prepare( owner, task ).setFuture( executors.schedule( task, delay, unit ) );
return prepare( owner, task ).setFuture( BungeeCord.getInstance().executors.schedule( task, delay, unit ) );
}
@Override
public ScheduledTask schedule(Plugin owner, Runnable task, long delay, long period, TimeUnit unit)
{
return prepare( owner, task ).setFuture( executors.scheduleWithFixedDelay( task, delay, period, unit ) );
return prepare( owner, task ).setFuture( BungeeCord.getInstance().executors.scheduleWithFixedDelay( task, delay, period, unit ) );
}
private BungeeTask prepare(Plugin owner, Runnable task)