Commit ce0be326 authored by gejun's avatar gejun

shorten the delay that -defer_close_second takes effect in worse cases from 2 seconds to 1 second

parent 57cc25d1
......@@ -326,15 +326,13 @@ void SocketMap::List(std::vector<butil::EndPoint>* pts) {
}
}
void SocketMap::ListOrphans(int defer_seconds,
std::vector<butil::EndPoint>* out) {
void SocketMap::ListOrphans(int64_t defer_us, std::vector<butil::EndPoint>* out) {
out->clear();
int64_t now = butil::cpuwide_time_s();
const int64_t now = butil::cpuwide_time_us();
BAIDU_SCOPED_LOCK(_mutex);
for (Map::iterator it = _map.begin(); it != _map.end(); ++it) {
SingleConnection& sc = it->second;
if (sc.ref_count == 0
&& now - sc.no_ref_us / 1000000L > defer_seconds) {
if (sc.ref_count == 0 && now - sc.no_ref_us >= defer_us) {
out->push_back(it->first);
}
}
......@@ -378,7 +376,7 @@ void SocketMap::WatchConnections() {
const int defer_seconds = _options.defer_close_second_dynamic ?
*_options.defer_close_second_dynamic :
_options.defer_close_second;
ListOrphans(defer_seconds, &orphan_sockets);
ListOrphans(defer_seconds * 1000000L, &orphan_sockets);
for (size_t i = 0; i < orphan_sockets.size(); ++i) {
RemoveInternal(orphan_sockets[i], (SocketId)-1, true);
}
......
......@@ -97,7 +97,7 @@ public:
private:
void RemoveInternal(const butil::EndPoint& pt, SocketId id,
bool remove_orphan);
void ListOrphans(int defer_seconds, std::vector<butil::EndPoint>* out);
void ListOrphans(int64_t defer_us, std::vector<butil::EndPoint>* out);
void WatchConnections();
static void* RunWatchConnections(void*);
void Print(std::ostream& os);
......
......@@ -56,7 +56,7 @@ TEST_F(SocketMapTest, idle_timeout) {
brpc::SocketId id;
// Socket still exists since it has not reached timeout yet
ASSERT_EQ(0, brpc::SocketMapFind(g_endpoint, &id));
sleep(TIMEOUT + 1);
usleep(TIMEOUT * 1000000L + 1100000L);
// Socket should be removed after timeout
ASSERT_EQ(-1, brpc::SocketMapFind(g_endpoint, &id));
......@@ -66,7 +66,7 @@ TEST_F(SocketMapTest, idle_timeout) {
ASSERT_EQ(0, brpc::SocketMapFind(g_endpoint, &id));
// Change `FLAGS_idle_timeout_second' to 0 to disable checking
brpc::FLAGS_defer_close_second = 0;
sleep(1);
usleep(1100000L);
// And then Socket should be removed
ASSERT_EQ(-1, brpc::SocketMapFind(g_endpoint, &id));
......@@ -82,7 +82,7 @@ TEST_F(SocketMapTest, idle_timeout) {
id = ptr->id();
ptr->ReturnToPool();
ptr.reset(NULL);
sleep(TIMEOUT + 1);
usleep(TIMEOUT * 1000000L + 1100000L);
// Pooled connection should be `ReleaseAdditionalReference',
// which destroyed the Socket. As a result `GetSocketFromPool'
// should return a new one
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment