Bug 1471280 - Add new pref for how much longer resolver threads should remain idle draft
authorValentin Gosu <valentin.gosu@gmail.com>
Wed, 04 Jul 2018 21:25:28 +0200
changeset 814225 f5e9c8844137b18c8a764c449eb01c8f3732b067
parent 814224 58e8c141cf41e2a7746612ae999c69af549bdeab
push id115137
push uservalentin.gosu@gmail.com
push dateWed, 04 Jul 2018 19:44:12 +0000
bugs1471280
milestone63.0a1
Bug 1471280 - Add new pref for how much longer resolver threads should remain idle The new pref is "network.dns.resolver-thread-extra-idle-time-seconds" The default is 60 seconds. This means that threads will stay idle for an extra 60 seconds, after which they are shutdown. Setting the pref to 0 would preserve the behaviour before the threads were swiched to use nsThreadPool - meaning that they would be shutdown immediately after ThreadFunc completes. Setting the pref to -1 would keep the threads alive forever. MozReview-Commit-ID: CoUB5gan4MR
modules/libpref/init/all.js
netwerk/dns/nsHostResolver.cpp
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -2036,16 +2036,20 @@ pref("network.dns.localDomains", "");
 // When non empty all non-localhost DNS queries (including IP addresses)
 // resolve to this value. The value can be a name or an IP address.
 // domains mapped to localhost with localDomains stay localhost.
 pref("network.dns.forceResolve", "");
 
 // Contols whether or not "localhost" should resolve when offline
 pref("network.dns.offline-localhost", true);
 
+// Defines how much longer resolver threads should stay idle before are shut down.
+// A negative value will keep the thread alive forever.
+pref("network.dns.resolver-thread-extra-idle-time-seconds", 60);
+
 // The maximum allowed length for a URL - 1MB default
 pref("network.standard-url.max-length", 1048576);
 
 // Whether nsIURI.host/.hostname/.spec should return a punycode string
 // If set to false we will revert to previous behaviour and return a unicode string.
 pref("network.standard-url.punycode-host", true);
 
 // Idle timeout for ftp control connections - 5 minute default
--- a/netwerk/dns/nsHostResolver.cpp
+++ b/netwerk/dns/nsHostResolver.cpp
@@ -494,16 +494,17 @@ nsHostRecord::RemoveOrRefresh()
     // Already resolved; not in a pending state; remove from cache
     return true;
 }
 
 //----------------------------------------------------------------------------
 
 static const char kPrefGetTtl[] = "network.dns.get-ttl";
 static const char kPrefNativeIsLocalhost[] = "network.dns.native-is-localhost";
+static const char kPrefThreadIdleTime[] = "network.dns.resolver-thread-extra-idle-time-seconds";
 static bool sGetTtlEnabled = false;
 mozilla::Atomic<bool, mozilla::Relaxed> gNativeIsLocalhost;
 
 static void DnsPrefChanged(const char* aPref, void* aClosure)
 {
     MOZ_ASSERT(NS_IsMainThread(),
                "Should be getting pref changed notification on main thread!");
 
@@ -579,19 +580,33 @@ nsHostResolver::Init()
     // during application startup.
     static int initCount = 0;
     if (initCount++ > 0) {
         LOG(("Calling 'res_ninit'.\n"));
         res_ninit(&_res);
     }
 #endif
 
+    // We can configure the threadpool to keep threads alive for a while after
+    // the last ThreadFunc task has been executed.
+    int32_t poolTimeoutSecs = Preferences::GetInt(kPrefThreadIdleTime, 60);
+    uint32_t poolTimeoutMs;
+    if (poolTimeoutSecs < 0) {
+        // This means never shut down the idle threads
+        poolTimeoutMs = UINT32_MAX;
+    } else {
+        // We clamp down the idle time between 0 and one hour.
+        poolTimeoutMs = mozilla::clamped<uint32_t>(poolTimeoutSecs * 1000,
+                                                   0, 3600 * 1000);
+    }
+
     nsCOMPtr<nsIThreadPool> threadPool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
     MOZ_ALWAYS_SUCCEEDS(threadPool->SetThreadLimit(MAX_RESOLVER_THREADS));
     MOZ_ALWAYS_SUCCEEDS(threadPool->SetIdleThreadLimit(MAX_RESOLVER_THREADS));
+    MOZ_ALWAYS_SUCCEEDS(threadPool->SetIdleThreadTimeout(poolTimeoutMs));
     MOZ_ALWAYS_SUCCEEDS(threadPool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize));
     MOZ_ALWAYS_SUCCEEDS(threadPool->SetName(NS_LITERAL_CSTRING("DNS Resolver")));
     mResolverThreads = threadPool.forget();
 
     return NS_OK;
 }
 
 void