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