Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add http proxy support #63

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
8 changes: 7 additions & 1 deletion src/main/java/com/huobi/constant/HuobiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import com.huobi.constant.enums.ExchangeEnum;

import java.net.Proxy;

@Builder
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -22,7 +24,7 @@ public class HuobiOptions implements Options {
private String apiKey;

private String secretKey;

private Proxy proxy;
@Builder.Default
private boolean websocketAutoConnect = true;

Expand Down Expand Up @@ -56,4 +58,8 @@ public boolean isWebSocketAutoConnect() {
return this.websocketAutoConnect;
}

@Override
public Proxy getProxy(){
return this.proxy;
};
}
4 changes: 4 additions & 0 deletions src/main/java/com/huobi/constant/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.huobi.constant.enums.ExchangeEnum;

import java.net.Proxy;

public interface Options {

String getApiKey();
Expand All @@ -16,4 +18,6 @@ public interface Options {

boolean isWebSocketAutoConnect();

Proxy getProxy();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Options getOptions() {

public HuobiRestConnection(Options options) {
this.options = options;
ConnectionFactory.initFactory(options.getProxy());
try {
this.host = new URL(this.options.getRestHost()).getHost();
} catch (MalformedURLException e) {
Expand Down
171 changes: 94 additions & 77 deletions src/main/java/com/huobi/utils/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.huobi.utils;

import java.io.IOException;
import java.net.Proxy;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
Expand All @@ -24,94 +23,112 @@

public class ConnectionFactory {

private static Boolean LATENCY_DEBUG_SWATCH = Boolean.FALSE;
private static Boolean LATENCY_DEBUG_SWATCH = Boolean.FALSE;

private static LinkedBlockingQueue<NetworkLatency> LATENCY_DEBUG_QUEUE = new LinkedBlockingQueue<>();
private static LinkedBlockingQueue<NetworkLatency> LATENCY_DEBUG_QUEUE = new LinkedBlockingQueue<>();

private static ConnectionPool connectionPool =
new ConnectionPool(20, 300, TimeUnit.SECONDS);
private static ConnectionPool connectionPool =
new ConnectionPool(20, 300, TimeUnit.SECONDS);

private static final OkHttpClient client = new OkHttpClient.Builder()
.followSslRedirects(false)
.followRedirects(false)
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.connectionPool(connectionPool)
.addNetworkInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = chain.request();

Long startNano = System.nanoTime();

Response response = chain.proceed(request);
private ConnectionFactory(){}
public static void initFactory(Proxy proxy) {
init(proxy);
}

Long endNano = System.nanoTime();

if (LATENCY_DEBUG_SWATCH) {
LATENCY_DEBUG_QUEUE.add(new NetworkLatency(request.url().url().getPath(), startNano, endNano));
}
private static OkHttpClient client;
@Getter
public static OkHttpClient.Builder builder;
// static {
// }

return response;
private static void init(Proxy proxy) {
builder = new OkHttpClient.Builder();
if (!Objects.isNull(proxy)) {
builder.proxy(proxy);
}
})
.build();

private static final Logger log = LoggerFactory.getLogger(ConnectionFactory.class);

public static String execute(Request request) {

Response response = null;
String str = null;
try {
log.debug("[Request URL]{}", request.url());
response = client.newCall(request).execute();
if (response.code() != 200) {
throw new SDKException(SDKException.EXEC_ERROR, "[Execute] Response Status Error : " + response.code() + " message:" + response.message());
}
if (response != null && response.body() != null) {
str = response.body().string();
response.close();
} else {
throw new SDKException(SDKException.ENV_ERROR, "[Execute] Cannot get the response from server");
}
log.debug("[Response]{}", str);
return str;
} catch (IOException e) {
e.printStackTrace();
throw new SDKException(SDKException.RUNTIME_ERROR, "[Execute] Cannot get the response from server");
client = builder
.followSslRedirects(false)
.followRedirects(false)
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.connectionPool(connectionPool)
.addNetworkInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = chain.request();

Long startNano = System.nanoTime();

Response response = chain.proceed(request);

Long endNano = System.nanoTime();

if (LATENCY_DEBUG_SWATCH) {
LATENCY_DEBUG_QUEUE.add(new NetworkLatency(request.url().url().getPath(), startNano, endNano));
}

return response;
}
})
.build();
}

}
private static final Logger log = LoggerFactory.getLogger(ConnectionFactory.class);

public static String execute(Request request) {

Response response = null;
String str = null;
try {
log.debug("[Request URL]{}", request.url());
response = client.newCall(request).execute();
if (response.code() != 200) {
throw new SDKException(SDKException.EXEC_ERROR, "[Execute] Response Status Error : " + response.code() + " message:" + response.message());
}
if (response != null && response.body() != null) {
str = response.body().string();
response.close();
} else {
throw new SDKException(SDKException.ENV_ERROR, "[Execute] Cannot get the response from server");
}
log.debug("[Response]{}", str);
return str;
} catch (IOException e) {
e.printStackTrace();
throw new SDKException(SDKException.RUNTIME_ERROR, "[Execute] Cannot get the response from server");
}

public static WebSocket createWebSocket(Request request, WebSocketListener listener) {
return client.newWebSocket(request, listener);
}
}

public static void setLatencyDebug() {
LATENCY_DEBUG_SWATCH = Boolean.TRUE;
}
public static WebSocket createWebSocket(Request request, WebSocketListener listener) {
return client.newWebSocket(request, listener);
}

public static LinkedBlockingQueue<NetworkLatency> getLatencyDebugQueue() {
return LATENCY_DEBUG_QUEUE;
}
public static void setLatencyDebug() {
LATENCY_DEBUG_SWATCH = Boolean.TRUE;
}

public static void clearLatencyDebugQueue() {
LATENCY_DEBUG_QUEUE.clear();
}
public static LinkedBlockingQueue<NetworkLatency> getLatencyDebugQueue() {
return LATENCY_DEBUG_QUEUE;
}

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class NetworkLatency {
public static void clearLatencyDebugQueue() {
LATENCY_DEBUG_QUEUE.clear();
}

private String path;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class NetworkLatency {

private Long startNanoTime;
private String path;

private Long endNanoTime;
}
private Long startNanoTime;

private Long endNanoTime;
}
}
Empty file.
Empty file.
Loading