Skip to content

Commit

Permalink
Inject Netty executors into Styx Core. (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkokar authored Jan 14, 2020
1 parent dedeb5a commit c9298c0
Show file tree
Hide file tree
Showing 37 changed files with 289 additions and 797 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2019 Expedia Inc.
Copyright (C) 2013-2020 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.net.HostAndPort;
import com.hotels.styx.NettyExecutor;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.LiveHttpRequest;
Expand Down Expand Up @@ -178,13 +179,16 @@ private static Origin originFromRequest(LiveHttpRequest request, Boolean isHttps
* Builder for {@link StyxHttpClient}.
*/
public static class Builder {
private static final NettyExecutor DEFAULT_EXECUTOR = NettyExecutor.create("Styx-Client", 0);

private int connectTimeoutMillis = 1000;
private int maxResponseSize = 1024 * 100;
private int responseTimeout = 60000;
private int maxHeaderSize = 8192;
private TlsSettings tlsSettings;
private boolean isHttps;
private String userAgent;
private NettyExecutor executor = DEFAULT_EXECUTOR;

public Builder() {
}
Expand Down Expand Up @@ -314,19 +318,24 @@ Builder copy() {
return new Builder(this);
}

public Builder executor(NettyExecutor executor) {
this.executor = executor;
return this;
}

/**
* Construct a client instance.
*
* @return a new instance
*/
public StyxHttpClient build() {

NettyConnectionFactory connectionFactory = new NettyConnectionFactory.Builder()
.httpConfig(newHttpConfigBuilder().setMaxHeadersSize(maxHeaderSize).build())
.tlsSettings(tlsSettings)
.httpRequestOperationFactory(httpRequestOperationFactoryBuilder()
.responseTimeoutMillis(responseTimeout)
.build())
.executor(executor)
.build();

return new StyxHttpClient(connectionFactory, this.copy());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2019 Expedia Inc.
Copyright (C) 2013-2020 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package com.hotels.styx.client.netty.connectionpool;

import com.hotels.styx.NettyExecutor;
import com.hotels.styx.api.exceptions.OriginUnreachableException;
import com.hotels.styx.api.extension.Origin;
import com.hotels.styx.api.extension.service.TlsSettings;
Expand All @@ -23,14 +24,12 @@
import com.hotels.styx.client.ConnectionSettings;
import com.hotels.styx.client.HttpConfig;
import com.hotels.styx.client.HttpRequestOperationFactory;
import com.hotels.styx.client.netty.eventloop.PlatformAwareClientEventLoopGroupFactory;
import com.hotels.styx.client.ssl.SslContextFactory;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.ssl.SslContext;
import reactor.core.publisher.Mono;

Expand All @@ -48,24 +47,17 @@
* A connection factory that creates connections using netty.
*/
public class NettyConnectionFactory implements Connection.Factory {
private static final PlatformAwareClientEventLoopGroupFactory FACTORY = new PlatformAwareClientEventLoopGroupFactory(
"Styx-Client-Global",
0);
private static final Class<? extends Channel> GLOBAL_CLIENT_EVENT_LOOP_CLASS = FACTORY.clientSocketChannelClass();
private static final EventLoopGroup GLOBAL_CLIENT_EVENT_LOOP = FACTORY.newClientWorkerEventLoopGroup();

private final HttpConfig httpConfig;
private final SslContext sslContext;
private final boolean sendSni;
private final Optional<String> sniHost;
private final HttpRequestOperationFactory httpRequestOperationFactory;
private final NettyExecutor executor;
private Bootstrap bootstrap;
private EventLoopGroup eventLoopGroup;
private Class<? extends Channel> clientSocketChannelClass;

private NettyConnectionFactory(Builder builder) {
this.clientSocketChannelClass = requireNonNull(builder.channelClass);
this.eventLoopGroup = requireNonNull(builder.eventLoopGroup);
this.executor = requireNonNull(builder.executor);
this.httpConfig = requireNonNull(builder.httpConfig);
this.sslContext = builder.tlsSettings == null ? null : SslContextFactory.get(builder.tlsSettings);
this.httpRequestOperationFactory = requireNonNull(builder.httpRequestOperationFactory);
Expand Down Expand Up @@ -101,8 +93,8 @@ private ChannelFuture openConnection(Origin origin, ConnectionSettings connectio
private synchronized void bootstrap(ConnectionSettings connectionSettings) {
if (bootstrap == null) {
bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(clientSocketChannelClass)
bootstrap.group(executor.eventLoopGroup())
.channel(executor.clientEventLoopClass())
.handler(new Initializer())
.option(TCP_NODELAY, true)
.option(SO_KEEPALIVE, true)
Expand All @@ -124,15 +116,15 @@ protected void initChannel(Channel ch) {
* Builder.
*/
public static final class Builder {
private static final NettyExecutor DEFAULT_EXECUTOR = NettyExecutor.create("Netty-Executor", 0);

private HttpRequestOperationFactory httpRequestOperationFactory = httpRequestOperationFactoryBuilder().build();
private HttpConfig httpConfig = defaultHttpConfig();
private TlsSettings tlsSettings;
private EventLoopGroup eventLoopGroup = GLOBAL_CLIENT_EVENT_LOOP;
private Class<? extends Channel> channelClass = GLOBAL_CLIENT_EVENT_LOOP_CLASS;
private NettyExecutor executor = DEFAULT_EXECUTOR;

public Builder nettyEventLoop(EventLoopGroup eventLoopGroup, Class<? extends Channel> channelClass) {
this.eventLoopGroup = requireNonNull(eventLoopGroup);
this.channelClass = requireNonNull(channelClass);
public Builder executor(NettyExecutor executor) {
this.executor = executor;
return this;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions components/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<artifactId>styx-api</artifactId>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<classifier>${netty-transport-native-epoll.classifier}</classifier>
</dependency>

<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
Expand Down
Loading

0 comments on commit c9298c0

Please sign in to comment.