com.netease.stream.config.ClientConfiguration Maven / Gradle / Ivy
package com.netease.stream.config;
import com.netease.stream.http.Protocol;
import com.netease.stream.util.PropertiesUtils;
/**
* Client configuration options such as proxy settings, user agent string, max retry attempts, etc.
*/
public class ClientConfiguration {
/** The default timeout for a connected socket. */
public static final int DEFAULT_SOCKET_TIMEOUT = 50 * 1000;
/** The default max connection pool size. */
public static final int DEFAULT_MAX_CONNECTIONS = 50;
/** The default HTTP user agent header for Java SDK clients. */
public static final String DEFAULT_USER_AGENT = PropertiesUtils.getUserAgent();
/** The HTTP user agent header passed with all HTTP requests. */
private String userAgent = DEFAULT_USER_AGENT;
/**
* The protocol to use when connecting to the Web Services.
*
* The default configuration is to use HTTP for all requests for increased security.
*/
private Protocol protocol = Protocol.HTTP;
/** The maximum number of open HTTP connections. */
private int maxConnections = DEFAULT_MAX_CONNECTIONS;
/**
* The amount of time to wait (in milliseconds) for data to be transfered over an established,
* open connection before the connection is timed out. A value of 0 means infinity, and is not
* recommended.
*/
private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
/**
* The amount of time to wait (in milliseconds) when initially establishing a connection before
* giving up and timing out. A value of 0 means infinity, and is not recommended.
*/
private int connectionTimeout = 50 * 1000;
/**
* Optional size hint (in bytes) for the low level TCP send buffer. This is an advanced option
* for advanced users who want to tune low level TCP parameters to try and squeeze out more
* performance.
*/
private int socketSendBufferSizeHint = 0;
/**
* Optional size hint (in bytes) for the low level TCP receive buffer. This is an advanced
* option for advanced users who want to tune low level TCP parameters to try and squeeze out
* more performance.
*/
private int socketReceiveBufferSizeHint = 0;
public ClientConfiguration() {}
public ClientConfiguration(ClientConfiguration other) {
this.connectionTimeout = other.connectionTimeout;
this.maxConnections = other.maxConnections;
this.protocol = other.protocol;
this.socketTimeout = other.socketTimeout;
this.userAgent = other.userAgent;
this.socketReceiveBufferSizeHint = other.socketReceiveBufferSizeHint;
this.socketSendBufferSizeHint = other.socketSendBufferSizeHint;
}
/**
* Returns the protocol (HTTP or HTTPS) to use when connecting to the Web Services.
*
* The default configuration is to use HTTPS for all requests for increased security.
*
* Individual clients can also override this setting by explicitly including the protocol as
* part of the endpoint URL.
*
* @return The protocol to use when connecting to teh Web Services.
*/
public Protocol getProtocol() {
return protocol;
}
/**
* Sets the protocol (i.e. HTTP or HTTPS) to use when connecting to the Web Services.
*
* The default configuration is to use HTTPS for all requests for increased security.
*
* Individual clients can also override this setting by explicitly including the protocol as
* part of the endpoint URL.
*
* @param protocol The protocol to use when connecting to the Web Services.
*/
public void setProtocol(Protocol protocol) {
this.protocol = protocol;
}
/**
* Sets the protocol (i.e. HTTP or HTTPS) to use when connecting to the Web Services, and
* returns the updated ClientConfiguration object so that additional calls may be chained
* together.
*
* The default configuration is to use HTTPS for all requests for increased security.
*
* Individual clients can also override this setting by explicitly including the protocol as
* part of the endpoint URL.
*
* @param protocol The protocol to use when connecting to the Web Services.
* @return The updated ClientConfiguration object with the new max HTTP connections setting.
*/
public ClientConfiguration withProtocol(Protocol protocol) {
setProtocol(protocol);
return this;
}
/**
* Returns the maximum number of allowed open HTTP connections.
*
* @return The maximum number of allowed open HTTP connections.
*/
public int getMaxConnections() {
return maxConnections;
}
/**
* Sets the maximum number of allowed open HTTP connections.
*
* @param maxConnections The maximum number of allowed open HTTP connections.
*/
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
/**
* Sets the maximum number of allowed open HTTP connections and returns the updated
* ClientConfiguration object.
*
* @param maxConnections The maximum number of allowed open HTTP connections.
* @return The updated ClientConfiguration object with the new max HTTP connections setting.
*/
public ClientConfiguration withMaxConnections(int maxConnections) {
setMaxConnections(maxConnections);
return this;
}
/**
* Returns the HTTP user agent header to send with all requests.
*
* @return The user agent string to use when sending requests.
*/
public String getUserAgent() {
return userAgent;
}
/**
* Sets the HTTP user agent header to send with all requests.
*
* @param userAgent The user agent string to use when sending requests.
*/
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
/**
* Sets the HTTP user agent header used in requests and returns the updated ClientConfiguration
* object.
*
* @param userAgent The user agent string to use when sending requests.
* @return The updated ClientConfiguration object.
*/
public ClientConfiguration withUserAgent(String userAgent) {
setUserAgent(userAgent);
return this;
}
/**
* Returns the amount of time to wait (in milliseconds) for data to be transfered over an
* established, open connection before the connection times out and is closed. A value of 0
* means infinity, and isn't recommended.
*
* @return The amount of time to wait (in milliseconds) for data to be transfered over an
* established, open connection before the connection times out and is closed.
*/
public int getSocketTimeout() {
return socketTimeout;
}
/**
* Sets the amount of time to wait (in milliseconds) for data to be transfered over an
* established, open connection before the connection times out and is closed. A value of 0
* means infinity, and isn't recommended.
*
* @param socketTimeout The amount of time to wait (in milliseconds) for data to be transfered
* over an established, open connection before the connection is times out and is closed.
*/
public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}
/**
* Sets the amount of time to wait (in milliseconds) for data to be transfered over an
* established, open connection before the connection times out and is closed, and returns the
* updated ClientConfiguration object so that additional method calls may be chained together.
*
* @param socketTimeout The amount of time to wait (in milliseconds) for data to be transfered
* over an established, open connection before the connection is times out and is closed.
*
* @return The updated ClientConfiguration object.
*/
public ClientConfiguration withSocketTimeout(int socketTimeout) {
setSocketTimeout(socketTimeout);
return this;
}
/**
* Returns the amount of time to wait (in milliseconds) when initially establishing a connection
* before giving up and timing out. A value of 0 means infinity, and is not recommended.
*
* @return The amount of time to wait (in milliseconds) when initially establishing a connection
* before giving up and timing out.
*/
public int getConnectionTimeout() {
return connectionTimeout;
}
/**
* Sets the amount of time to wait (in milliseconds) when initially establishing a connection
* before giving up and timing out. A value of 0 means infinity, and is not recommended.
*
* @param connectionTimeout The amount of time to wait (in milliseconds) when initially
* establishing a connection before giving up and timing out.
*/
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
/**
* Sets the amount of time to wait (in milliseconds) when initially establishing a connection
* before giving up and timing out, and returns the updated ClientConfiguration object so that
* additional method calls may be chained together.
*
* @param connectionTimeout the amount of time to wait (in milliseconds) when initially
* establishing a connection before giving up and timing out.
* @return The updated ClientConfiguration object.
*/
public ClientConfiguration withConnectionTimeout(int connectionTimeout) {
setConnectionTimeout(connectionTimeout);
return this;
}
/**
* Returns the optional size hints (in bytes) for the low level TCP send and receive buffers.
* This is an advanced option for advanced users who want to tune low level TCP parameters to
* try and squeeze out more performance.
*
* The optimal TCP buffer sizes for a particular application are highly dependent on network
* configuration and operating system configuration and capabilities. For example, most modern
* operating systems provide auto-tuning functionality for TCP buffer sizes, which can have a
* big impact on performance for TCP connections that are held open long enough for the
* auto-tuning to optimize buffer sizes.
*
* Large buffer sizes (ex: 2MB) will allow the operating system to buffer more data in memory
* without requiring the remote server to acknowledge receipt of that information, so can be
* particularly useful when the network has high latency.
*
* This is only a hint, and the operating system may choose not to honor it. When using
* this option, users should always check the operating system's configured limits and
* defaults. Most OS's have a maximum TCP buffer size limit configured, and won't let you go
* beyond that limit unless you explicitly raise the max TCP buffer size limit.
*
* There are many resources available online to help with configuring TCP buffer sizes and
* operating system specific TCP settings, including:
*
* - http://onlamp.com/pub/a/onlamp/2005/11/17/tcp_tuning.html
* - http://fasterdata.es.net/TCP-tuning/
*
*
* @return A two element array containing first the TCP send buffer size hint and then the TCP
* receive buffer size hint.
*/
public int[] getSocketBufferSizeHints() {
return new int[] {socketSendBufferSizeHint, socketReceiveBufferSizeHint};
}
/**
* Sets the optional size hints (in bytes) for the low level TCP send and receive buffers. This
* is an advanced option for advanced users who want to tune low level TCP parameters to try and
* squeeze out more performance.
*
* The optimal TCP buffer sizes for a particular application are highly dependent on network
* configuration and operating system configuration and capabilities. For example, most modern
* operating systems provide auto-tuning functionality for TCP buffer sizes, which can have a
* big impact on performance for TCP connections that are held open long enough for the
* auto-tuning to optimize buffer sizes.
*
* Large buffer sizes (ex: 2MB) will allow the operating system to buffer more data in memory
* without requiring the remote server to acknowledge receipt of that information, so can be
* particularly useful when the network has high latency.
*
* This is only a hint, and the operating system may choose not to honor it. When using
* this option, users should always check the operating system's configured limits and
* defaults. Most OS's have a maximum TCP buffer size limit configured, and won't let you go
* beyond that limit unless you explicitly raise the max TCP buffer size limit.
*
* There are many resources available online to help with configuring TCP buffer sizes and
* operating system specific TCP settings, including:
*
* @param socketSendBufferSizeHint The size hint (in bytes) for the low level TCP send buffer.
* @param socketReceiveBufferSizeHint The size hint (in bytes) for the low level TCP receive
* buffer.
*/
public void setSocketBufferSizeHints(int socketSendBufferSizeHint,
int socketReceiveBufferSizeHint) {
this.socketSendBufferSizeHint = socketSendBufferSizeHint;
this.socketReceiveBufferSizeHint = socketReceiveBufferSizeHint;
}
/**
* Sets the optional size hints (in bytes) for the low level TCP send and receive buffers, and
* returns the updated ClientConfiguration object so that additional method calls may be chained
* together.
*
* This is an advanced option for advanced users who want to tune low level TCP parameters to
* try and squeeze out more performance.
*
* The optimal TCP buffer sizes for a particular application are highly dependent on network
* configuration and operating system configuration and capabilities. For example, most modern
* operating systems provide auto-tuning functionality for TCP buffer sizes, which can have a
* big impact on performance for TCP connections that are held open long enough for the
* auto-tuning to optimize buffer sizes.
*
* Large buffer sizes (ex: 2MB) will allow the operating system to buffer more data in memory
* without requiring the remote server to acknowledge receipt of that information, so can be
* particularly useful when the network has high latency.
*
* This is only a hint, and the operating system may choose not to honor it. When using
* this option, users should always check the operating system's configured limits and
* defaults. Most OS's have a maximum TCP buffer size limit configured, and won't let you go
* beyond that limit unless you explicitly raise the max TCP buffer size limit.
*
* There are many resources available online to help with configuring TCP buffer sizes and
* operating system specific TCP settings, including:
*
* - http://onlamp.com/pub/a/onlamp/2005/11/17/tcp_tuning.html
* - http://fasterdata.es.net/TCP-tuning/
*
*
* @param socketSendBufferSizeHint The size hint (in bytes) for the low level TCP send buffer.
* @param socketReceiveBufferSizeHint The size hint (in bytes) for the low level TCP receive
* buffer.
* @return The updated ClientConfiguration object.
*/
public ClientConfiguration withSocketBufferSizeHints(int socketSendBufferSizeHint,
int socketReceiveBufferSizeHint) {
setSocketBufferSizeHints(socketSendBufferSizeHint, socketReceiveBufferSizeHint);
return this;
}
}