OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 | 600 |
601 bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const { | 601 bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const { |
602 return base::ContainsKey(group_map_, group_name); | 602 return base::ContainsKey(group_map_, group_name); |
603 } | 603 } |
604 | 604 |
605 void ClientSocketPoolBaseHelper::CloseIdleSockets() { | 605 void ClientSocketPoolBaseHelper::CloseIdleSockets() { |
606 CleanupIdleSockets(true); | 606 CleanupIdleSockets(true); |
607 DCHECK_EQ(0, idle_socket_count_); | 607 DCHECK_EQ(0, idle_socket_count_); |
608 } | 608 } |
609 | 609 |
610 void ClientSocketPoolBaseHelper::CloseIdleSocketsInGroup( | |
611 const std::string& group_name) { | |
612 if (idle_socket_count_ == 0) | |
613 return; | |
614 GroupMap::iterator it = group_map_.find(group_name); | |
615 if (it == group_map_.end()) | |
616 return; | |
617 base::TimeTicks now = base::TimeTicks::Now(); | |
davidben
2017/03/02 22:00:51
Super-nitpicky nit: You can probably just pass tha
xunjieli
2017/03/02 23:04:00
Done.
| |
618 CleanupIdleSocketsInGroup(true, it->second, now); | |
619 if (it->second->IsEmpty()) | |
620 RemoveGroup(it); | |
621 } | |
622 | |
610 int ClientSocketPoolBaseHelper::IdleSocketCountInGroup( | 623 int ClientSocketPoolBaseHelper::IdleSocketCountInGroup( |
611 const std::string& group_name) const { | 624 const std::string& group_name) const { |
612 GroupMap::const_iterator i = group_map_.find(group_name); | 625 GroupMap::const_iterator i = group_map_.find(group_name); |
613 CHECK(i != group_map_.end()); | 626 CHECK(i != group_map_.end()); |
614 | 627 |
615 return i->second->idle_sockets().size(); | 628 return i->second->idle_sockets().size(); |
616 } | 629 } |
617 | 630 |
618 LoadState ClientSocketPoolBaseHelper::GetLoadState( | 631 LoadState ClientSocketPoolBaseHelper::GetLoadState( |
619 const std::string& group_name, | 632 const std::string& group_name, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
754 if (idle_socket_count_ == 0) | 767 if (idle_socket_count_ == 0) |
755 return; | 768 return; |
756 | 769 |
757 // Current time value. Retrieving it once at the function start rather than | 770 // Current time value. Retrieving it once at the function start rather than |
758 // inside the inner loop, since it shouldn't change by any meaningful amount. | 771 // inside the inner loop, since it shouldn't change by any meaningful amount. |
759 base::TimeTicks now = base::TimeTicks::Now(); | 772 base::TimeTicks now = base::TimeTicks::Now(); |
760 | 773 |
761 GroupMap::iterator i = group_map_.begin(); | 774 GroupMap::iterator i = group_map_.begin(); |
762 while (i != group_map_.end()) { | 775 while (i != group_map_.end()) { |
763 Group* group = i->second; | 776 Group* group = i->second; |
764 | 777 CleanupIdleSocketsInGroup(force, group, now); |
765 auto idle_socket_it = group->mutable_idle_sockets()->begin(); | |
766 while (idle_socket_it != group->idle_sockets().end()) { | |
767 base::TimeDelta timeout = idle_socket_it->socket->WasEverUsed() | |
768 ? used_idle_socket_timeout_ | |
769 : unused_idle_socket_timeout_; | |
770 bool timed_out = (now - idle_socket_it->start_time) >= timeout; | |
771 bool should_clean_up = force || timed_out || !idle_socket_it->IsUsable(); | |
772 if (should_clean_up) { | |
773 if (force) { | |
774 RecordIdleSocketFate(IDLE_SOCKET_FATE_CLEAN_UP_FORCED); | |
775 } else if (timed_out) { | |
776 RecordIdleSocketFate( | |
777 idle_socket_it->socket->WasEverUsed() | |
778 ? IDLE_SOCKET_FATE_CLEAN_UP_TIMED_OUT_REUSED | |
779 : IDLE_SOCKET_FATE_CLEAN_UP_TIMED_OUT_UNUSED); | |
780 } else { | |
781 DCHECK(!idle_socket_it->IsUsable()); | |
782 RecordIdleSocketFate(IDLE_SOCKET_FATE_CLEAN_UP_UNUSABLE); | |
783 } | |
784 delete idle_socket_it->socket; | |
785 idle_socket_it = group->mutable_idle_sockets()->erase(idle_socket_it); | |
786 DecrementIdleCount(); | |
787 } else { | |
788 ++idle_socket_it; | |
789 } | |
790 } | |
791 | |
792 // Delete group if no longer needed. | 778 // Delete group if no longer needed. |
793 if (group->IsEmpty()) { | 779 if (group->IsEmpty()) { |
794 RemoveGroup(i++); | 780 RemoveGroup(i++); |
795 } else { | 781 } else { |
796 ++i; | 782 ++i; |
797 } | 783 } |
798 } | 784 } |
799 } | 785 } |
800 | 786 |
787 void ClientSocketPoolBaseHelper::CleanupIdleSocketsInGroup( | |
788 bool force, | |
789 Group* group, | |
790 const base::TimeTicks& now) { | |
791 auto idle_socket_it = group->mutable_idle_sockets()->begin(); | |
792 while (idle_socket_it != group->idle_sockets().end()) { | |
793 base::TimeDelta timeout = idle_socket_it->socket->WasEverUsed() | |
794 ? used_idle_socket_timeout_ | |
795 : unused_idle_socket_timeout_; | |
796 bool timed_out = (now - idle_socket_it->start_time) >= timeout; | |
797 bool should_clean_up = force || timed_out || !idle_socket_it->IsUsable(); | |
798 if (should_clean_up) { | |
799 if (force) { | |
800 RecordIdleSocketFate(IDLE_SOCKET_FATE_CLEAN_UP_FORCED); | |
801 } else if (timed_out) { | |
802 RecordIdleSocketFate(idle_socket_it->socket->WasEverUsed() | |
803 ? IDLE_SOCKET_FATE_CLEAN_UP_TIMED_OUT_REUSED | |
804 : IDLE_SOCKET_FATE_CLEAN_UP_TIMED_OUT_UNUSED); | |
805 } else { | |
806 DCHECK(!idle_socket_it->IsUsable()); | |
807 RecordIdleSocketFate(IDLE_SOCKET_FATE_CLEAN_UP_UNUSABLE); | |
808 } | |
809 delete idle_socket_it->socket; | |
810 idle_socket_it = group->mutable_idle_sockets()->erase(idle_socket_it); | |
811 DecrementIdleCount(); | |
812 } else { | |
813 ++idle_socket_it; | |
814 } | |
815 } | |
816 } | |
817 | |
801 ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup( | 818 ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup( |
802 const std::string& group_name) { | 819 const std::string& group_name) { |
803 GroupMap::iterator it = group_map_.find(group_name); | 820 GroupMap::iterator it = group_map_.find(group_name); |
804 if (it != group_map_.end()) | 821 if (it != group_map_.end()) |
805 return it->second; | 822 return it->second; |
806 Group* group = new Group; | 823 Group* group = new Group; |
807 group_map_[group_name] = group; | 824 group_map_[group_name] = group; |
808 return group; | 825 return group; |
809 } | 826 } |
810 | 827 |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1428 // If there are no more requests, kill the backup timer. | 1445 // If there are no more requests, kill the backup timer. |
1429 if (pending_requests_.empty()) | 1446 if (pending_requests_.empty()) |
1430 backup_job_timer_.Stop(); | 1447 backup_job_timer_.Stop(); |
1431 request->CrashIfInvalid(); | 1448 request->CrashIfInvalid(); |
1432 return request; | 1449 return request; |
1433 } | 1450 } |
1434 | 1451 |
1435 } // namespace internal | 1452 } // namespace internal |
1436 | 1453 |
1437 } // namespace net | 1454 } // namespace net |
OLD | NEW |