Made threads-per-cpu configurable for moar speed

This commit is contained in:
Amos Ng 2020-06-12 03:10:00 +08:00
parent ac682e98c3
commit 40be05e4d4
No known key found for this signature in database
GPG key ID: 89086414F634D123
3 changed files with 15 additions and 7 deletions

View file

@ -3,5 +3,6 @@
"max_cache_size_mib": 2048, "max_cache_size_mib": 2048,
"client_port": 8080, "client_port": 8080,
"max_burst_rate_kib_per_second": 100, "max_burst_rate_kib_per_second": 100,
"max_bandwidth_mib_per_hour": 1 "max_bandwidth_mib_per_hour": 1,
"threads_per_cpu": 32
} }

View file

@ -16,14 +16,17 @@ public final class ClientSettings {
private final int clientPort; private final int clientPort;
@SerializedName("client_secret") @SerializedName("client_secret")
private final String clientSecret; private final String clientSecret;
@SerializedName("threads_per_cpu")
private final int threadsPerCPU;
public ClientSettings(long maxCacheSizeMib, long maxBandwidthMibPerHour, long maxBurstRateKibPerSecond, public ClientSettings(long maxCacheSizeMib, long maxBandwidthMibPerHour, long maxBurstRateKibPerSecond,
int clientPort, String clientSecret) { int clientPort, String clientSecret, int threadsPerCPU) {
this.maxCacheSizeMib = maxCacheSizeMib; this.maxCacheSizeMib = maxCacheSizeMib;
this.maxBandwidthMibPerHour = maxBandwidthMibPerHour; this.maxBandwidthMibPerHour = maxBandwidthMibPerHour;
this.maxBurstRateKibPerSecond = maxBurstRateKibPerSecond; this.maxBurstRateKibPerSecond = maxBurstRateKibPerSecond;
this.clientPort = clientPort; this.clientPort = clientPort;
this.clientSecret = Objects.requireNonNull(clientSecret); this.clientSecret = Objects.requireNonNull(clientSecret);
this.threadsPerCPU = threadsPerCPU;
} }
public long getMaxCacheSizeMib() { public long getMaxCacheSizeMib() {
@ -46,11 +49,15 @@ public final class ClientSettings {
return clientSecret; return clientSecret;
} }
public int getThreadsPerCPU() {
return threadsPerCPU;
}
@Override @Override
public String toString() { public String toString() {
return "ClientSettings{" + "maxCacheSizeMib=" + maxCacheSizeMib + ", maxBandwidthMibPerHour=" return "ClientSettings{" + "maxCacheSizeMib=" + maxCacheSizeMib + ", maxBandwidthMibPerHour="
+ maxBandwidthMibPerHour + ", maxBurstRateKibPerSecond=" + maxBurstRateKibPerSecond + ", clientPort=" + maxBandwidthMibPerHour + ", maxBurstRateKibPerSecond=" + maxBurstRateKibPerSecond + ", clientPort="
+ clientPort + ", clientSecret='" + "<hidden>" + '\'' + '}'; + clientPort + ", clientSecret='" + "<hidden>" + '\'' + ", threadsPerCPU=" + threadsPerCPU + "}";
} }
public static boolean isSecretValid(String clientSecret) { public static boolean isSecretValid(String clientSecret) {

View file

@ -35,12 +35,12 @@ import java.util.concurrent.atomic.AtomicReference
import javax.net.ssl.SSLException import javax.net.ssl.SSLException
private val LOGGER = LoggerFactory.getLogger("Application") private val LOGGER = LoggerFactory.getLogger("Application")
private val THREADS_TO_ALLOCATE = Runtime.getRuntime().availableProcessors() * 32 / 2 private val THREADS_TO_ALLOCATE = Runtime.getRuntime().availableProcessors()
class Netty(private val tls: ServerSettings.TlsCert, private val clientSettings: ClientSettings, private val stats: AtomicReference<Statistics>) : ServerConfig { class Netty(private val tls: ServerSettings.TlsCert, private val clientSettings: ClientSettings, private val stats: AtomicReference<Statistics>) : ServerConfig {
override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer { override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer {
private val masterGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE) private val masterGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE * clientSettings.getThreadsPerCPU())
private val workerGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE) private val workerGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE * clientSettings.getThreadsPerCPU())
private lateinit var closeFuture: ChannelFuture private lateinit var closeFuture: ChannelFuture
private lateinit var address: InetSocketAddress private lateinit var address: InetSocketAddress
@ -54,7 +54,7 @@ class Netty(private val tls: ServerSettings.TlsCert, private val clientSettings:
override fun start(): Http4kServer = apply { override fun start(): Http4kServer = apply {
if (LOGGER.isInfoEnabled) { if (LOGGER.isInfoEnabled) {
LOGGER.info("Starting webserver with {} threads", THREADS_TO_ALLOCATE) LOGGER.info("Starting webserver with {} threads", THREADS_TO_ALLOCATE * clientSettings.getThreadsPerCPU())
} }
val (mainCert, chainCert) = getX509Certs(tls.certificate) val (mainCert, chainCert) = getX509Certs(tls.certificate)