1 | /* |
2 | * Copyright (C) 2009 Apple Inc. All Rights Reserved. |
3 | * Copyright (C) 2012 Igalia S.L. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #include "config.h" |
28 | #include "DNSResolveQueue.h" |
29 | |
30 | #if USE(SOUP) |
31 | #include "DNSResolveQueueSoup.h" |
32 | #elif USE(CURL) |
33 | #include "DNSResolveQueueCurl.h" |
34 | #elif USE(CF) |
35 | #include "DNSResolveQueueCFNet.h" |
36 | #endif |
37 | |
38 | #include <wtf/CompletionHandler.h> |
39 | #include <wtf/NeverDestroyed.h> |
40 | |
41 | namespace WebCore { |
42 | |
43 | // When resolve queue is empty, we fire async resolution requests immediately (which is important if the prefetch is triggered by hovering). |
44 | // But during page parsing, we should coalesce identical requests to avoid stressing out the DNS resolver. |
45 | static const int gNamesToResolveImmediately = 4; |
46 | |
47 | // Coalesce prefetch requests for this long before sending them out. |
48 | static const Seconds coalesceDelay { 1_s }; |
49 | |
50 | // Sending many DNS requests at once can overwhelm some gateways. See <rdar://8105550> for specific CFNET issues with CFHost throttling. |
51 | static const int gMaxSimultaneousRequests = 8; |
52 | |
53 | // For a page has links to many outside sites, it is likely that the system DNS resolver won't be able to cache them all anyway, and we don't want |
54 | // to negatively affect other applications' performance by pushing their cached entries out. |
55 | // If we end up with lots of names to prefetch, some will be dropped. |
56 | static const int gMaxRequestsToQueue = 64; |
57 | |
58 | // If there were queued names that couldn't be sent simultaneously, check the state of resolvers after this delay. |
59 | static const Seconds resolvingRetryDelay { 100_ms }; |
60 | |
61 | DNSResolveQueue& DNSResolveQueue::singleton() |
62 | { |
63 | static NeverDestroyed<DNSResolveQueuePlatform> queue; |
64 | |
65 | return queue; |
66 | } |
67 | |
68 | DNSResolveQueue::DNSResolveQueue() |
69 | : m_timer(*this, &DNSResolveQueue::timerFired) |
70 | , m_requestsInFlight(0) |
71 | { |
72 | // isUsingProxy will return the initial value of m_isUsingProxy at first on |
73 | // platforms that have an asynchronous implementation of updateIsUsingProxy, |
74 | // so initialize it to true so we won't prefetch before we know if we are using a proxy. |
75 | } |
76 | |
77 | // Don't do DNS prefetch if proxies are involved. For many proxy types, the user agent is never |
78 | // exposed to the IP address during normal operation. Querying an internal DNS server may not help |
79 | // performance, as it doesn't necessarily look up the actual external IP. Also, if DNS returns a |
80 | // fake internal address, local caches may keep it even after re-connecting to another network. |
81 | bool DNSResolveQueue::isUsingProxy() |
82 | { |
83 | MonotonicTime time = MonotonicTime::now(); |
84 | static const Seconds minimumProxyCheckDelay = 5_s; |
85 | if (time - m_lastProxyEnabledStatusCheckTime > minimumProxyCheckDelay) { |
86 | m_lastProxyEnabledStatusCheckTime = time; |
87 | updateIsUsingProxy(); |
88 | } |
89 | return m_isUsingProxy; |
90 | } |
91 | |
92 | void DNSResolveQueue::add(const String& hostname) |
93 | { |
94 | // If there are no names queued, and few enough are in flight, resolve immediately (the mouse may be over a link). |
95 | if (!m_names.size()) { |
96 | if (isUsingProxy()) |
97 | return; |
98 | if (++m_requestsInFlight <= gNamesToResolveImmediately) { |
99 | platformResolve(hostname); |
100 | return; |
101 | } |
102 | --m_requestsInFlight; |
103 | } |
104 | |
105 | // It's better to not prefetch some names than to clog the queue. |
106 | // Dropping the newest names, because on a single page, these are likely to be below oldest ones. |
107 | if (m_names.size() < gMaxRequestsToQueue) { |
108 | m_names.add(hostname); |
109 | if (!m_timer.isActive()) |
110 | m_timer.startOneShot(coalesceDelay); |
111 | } |
112 | } |
113 | |
114 | void DNSResolveQueue::timerFired() |
115 | { |
116 | if (isUsingProxy()) { |
117 | m_names.clear(); |
118 | return; |
119 | } |
120 | |
121 | int requestsAllowed = gMaxSimultaneousRequests - m_requestsInFlight; |
122 | |
123 | for (; !m_names.isEmpty() && requestsAllowed > 0; --requestsAllowed) { |
124 | ++m_requestsInFlight; |
125 | HashSet<String>::iterator currentName = m_names.begin(); |
126 | platformResolve(*currentName); |
127 | m_names.remove(currentName); |
128 | } |
129 | |
130 | if (!m_names.isEmpty()) |
131 | m_timer.startOneShot(resolvingRetryDelay); |
132 | } |
133 | |
134 | } // namespace WebCore |
135 | |