Skip to content

Commit bb3256b

Browse files
author
nmq
committed
delay sending keepalive when startup
1 parent d2a0df8 commit bb3256b

File tree

8 files changed

+40
-6
lines changed

8 files changed

+40
-6
lines changed

bean/RConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int RConfig::Parse(bool is_server, int argc, const char *const *argv) {
7070
args::ValueFlag<uint16_t> cap_timeout(opt, "", "pcap timeout(ms). > 0 and <= 50", {"cap_timeout"});
7171

7272
args::ValueFlag<int> keepAlive(opt, "keepalive interval",
73-
"interval used to send keepalive request. default 2s for client, 4s for server.",
73+
"interval used to send keepalive request. default 4s.",
7474
{"keepalive"});
7575
try {
7676
parser.ParseCLI(argc, argv);

bean/RConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct RConfig {
5050
#ifdef RSOCK_IS_SERVER_
5151
int keepAliveIntervalSec = 4; // default 4s, 3 times
5252
#else
53-
int keepAliveIntervalSec = 2; // default 2s, 3 times
53+
int keepAliveIntervalSec = 4; // default 2s, 3 times
5454
#endif
5555

5656
const std::string version = RSOCK_BUILD_TIME;

callbacks/NetConnKeepAlive.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void NetConnKeepAlive::OnFlush(uint64_t timestamp) {
119119
// wait until the conn is not new: it has sent some bytes before
120120
// This check mainly solve the bug: if keepalive packet reaches server before data does, the server will send rst,
121121
// and this conn will be closed and no more connected.
122-
if (!conn->IsNew()) {
122+
if (canSendRequest(conn, timestamp)) {
123123
auto it = mReqMap.find(conn->IntKey());
124124
if (it != mReqMap.end()) { // has record, increment number of trials
125125
it->second++;
@@ -164,3 +164,15 @@ int NetConnKeepAlive::RemoveAllRequest() {
164164
mReqMap.clear();
165165
return 0;
166166
}
167+
168+
bool NetConnKeepAlive::canSendRequest(INetConn *conn, uint64_t timestampMs) const {
169+
if (conn && !conn->IsNew()) {
170+
if (timestampMs > conn->EstablishedTimeStampMs()) { // use compare first, in case overflow
171+
int df = timestampMs - conn->EstablishedTimeStampMs();
172+
if (df >= REQUEST_DELAY) {
173+
return true;
174+
}
175+
}
176+
}
177+
return false;
178+
}

callbacks/NetConnKeepAlive.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ class NetConnKeepAlive : public INetConnKeepAlive, public ITimerObserver {
5151
// in case some invalid request is still in the pool. remove them
5252
void removeInvalidRequest();
5353

54+
bool canSendRequest(INetConn *conn, uint64_t timestampMs) const;
55+
5456
private:
5557
const int MAX_RETRY = 3;
5658
const uint32_t FLUSH_INTERVAL = 0; // shoud be same with RConfig.keepAlive
5759
KeepAliveRouteObserver *mRouteObserver = nullptr;
5860
IAppGroup *mAppGroup = nullptr;
5961
std::map<IntKeyType, int> mReqMap;
6062
IReset *mReset = nullptr;
63+
static const int REQUEST_DELAY = 15000; // 15s
6164
};
6265

6366

conn/INetConn.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include "../util/rsutil.h"
1010
#include "../src/util/KeyGenerator.h"
1111

12-
INetConn::INetConn(IntKeyType key) : IConn(KeyGenerator::StrForIntKey(key)), mIntKey(key) {
12+
INetConn::INetConn(IntKeyType key) : IConn(KeyGenerator::StrForIntKey(key)), mIntKey(key),
13+
mEstablishedTimeStampMs(rsk_now_ms()) {
1314
}
1415

1516
//INetConn::INetConn(const std::string &key) : IConn(key) {
@@ -84,6 +85,10 @@ bool INetConn::IsNew() const {
8485
}
8586

8687
bool INetConn::Alive() {
87-
return IConn::Alive() && mAlive;
88+
return mAlive;
89+
}
90+
91+
uint64_t INetConn::EstablishedTimeStampMs() const {
92+
return mEstablishedTimeStampMs;
8893
}
8994

conn/INetConn.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class INetConn : public IConn {
4141

4242
static const std::string BuildPrintableStr(const ConnInfo &info, IntKeyType key);
4343

44+
/*
45+
* return when is the connection established
46+
*/
47+
uint64_t EstablishedTimeStampMs() const;
48+
4449
bool IsNew() const;
4550

4651
IntKeyType IntKey() const;
@@ -54,6 +59,7 @@ class INetConn : public IConn {
5459
bool mNew = true;
5560
bool mAlive = true;
5661
int mErrCode = ERR_NO_ERR;
62+
const uint64_t mEstablishedTimeStampMs = 0xffffffff;
5763
};
5864

5965
#endif //RSOCK_INETCONN_H

net/INetManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ IBtmConn *INetManager::BindUdp(const ConnInfo &info) {
3636
}
3737

3838
void INetManager::closeTcp(uv_tcp_t *tcp) {
39-
uv_close((uv_handle_t *) tcp, close_cb);
39+
if (tcp) {
40+
uv_close((uv_handle_t *) tcp, close_cb);
41+
}
4042
}
4143

4244
bool INetManager::Wait2GetInfo(TcpInfo &info) {

server/ServerNetManager.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ uv_tcp_t *ServerNetManager::GetTcp(TcpInfo &info) {
5454
auto it = mPool.find(info);
5555
if (it != mPool.end()) {
5656
auto tcp = it->second.conn;
57+
if (!tcp) {
58+
LOGD << "tcp temporarily null, just return";
59+
return nullptr;
60+
}
5761
mPool.erase(it);
5862
bool ok = mTcpAckPool->Wait2TransferInfo(info, BLOCK_WAIT_MS);
5963
if (ok) {
@@ -62,6 +66,8 @@ uv_tcp_t *ServerNetManager::GetTcp(TcpInfo &info) {
6266
LOGE << "AckPool has no record in pool: " << info.ToStr();
6367
closeTcp(tcp);
6468
}
69+
} else {
70+
LOGD << "TcpPool has no info: " << info.ToStr();
6571
}
6672
int n = mTcpAckPool->RemoveInfo(info);
6773
LOGD << "Remove the tcpInfo " << ((n > 0) ? "succeeded" : "failed");

0 commit comments

Comments
 (0)