From ee65f95fe39e02e2cf445bde33fc12f9a1b33b15 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 24 Dec 2019 17:16:41 -0800 Subject: [PATCH] (cobra client) send a websocket ping every 30s to keep the connection opened --- docs/CHANGELOG.md | 5 ++++- ixcobra/ixcobra/IXCobraConnection.cpp | 6 ++++++ ixcobra/ixcobra/IXCobraConnection.h | 6 +++++- ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp | 4 ++++ ixwebsocket/IXWebSocketVersion.h | 2 +- ws/ws_cobra_publish.cpp | 4 ++++ ws/ws_cobra_subscribe.cpp | 4 ++++ ws/ws_cobra_to_sentry.cpp | 4 ++++ ws/ws_cobra_to_statsd.cpp | 4 ++++ 9 files changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 679d0f40..155d2cf7 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,13 +1,16 @@ # Changelog All changes to this project will be documented in this file. +## [7.6.5] - 2019-12-24 + +(cobra client) send a websocket ping every 30s to keep the connection opened + ## [7.6.4] - 2019-12-22 (client) error handling, quote url in error case when failing to parse one (ws) ws_cobra_publish: register callbacks before connecting (doc) mention mbedtls in supported ssl server backend - ## [7.6.3] - 2019-12-20 (tls) add a simple description of the TLS configuration routine for debugging diff --git a/ixcobra/ixcobra/IXCobraConnection.cpp b/ixcobra/ixcobra/IXCobraConnection.cpp index 9553aa9f..24058341 100644 --- a/ixcobra/ixcobra/IXCobraConnection.cpp +++ b/ixcobra/ixcobra/IXCobraConnection.cpp @@ -24,6 +24,7 @@ namespace ix PublishTrackerCallback CobraConnection::_publishTrackerCallback = nullptr; constexpr size_t CobraConnection::kQueueMaxSize; constexpr CobraConnection::MsgId CobraConnection::kInvalidMsgId; + constexpr int CobraConnection::kPingIntervalSecs; CobraConnection::CobraConnection() : _webSocket(new WebSocket()), @@ -228,6 +229,10 @@ namespace ix ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; invokeErrorCallback(ss.str(), std::string()); } + else if (msg->type == ix::WebSocketMessageType::Pong) + { + invokeEventCallback(ix::CobraConnection_EventType_Pong); + } }); } @@ -260,6 +265,7 @@ namespace ix _webSocket->setUrl(url); _webSocket->setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions); _webSocket->setTLSOptions(socketTLSOptions); + _webSocket->setPingInterval(kPingIntervalSecs); } // diff --git a/ixcobra/ixcobra/IXCobraConnection.h b/ixcobra/ixcobra/IXCobraConnection.h index 6e57ad0d..e380215c 100644 --- a/ixcobra/ixcobra/IXCobraConnection.h +++ b/ixcobra/ixcobra/IXCobraConnection.h @@ -30,7 +30,8 @@ namespace ix CobraConnection_EventType_Closed = 3, CobraConnection_EventType_Subscribed = 4, CobraConnection_EventType_UnSubscribed = 5, - CobraConnection_EventType_Published = 6 + CobraConnection_EventType_Published = 6, + CobraConnection_EventType_Pong = 7 }; enum CobraConnectionPublishMode @@ -215,6 +216,9 @@ namespace ix // Each pdu sent should have an incremental unique id std::atomic _id; + + // Frequency at which we send a websocket ping to the backing cobra connection + static constexpr int kPingIntervalSecs = 30; }; } // namespace ix diff --git a/ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp b/ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp index 8ae3c2c9..22a03fc7 100644 --- a/ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp +++ b/ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp @@ -65,6 +65,10 @@ namespace ix { ss << "Published message " << msgId << " acked"; } + else if (eventType == ix::CobraConnection_EventType_Pong) + { + ss << "Received websocket pong"; + } ix::IXCoreLogger::Log(ss.str().c_str()); }); diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index 453b56c0..b39f39b8 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "7.6.4" +#define IX_WEBSOCKET_VERSION "7.6.5" diff --git a/ws/ws_cobra_publish.cpp b/ws/ws_cobra_publish.cpp index 2daf6fc5..b99145d8 100644 --- a/ws/ws_cobra_publish.cpp +++ b/ws/ws_cobra_publish.cpp @@ -91,6 +91,10 @@ namespace ix spdlog::info("Published message id {} acked", msgId); messageAcked = true; } + else if (eventType == ix::CobraConnection_EventType_Pong) + { + spdlog::info("Received websocket pong"); + } }); conn.connect(); diff --git a/ws/ws_cobra_subscribe.cpp b/ws/ws_cobra_subscribe.cpp index 18c97543..0e6c9943 100644 --- a/ws/ws_cobra_subscribe.cpp +++ b/ws/ws_cobra_subscribe.cpp @@ -100,6 +100,10 @@ namespace ix { spdlog::error("Published message hacked: {}", msgId); } + else if (eventType == ix::CobraConnection_EventType_Pong) + { + spdlog::info("Received websocket pong"); + } }); while (true) diff --git a/ws/ws_cobra_to_sentry.cpp b/ws/ws_cobra_to_sentry.cpp index 3befaa4a..42618eb7 100644 --- a/ws/ws_cobra_to_sentry.cpp +++ b/ws/ws_cobra_to_sentry.cpp @@ -245,6 +245,10 @@ namespace ix { spdlog::error("Published message hacked: {}", msgId); } + else if (eventType == ix::CobraConnection_EventType_Pong) + { + spdlog::info("Received websocket pong"); + } }); while (true) diff --git a/ws/ws_cobra_to_statsd.cpp b/ws/ws_cobra_to_statsd.cpp index fb5327bc..0015b861 100644 --- a/ws/ws_cobra_to_statsd.cpp +++ b/ws/ws_cobra_to_statsd.cpp @@ -160,6 +160,10 @@ namespace ix { spdlog::error("Published message hacked: {}", msgId); } + else if (eventType == ix::CobraConnection_EventType_Pong) + { + spdlog::info("Received websocket pong"); + } }); while (true)