1
zyy
2 days ago 4fefff17528a878d345ff3311c297a66a671b8d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.yami.trading.huobi.websocket.utils;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
import com.yami.trading.common.nezha.NezhaConstants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.yami.trading.huobi.websocket.exception.SDKException;
 
 
public class ConnectionFactory {
 
  private static Boolean LATENCY_DEBUG_SWATCH = Boolean.FALSE;
 
  private static LinkedBlockingQueue<NetworkLatency> LATENCY_DEBUG_QUEUE = new LinkedBlockingQueue<>();
 
  private static ConnectionPool connectionPool =
      new ConnectionPool(20, 300, TimeUnit.SECONDS);
 
  private static final OkHttpClient client;
 
  static {
      OkHttpClient.Builder builder = 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);
 
                  Long endNano = System.nanoTime();
 
                  if (LATENCY_DEBUG_SWATCH) {
                    LATENCY_DEBUG_QUEUE.add(new NetworkLatency(request.url().url().getPath(), startNano, endNano));
                  }
 
                  return response;
                }
              });
 
      if (NezhaConstants.LOCAL_PROXY) {
        builder.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 10808)));
      }
      client = builder.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 LinkedBlockingQueue<NetworkLatency> getLatencyDebugQueue() {
    return LATENCY_DEBUG_QUEUE;
  }
 
  public static void clearLatencyDebugQueue() {
    LATENCY_DEBUG_QUEUE.clear();
  }
 
  @Data
  @Builder
  @AllArgsConstructor
  @NoArgsConstructor
  public static class NetworkLatency {
 
    private String path;
 
    private Long startNanoTime;
 
    private Long endNanoTime;
  }
}