| | |
| | | return t; |
| | | }); |
| | | |
| | | /** |
| | | * @param wsUrl Gate WebSocket 地址(由 {@link GateConfig#getWsUrl()} 提供) |
| | | */ |
| | | public GateKlineWebSocketClient(String wsUrl) { |
| | | this.wsUrl = wsUrl; |
| | | } |
| | | |
| | | /** |
| | | * 注册频道处理器。需在 init() 前调用。 |
| | | * |
| | | * @param handler 实现了 {@link GateChannelHandler} 接口的频道处理器 |
| | | */ |
| | | public void addChannelHandler(GateChannelHandler handler) { |
| | | channelHandlers.add(handler); |
| | | } |
| | | |
| | | /** |
| | | * 初始化:建立 WebSocket 连接 → 启动心跳。 |
| | | * 初始化:建立 WebSocket 连接 → 启动心跳检测。 |
| | | * 使用 {@code AtomicBoolean} 防重入,同一实例只允许初始化一次。 |
| | | */ |
| | | public void init() { |
| | | if (!isInitialized.compareAndSet(false, true)) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 销毁:取消订阅 → 关闭连接 → 关闭线程池。 |
| | | * <p>注意:先 closeBlocking 再 shutdown sharedExecutor, |
| | | * 避免 onClose 回调中的 reconnectWithBackoff 访问已关闭的线程池。 |
| | | * 销毁:取消所有频道订阅 → 关闭 WebSocket 连接 → 关闭线程池。 |
| | | * |
| | | * <h3>执行顺序</h3> |
| | | * 先取消订阅(等待 500ms 确保发送完成),再 closeBlocking 关闭连接, |
| | | * 最后 shutdown 线程池。先关连接再关线程池,避免 onClose 回调中的重连任务访问已关闭的线程池。 |
| | | */ |
| | | public void destroy() { |
| | | log.info("[WS] 开始销毁..."); |
| | |
| | | |
| | | /** |
| | | * 建立 WebSocket 连接。使用 SSLContext 配置 TLS 协议。 |
| | | * 连接成功后依次订阅所有已注册的频道处理器。 |
| | | * |
| | | * <h3>连接成功回调</h3> |
| | | * <ol> |
| | | * <li>设置 isConnected=true,isConnecting=false</li> |
| | | * <li>重置心跳计时器</li> |
| | | * <li>依次订阅所有已注册的频道处理器</li> |
| | | * <li>发送首次 ping</li> |
| | | * </ol> |
| | | * |
| | | * <h3>连接关闭回调</h3> |
| | | * 设置断连状态 → 取消心跳超时 → 异步触发指数退避重连(最多3次)。 |
| | | * |
| | | * <h3>线程安全</h3> |
| | | * 使用 {@code AtomicBoolean.isConnecting} 防止并发重连。 |
| | | */ |
| | | private void connect() { |
| | | if (isConnecting.get() || !isConnecting.compareAndSet(false, true)) { |
| | |
| | | |
| | | // ---- heartbeat ---- |
| | | |
| | | /** |
| | | * 启动心跳检测器。 |
| | | * 使用单线程 ScheduledExecutor,每 25 秒检查一次心跳超时。 |
| | | */ |
| | | private void startHeartbeat() { |
| | | if (heartbeatExecutor != null && !heartbeatExecutor.isTerminated()) heartbeatExecutor.shutdownNow(); |
| | | heartbeatExecutor = Executors.newSingleThreadScheduledExecutor(r -> { Thread t = new Thread(r, "gate-ws-heartbeat"); t.setDaemon(true); return t; }); |
| | | heartbeatExecutor.scheduleWithFixedDelay(this::checkHeartbeatTimeout, 25, 25, TimeUnit.SECONDS); |
| | | } |
| | | |
| | | /** |
| | | * 重置心跳计时器:取消旧超时任务,提交新的 10 秒超时检测。 |
| | | */ |
| | | private synchronized void resetHeartbeatTimer() { |
| | | cancelPongTimeout(); |
| | | if (heartbeatExecutor != null && !heartbeatExecutor.isShutdown()) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查心跳超时:如果距离上次收到消息超过 10 秒,主动发送 futures.ping。 |
| | | */ |
| | | private void checkHeartbeatTimeout() { |
| | | if (!isConnected.get()) return; |
| | | if (System.currentTimeMillis() - lastMessageTime.get() >= HEARTBEAT_TIMEOUT * 1000L) sendPing(); |
| | | } |
| | | |
| | | /** |
| | | * 发送应用层 futures.ping 消息。 |
| | | */ |
| | | private void sendPing() { |
| | | try { |
| | | if (webSocketClient != null && webSocketClient.isOpen()) { |
| | |
| | | } catch (Exception e) { log.warn("[WS] 发送 ping 失败", e); } |
| | | } |
| | | |
| | | /** |
| | | * 取消心跳超时检测任务。 |
| | | */ |
| | | private synchronized void cancelPongTimeout() { |
| | | if (pongTimeoutFuture != null && !pongTimeoutFuture.isDone()) pongTimeoutFuture.cancel(true); |
| | | } |
| | | |
| | | // ---- reconnect ---- |
| | | |
| | | /** |
| | | * 指数退避重连。初始延迟 5 秒,每次翻倍,最多重试 3 次。 |
| | | * |
| | | * @throws InterruptedException 线程被中断 |
| | | */ |
| | | private void reconnectWithBackoff() throws InterruptedException { |
| | | int attempt = 0, maxAttempts = 3; |
| | | long delayMs = 5000; |
| | |
| | | log.error("[WS] 超过最大重试次数({}),放弃重连", maxAttempts); |
| | | } |
| | | |
| | | /** |
| | | * 优雅关闭线程池:先 shutdown,等待 5 秒,超时则 shutdownNow 强制中断。 |
| | | * |
| | | * @param executor 需要关闭的线程池 |
| | | */ |
| | | private void shutdownExecutorGracefully(ExecutorService executor) { |
| | | if (executor == null || executor.isTerminated()) return; |
| | | try { executor.shutdown(); if (!executor.awaitTermination(5, TimeUnit.SECONDS)) executor.shutdownNow(); } |