88using System . Net ;
99using System . Net . Http ;
1010using System . Net . Http . Headers ;
11- using System . Runtime . InteropServices ;
1211using System . Text ;
1312using System . Threading ;
1413using System . Threading . Tasks ;
@@ -32,19 +31,10 @@ public abstract class HttpRequestor : IDisposable
3231
3332 static HttpRequestor ( )
3433 {
35- /* If machine.config is locked, then initializing ServicePointManager will fail and be unrecoverable.
36- * Machine.config locking is typically very brief (~1ms by the antivirus scanner) so we can attempt to lock
37- * it ourselves (by opening it for read) *beforehand and briefly wait if it's locked */
38- using ( var machineConfigLock = GetMachineConfigLock ( ) )
39- {
40- ServicePointManager . SecurityProtocol = ServicePointManager . SecurityProtocol | SecurityProtocolType . Tls12 ;
41-
42- // HTTP downloads are I/O-bound, not CPU-bound, so we default to
43- // 2x ProcessorCount. Can be overridden via gvfs.max-http-connections.
44- int connectionLimit = 2 * Environment . ProcessorCount ;
45- ServicePointManager . DefaultConnectionLimit = connectionLimit ;
46- availableConnections = new SemaphoreSlim ( connectionLimit ) ;
47- }
34+ // HTTP downloads are I/O-bound, not CPU-bound, so we default to
35+ // 2x ProcessorCount. Can be overridden via gvfs.max-http-connections.
36+ int connectionLimit = 2 * Environment . ProcessorCount ;
37+ availableConnections = new SemaphoreSlim ( connectionLimit ) ;
4838 }
4939
5040 protected HttpRequestor ( ITracer tracer , RetryConfig retryConfig , Enlistment enlistment )
@@ -62,7 +52,7 @@ protected HttpRequestor(ITracer tracer, RetryConfig retryConfig, Enlistment enli
6252 TryApplyConnectionLimitFromConfig ( tracer , enlistment ) ;
6353 }
6454
65- HttpClientHandler httpClientHandler = new HttpClientHandler ( ) { UseDefaultCredentials = true } ;
55+ HttpClientHandler httpClientHandler = new HttpClientHandler ( ) ;
6656
6757 this . authentication . ConfigureHttpClientHandlerSslIfNeeded ( this . Tracer , httpClientHandler , enlistment . CreateGitProcess ( ) ) ;
6858
@@ -180,8 +170,8 @@ protected GitEndPointResponseData SendRequest(
180170 }
181171 catch ( HttpRequestException httpRequestException ) when ( TryGetResponseMessageFromHttpRequestException ( httpRequestException , request , out response ) )
182172 {
183- /* HttpClientHandler will automatically resubmit in certain circumstances, such as a 401 unauthorized response when UseDefaultCredentials
184- * is true but another credential was provided. This resubmit can throw (instead of returning a proper status code) in some case cases, such
173+ /* HttpClientHandler may automatically resubmit in certain circumstances, such as a 401 unauthorized response.
174+ * This resubmit can throw (instead of returning a proper status code) in some cases, such
185175 * as when there is an exception loading the default credentials.
186176 * If we can extract the original response message from the exception, we can continue and process the original failed status code. */
187177 Tracer . RelatedWarning ( responseMetadata , $ "An exception occurred while resubmitting the request, but the original response is available.") ;
@@ -391,8 +381,7 @@ private static void TryApplyConnectionLimitFromConfig(ITracer tracer, Enlistment
391381
392382 if ( configuredLimit > 0 )
393383 {
394- int currentLimit = ServicePointManager . DefaultConnectionLimit ;
395- ServicePointManager . DefaultConnectionLimit = configuredLimit ;
384+ int currentLimit = availableConnections . CurrentCount ;
396385
397386 // Adjust the existing semaphore rather than replacing it, so any
398387 // in-flight waiters release permits to the correct instance.
@@ -425,28 +414,5 @@ private static void TryApplyConnectionLimitFromConfig(ITracer tracer, Enlistment
425414 tracer . RelatedWarning ( metadata , "HttpRequestor: Failed to read gvfs.max-http-connections config, using default" ) ;
426415 }
427416 }
428-
429- private static FileStream GetMachineConfigLock ( )
430- {
431- var machineConfigLocation = RuntimeEnvironment . SystemConfigurationFile ;
432- var tries = 0 ;
433- var maxTries = 3 ;
434- while ( tries ++ < maxTries )
435- {
436- try
437- {
438- /* Opening with FileShare.Read will fail if another process (eg antivirus) has opened the file for write,
439- but will still let ServicePointManager read the file.*/
440- FileStream stream = File . Open ( machineConfigLocation , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
441- return stream ;
442- }
443- catch ( IOException e ) when ( ( uint ) e . HResult == 0x80070020 ) // SHARING_VIOLATION
444- {
445- Thread . Sleep ( 10 ) ;
446- }
447- }
448- /* Couldn't get the lock - the process will likely fail. */
449- return null ;
450- }
451417 }
452418}
0 commit comments