Skip to content

Commit

Permalink
fix bug that server has 1 extra dead conn
Browse files Browse the repository at this point in the history
change keepalive req interval from 5 to 2 sec. that is if no response in 8 sec, it will reconnect
  • Loading branch information
nmq committed Mar 30, 2018
1 parent 3f62839 commit 7168a15
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions callbacks/INetConnKeepAlive.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class INetConnKeepAlive {
virtual int SendNetConnRst(const ConnInfo &src, IntKeyType connKey) = 0;

virtual INetConnKeepAlive *GetIKeepAlive() const = 0;

virtual int RemoveRequest(IntKeyType connKey) = 0;
};

using IntConnKeyType = uint32_t;
Expand Down
4 changes: 3 additions & 1 deletion conn/FakeTcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ void FakeTcp::SetAckISN(uint32_t isn) {
mInfo.UpdateAck(isn);
}

// it is likely that router doesn't send rst/fin even it has no record for this connection
// Because server will not send request, so it doesn't know if this conn is dead.
bool FakeTcp::Alive() {
return mAlive;
return mAlive && IConn::Alive();
}

ConnInfo *FakeTcp::GetInfo() {
Expand Down
4 changes: 0 additions & 4 deletions conn/FakeUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ ConnInfo *FakeUdp::GetInfo() {
bool FakeUdp::IsUdp() {
return true;
}

bool FakeUdp::Alive() {
return true;
}
2 changes: 0 additions & 2 deletions conn/FakeUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class FakeUdp : public INetConn {

inline bool IsUdp() override;

bool Alive() override;

private:
ConnInfo mInfo;
};
Expand Down
9 changes: 7 additions & 2 deletions conn/IAppGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ int IAppGroup::NetConnKeepAliveHelper::OnSendRequest(uint8_t cmd, ssize_t nread,
}

int IAppGroup::NetConnKeepAliveHelper::OnRecvResponse(INetConn::IntKeyType connKey) {
return mReqMap.erase(connKey);
return RemoveRequest(connKey);
}

INetConn *IAppGroup::NetConnKeepAliveHelper::ConnOfIntKey(INetConn::IntKeyType connKey) {
Expand Down Expand Up @@ -251,8 +251,9 @@ void IAppGroup::NetConnKeepAliveHelper::onFlush() {
auto aCopy = mReqMap;
for (auto &e: aCopy) {
if (e.second >= MAX_RETRY) { // keep alive timeout
LOGE << "keepalive timeout";
mAppGroup->onNetconnDead(e.first);
mReqMap.erase(e.first);
RemoveRequest(e.first);
}
}
}
Expand All @@ -261,6 +262,10 @@ INetConnKeepAlive *IAppGroup::NetConnKeepAliveHelper::GetIKeepAlive() const {
return mKeepAlive;
}

int IAppGroup::NetConnKeepAliveHelper::RemoveRequest(INetConnKeepAlive::IntKeyType connKey) {
return mReqMap.erase(connKey);
}

IAppGroup::ResetHelper::ResetHelper(IAppGroup *appGroup) {
mAppGroup = appGroup;
mReset = new ConnReset(this);
Expand Down
4 changes: 3 additions & 1 deletion conn/IAppGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class IAppGroup : public IGroup, public ITcpObserver {

INetConnKeepAlive *GetIKeepAlive() const override;

int RemoveRequest(IntKeyType connKey) override;

private:
void onFlush();

Expand All @@ -89,7 +91,7 @@ class IAppGroup : public IGroup, public ITcpObserver {

private:
const int MAX_RETRY = 3;
const uint32_t FLUSH_INTERVAL = 5000; // every 5sec
const uint32_t FLUSH_INTERVAL = 2000; // every 2sec
const uint32_t FIRST_FLUSH_DELAY = 5000; // on app start
IAppGroup *mAppGroup = nullptr;
uv_timer_t *mFlushTimer = nullptr;
Expand Down

0 comments on commit 7168a15

Please sign in to comment.