Skip to content

Commit

Permalink
Add delay before reconnect to network service in IpfsDnsResolverImpl.
Browse files Browse the repository at this point in the history
Resolves brave/brave-browser#24461
This should lower CPU usage in case the network service fails to start.
  • Loading branch information
cypt4 committed Aug 12, 2022
1 parent fd04ea9 commit ef4ecfc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
10 changes: 8 additions & 2 deletions browser/ipfs/ipfs_dns_resolver_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace ipfs {

const int kThrottleDeltaMs = 1000;

mojo::Remote<network::mojom::DnsConfigChangeManager>
GetDnsConfigChangeManager() {
mojo::Remote<network::mojom::DnsConfigChangeManager>
Expand All @@ -21,7 +23,7 @@ GetDnsConfigChangeManager() {
return dns_config_change_manager_remote;
}

IpfsDnsResolverImpl::IpfsDnsResolverImpl() {
IpfsDnsResolverImpl::IpfsDnsResolverImpl() : weak_ptr_factory_(this) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
dns_config_change_manager_ = GetDnsConfigChangeManager();
SetupDnsConfigChangeNotifications();
Expand All @@ -41,7 +43,11 @@ IpfsDnsResolverImpl::~IpfsDnsResolverImpl() {}
void IpfsDnsResolverImpl::OnDnsConfigChangeManagerConnectionError() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
receiver_.reset();
SetupDnsConfigChangeNotifications();
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&IpfsDnsResolverImpl::SetupDnsConfigChangeNotifications,
weak_ptr_factory_.GetWeakPtr()),
base::Milliseconds(kThrottleDeltaMs));
}

void IpfsDnsResolverImpl::OnDnsConfigChanged() {
Expand Down
3 changes: 3 additions & 0 deletions browser/ipfs/ipfs_dns_resolver_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ class IpfsDnsResolverImpl
absl::optional<std::string> GetFirstDnsOverHttpsServer() override;

private:
FRIEND_TEST_ALL_PREFIXES(IpfsDnsResolverImplUnitTest, ReconnectOnMojoError);
void SetupDnsConfigChangeNotifications();

mojo::Remote<network::mojom::DnsConfigChangeManager>
dns_config_change_manager_;
mojo::Receiver<network::mojom::DnsConfigChangeManagerClient> receiver_{this};

SEQUENCE_CHECKER(sequence_checker_);

base::WeakPtrFactory<IpfsDnsResolverImpl> weak_ptr_factory_;
};

} // namespace ipfs
Expand Down
1 change: 1 addition & 0 deletions browser/ipfs/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ source_set("unittests") {
"//brave/browser/ipfs/ipfs_blob_context_getter_factory_unittest.cc",
"//brave/browser/ipfs/ipfs_host_resolver_unittest.cc",
"//brave/browser/ipfs/ipfs_tab_helper_unittest.cc",
"//brave/browser/ipfs/test/ipfs_dns_resolver_impl_unittest.cc",
"//brave/browser/ipfs/test/ipfs_navigation_throttle_unittest.cc",
"//brave/browser/ipfs/test/ipfs_network_utils_unittest.cc",
"//brave/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc",
Expand Down
45 changes: 45 additions & 0 deletions browser/ipfs/test/ipfs_dns_resolver_impl_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ipfs/ipfs_dns_resolver_impl.h"

#include <memory>
#include <utility>

#include "content/public/browser/network_service_instance.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ipfs {

class IpfsDnsResolverImplUnitTest : public testing::Test {
public:
IpfsDnsResolverImplUnitTest() = default;
IpfsDnsResolverImplUnitTest(const IpfsDnsResolverImplUnitTest&) = delete;
IpfsDnsResolverImplUnitTest& operator=(const IpfsDnsResolverImplUnitTest&) =
delete;
~IpfsDnsResolverImplUnitTest() override = default;

void SetUp() override {}

void TearDown() override {}
content::BrowserTaskEnvironment task_environment_;
};

TEST_F(IpfsDnsResolverImplUnitTest, ReconnectOnMojoError) {
std::unique_ptr<IpfsDnsResolverImpl> ipfs_dns_resolver_impl_ =
std::make_unique<IpfsDnsResolverImpl>();
ipfs_dns_resolver_impl_->receiver_.reset();
ipfs_dns_resolver_impl_->OnDnsConfigChangeManagerConnectionError();
EXPECT_FALSE(ipfs_dns_resolver_impl_->receiver_.is_bound());
{
base::RunLoop loop;
while (!ipfs_dns_resolver_impl_->receiver_.is_bound()) {
loop.RunUntilIdle();
}
}
}

} // namespace ipfs

0 comments on commit ef4ecfc

Please sign in to comment.