| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Concurrent; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Threading; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Holds a cache of failing proxies and manages when they should be retried. |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class FailedProxyCache |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// When returned by <see cref="GetProxyRenewTicks"/>, indicates a proxy is immediately usable. |
| | | 18 | | /// </summary> |
| | | 19 | | public const long Immediate = 0; |
| | | 20 | | |
| | | 21 | | // If a proxy fails, time out 30 minutes. WinHTTP and Firefox both use this. |
| | | 22 | | private const int FailureTimeoutInMilliseconds = 1000 * 60 * 30; |
| | | 23 | | |
| | | 24 | | // Scan through the failures and flush any that have expired every 5 minutes. |
| | | 25 | | private const int FlushFailuresTimerInMilliseconds = 1000 * 60 * 5; |
| | | 26 | | |
| | | 27 | | // _failedProxies will only be flushed (rare but somewhat expensive) if we have more than this number of proxies |
| | | 28 | | private const int LargeProxyConfigBoundary = 8; |
| | | 29 | | |
| | | 30 | | // Value is the Environment.TickCount64 to remove the proxy from the failure list. |
| | 0 | 31 | | private readonly ConcurrentDictionary<Uri, long> _failedProxies = new ConcurrentDictionary<Uri, long>(); |
| | | 32 | | |
| | | 33 | | // When Environment.TickCount64 >= _nextFlushTicks, cause a flush. |
| | 0 | 34 | | private long _nextFlushTicks = Environment.TickCount64 + FlushFailuresTimerInMilliseconds; |
| | | 35 | | |
| | | 36 | | // This lock can be folded into _nextFlushTicks for space optimization, but |
| | | 37 | | // this class should only have a single instance so would rather have clarity. |
| | 0 | 38 | | private SpinLock _flushLock = new SpinLock(enableThreadOwnerTracking: false); // mutable struct; do not make thi |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Checks when a proxy will become usable. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="uri">The <see cref="Uri"/> of the proxy to check.</param> |
| | | 44 | | /// <returns>If the proxy can be used, <see cref="Immediate"/>. Otherwise, the next <see cref="Environment.TickC |
| | | 45 | | public long GetProxyRenewTicks(Uri uri) |
| | 0 | 46 | | { |
| | 0 | 47 | | Cleanup(); |
| | | 48 | | |
| | | 49 | | // If not failed, ready immediately. |
| | 0 | 50 | | if (!_failedProxies.TryGetValue(uri, out long renewTicks)) |
| | 0 | 51 | | { |
| | 0 | 52 | | return Immediate; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // If we haven't reached out renew time, the proxy can't be used. |
| | 0 | 56 | | if (Environment.TickCount64 < renewTicks) |
| | 0 | 57 | | { |
| | 0 | 58 | | return renewTicks; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | // Renew time reached, we can remove the proxy from the cache. |
| | 0 | 62 | | if (TryRenewProxy(uri, renewTicks)) |
| | 0 | 63 | | { |
| | 0 | 64 | | return Immediate; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // Another thread updated the cache before we could remove it. |
| | | 68 | | // We can't know if this is a removal or an update, so check again. |
| | 0 | 69 | | return _failedProxies.TryGetValue(uri, out renewTicks) ? renewTicks : Immediate; |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Sets a proxy as failed, to avoid trying it again for some time. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="uri">The URI of the proxy.</param> |
| | | 76 | | public void SetProxyFailed(Uri uri) |
| | 0 | 77 | | { |
| | 0 | 78 | | _failedProxies[uri] = Environment.TickCount64 + FailureTimeoutInMilliseconds; |
| | 0 | 79 | | Cleanup(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Renews a proxy prior to its period expiring. Used when all proxies are failed to renew the proxy closest to |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="uri">The <paramref name="uri"/> of the proxy to renew.</param> |
| | | 86 | | /// <param name="renewTicks">The current renewal time for the proxy. If the value has changed from this, the pro |
| | | 87 | | public bool TryRenewProxy(Uri uri, long renewTicks) => |
| | 0 | 88 | | _failedProxies.TryRemove(new KeyValuePair<Uri, long>(uri, renewTicks)); |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Cleans up any old proxies that should no longer be marked as failing. |
| | | 92 | | /// </summary> |
| | | 93 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 94 | | private void Cleanup() |
| | 0 | 95 | | { |
| | 0 | 96 | | if (_failedProxies.Count > LargeProxyConfigBoundary && Environment.TickCount64 >= Interlocked.Read(ref _next |
| | 0 | 97 | | { |
| | 0 | 98 | | CleanupHelper(); |
| | 0 | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Cleans up any old proxies that should no longer be marked as failing. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <remarks> |
| | | 106 | | /// I expect this to never be called by <see cref="Cleanup"/> in a production system. It is only needed in the c |
| | | 107 | | /// that a system has a very large number of proxies that the PAC script cycles through. It is moderately expens |
| | | 108 | | /// so it's only run periodically and is disabled until we exceed <see cref="LargeProxyConfigBoundary"/> failed |
| | | 109 | | /// </remarks> |
| | | 110 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 111 | | private void CleanupHelper() |
| | 0 | 112 | | { |
| | 0 | 113 | | bool lockTaken = false; |
| | | 114 | | try |
| | 0 | 115 | | { |
| | 0 | 116 | | _flushLock.TryEnter(ref lockTaken); |
| | 0 | 117 | | if (!lockTaken) |
| | 0 | 118 | | { |
| | 0 | 119 | | return; |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | long curTicks = Environment.TickCount64; |
| | | 123 | | |
| | 0 | 124 | | foreach (KeyValuePair<Uri, long> kvp in _failedProxies) |
| | 0 | 125 | | { |
| | 0 | 126 | | if (curTicks >= kvp.Value) |
| | 0 | 127 | | { |
| | 0 | 128 | | ((ICollection<KeyValuePair<Uri, long>>)_failedProxies).Remove(kvp); |
| | 0 | 129 | | } |
| | 0 | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | finally |
| | 0 | 133 | | { |
| | 0 | 134 | | if (lockTaken) |
| | 0 | 135 | | { |
| | 0 | 136 | | Interlocked.Exchange(ref _nextFlushTicks, Environment.TickCount64 + FlushFailuresTimerInMilliseconds |
| | 0 | 137 | | _flushLock.Exit(false); |
| | 0 | 138 | | } |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |