Skip to content

Commit

Permalink
Merge pull request #93 from brharrington/split-clientcfg
Browse files Browse the repository at this point in the history
split out client config
  • Loading branch information
brharrington committed Jan 12, 2015
2 parents a9ce9e7 + 9d425a2 commit 4b5187b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spectator.http;

import com.netflix.spectator.api.Spectator;

import java.net.URI;

/** Configuration settings to use for making the request. */
class ClientConfig {

private final String name;
private final String vipAddress;
private final URI originalUri;
private final URI uri;

/** Create a new instance. */
ClientConfig(String name, String vipAddress, URI originalUri, URI uri) {
this.name = name;
this.vipAddress = vipAddress;
this.originalUri = originalUri;
this.uri = uri;
}

private String prop(String k) {
return name + ".niws.client." + k;
}

/** Name of the client. */
String name() {
return name;
}

/** Original URI specified before selecting a specific server. */
URI originalUri() {
return originalUri;
}

/** URI for the request. */
URI uri() {
return uri;
}

/** Port to use for the connection. */
int port(int dflt) {
return Spectator.config().getInt(prop("Port"), dflt);
}

/** Maximum time to wait for a connection attempt in milliseconds. */
int connectTimeout() {
return Spectator.config().getInt(prop("ConnectTimeout"), 1000);
}

/** Maximum time to wait for reading data in milliseconds. */
int readTimeout() {
return Spectator.config().getInt(prop("ReadTimeout"), 30000);
}

/** Maximum number of redirects to follow. Set to 0 to disable. */
int followRedirects() {
return Spectator.config().getInt(prop("FollowRedirects"), 3);
}

/** Should HTTPS be used for the request? */
boolean isSecure() {
final boolean https = "https".equals(uri.getScheme());
return https || Spectator.config().getBoolean(prop("IsSecure"), false);
}

/**
* When getting a server list from eureka should the host name or ip address be used? The
* default is to use the ip address and avoid the dns lookup.
*/
boolean useIpAddress() {
return Spectator.config().getBoolean(prop("UseIpAddress"), false);
}

/**
* Should it attempt to compress the request body and automatically decompress the response
* body?
*/
boolean gzipEnabled() {
return Spectator.config().getBoolean(prop("GzipEnabled"), true);
}

/** Max number of retries. */
int numRetries() {
return Spectator.config().getInt(prop("MaxAutoRetriesNextServer"), 2);
}

/**
* Initial delay to use between retries if a throttled response (429 or 503) is received. The
* delay will be doubled between each throttled attempt.
*/
int retryDelay() {
return Spectator.config().getInt(prop("RetryDelay"), 500);
}

/** Max size of the request body. Defaults to 10MB. */
int aggregationLimit() {
return Spectator.config().getInt(prop("AggregationLimit"), 10 * 1024 * 1024);
}

/** User agent string to use when making the request. */
String userAgent() {
return Spectator.config().get(prop("UserAgent"), "RxHttp");
}

/** VIP used to lookup a set of servers in eureka. */
String vip() {
return (vipAddress == null)
? Spectator.config().get(prop("DeploymentContextBasedVipAddresses"))
: vipAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ErrorRetryHandler implements
Func1<Throwable, Observable<? extends HttpClientResponse<ByteBuf>>> {

private final HttpLogEntry entry;
private final RxHttp.ClientConfig config;
private final ClientConfig config;
private final Server server;
private final HttpClientRequest<ByteBuf> req;

Expand All @@ -56,7 +56,7 @@ class ErrorRetryHandler implements
*/
ErrorRetryHandler(
HttpLogEntry entry,
RxHttp.ClientConfig config,
ClientConfig config,
Server server,
HttpClientRequest<ByteBuf> req,
int attempt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RedirectHandler implements

private final HttpLogEntry entry;
private final HttpClientRequest<ByteBuf> req;
private final RxHttp.ClientConfig config;
private final ClientConfig config;
private final Server server;

private int redirect;
Expand All @@ -54,7 +54,7 @@ class RedirectHandler implements
*/
RedirectHandler(
HttpLogEntry entry,
RxHttp.ClientConfig config,
ClientConfig config,
Server server,
HttpClientRequest<ByteBuf> req) {
this.entry = entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.spectator.api.Spectator;
import com.netflix.spectator.impl.Preconditions;
import com.netflix.spectator.sandbox.HttpLogEntry;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -611,111 +610,4 @@ private static class HttpDecompressionConfigurator implements PipelineConfigurat
}
}

/** Configuration settings to use for making the request. */
static class ClientConfig {

private final String name;
private final String vipAddress;
private final URI originalUri;
private final URI uri;

/** Create a new instance. */
ClientConfig(String name, String vipAddress, URI originalUri, URI uri) {
this.name = name;
this.vipAddress = vipAddress;
this.originalUri = originalUri;
this.uri = uri;
}

private String prop(String k) {
return name + ".niws.client." + k;
}

/** Name of the client. */
String name() {
return name;
}

/** Original URI specified before selecting a specific server. */
URI originalUri() {
return originalUri;
}

/** URI for the request. */
URI uri() {
return uri;
}

/** Port to use for the connection. */
int port(int dflt) {
return Spectator.config().getInt(prop("Port"), dflt);
}

/** Maximum time to wait for a connection attempt in milliseconds. */
int connectTimeout() {
return Spectator.config().getInt(prop("ConnectTimeout"), 1000);
}

/** Maximum time to wait for reading data in milliseconds. */
int readTimeout() {
return Spectator.config().getInt(prop("ReadTimeout"), 30000);
}

/** Maximum number of redirects to follow. Set to 0 to disable. */
int followRedirects() {
return Spectator.config().getInt(prop("FollowRedirects"), 3);
}

/** Should HTTPS be used for the request? */
boolean isSecure() {
final boolean https = "https".equals(uri.getScheme());
return https || Spectator.config().getBoolean(prop("IsSecure"), false);
}

/**
* When getting a server list from eureka should the host name or ip address be used? The
* default is to use the ip address and avoid the dns lookup.
*/
boolean useIpAddress() {
return Spectator.config().getBoolean(prop("UseIpAddress"), false);
}

/**
* Should it attempt to compress the request body and automatically decompress the response
* body?
*/
boolean gzipEnabled() {
return Spectator.config().getBoolean(prop("GzipEnabled"), true);
}

/** Max number of retries. */
int numRetries() {
return Spectator.config().getInt(prop("MaxAutoRetriesNextServer"), 2);
}

/**
* Initial delay to use between retries if a throttled response (429 or 503) is received. The
* delay will be doubled between each throttled attempt.
*/
int retryDelay() {
return Spectator.config().getInt(prop("RetryDelay"), 500);
}

/** Max size of the request body. Defaults to 10MB. */
int aggregationLimit() {
return Spectator.config().getInt(prop("AggregationLimit"), 10 * 1024 * 1024);
}

/** User agent string to use when making the request. */
String userAgent() {
return Spectator.config().get(prop("UserAgent"), "RxHttp");
}

/** VIP used to lookup a set of servers in eureka. */
String vip() {
return (vipAddress == null)
? Spectator.config().get(prop("DeploymentContextBasedVipAddresses"))
: vipAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StatusRetryHandler implements
Func1<HttpClientResponse<ByteBuf>, Observable<HttpClientResponse<ByteBuf>>> {

private final HttpLogEntry entry;
private final RxHttp.ClientConfig config;
private final ClientConfig config;
private final Server server;
private final HttpClientRequest<ByteBuf> req;

Expand All @@ -59,7 +59,7 @@ class StatusRetryHandler implements
*/
StatusRetryHandler(
HttpLogEntry entry,
RxHttp.ClientConfig config,
ClientConfig config,
Server server,
HttpClientRequest<ByteBuf> req,
int attempt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.ConfigurationManager;
import com.netflix.spectator.http.RxHttp;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
Expand Down Expand Up @@ -571,7 +570,7 @@ public void portOverrideSetting() throws Exception {
set("port-override.niws.client.Port", "2");
URI origUri = URI.create("niws://port-override/foo");
URI relUri = URI.create("/foo");
RxHttp.ClientConfig cfg = new RxHttp.ClientConfig("port-override", "vip", origUri, relUri);
ClientConfig cfg = new ClientConfig("port-override", "vip", origUri, relUri);
InstanceInfo info = InstanceInfo.Builder.newBuilder()
.setAppName("foo")
.setPort(1)
Expand All @@ -584,7 +583,7 @@ public void portOverrideSetting() throws Exception {
public void portDefaultSetting() throws Exception {
URI origUri = URI.create("niws://port-default/foo");
URI relUri = URI.create("/foo");
RxHttp.ClientConfig cfg = new RxHttp.ClientConfig("port-default", "vip", origUri, relUri);
ClientConfig cfg = new ClientConfig("port-default", "vip", origUri, relUri);
InstanceInfo info = InstanceInfo.Builder.newBuilder()
.setAppName("foo")
.setPort(1)
Expand Down

0 comments on commit 4b5187b

Please sign in to comment.