Example #1
0
        private static HttpClientCache CreateHttpClientCache(IClientConfig clientConfig)
        {
            var clients = new HttpClient[clientConfig.HttpClientCacheSize];

            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = CreateHttpClient(clientConfig);
            }
            var cache = new HttpClientCache(clients);

            return(cache);
        }
Example #2
0
        /// <summary>
        /// Creates an HTTP request for the given URI.
        /// </summary>
        /// <param name="requestUri">The request URI.</param>
        /// <returns>An HTTP request.</returns>
        public IHttpRequest <HttpContent> CreateHttpRequest(Uri requestUri)
        {
            HttpClient httpClient = null;

            if (ClientConfig.CacheHttpClients(_clientConfig))
            {
                if (_httpClientCache == null)
                {
                    if (!ClientConfig.UseGlobalHttpClientCache(_clientConfig))
                    {
                        _useGlobalHttpClientCache = false;

                        _httpClientCacheRWLock.EnterWriteLock();
                        try
                        {
                            if (_httpClientCache == null)
                            {
                                _httpClientCache = CreateHttpClientCache(_clientConfig);
                            }
                        }
                        finally
                        {
                            _httpClientCacheRWLock.ExitWriteLock();
                        }
                    }
                    else
                    {
                        _useGlobalHttpClientCache = true;

                        // Check to see if an HttpClient was created by another service client with the
                        // same settings on the ClientConfig.
                        var configUniqueString = ClientConfig.CreateConfigUniqueString(_clientConfig);
                        _httpClientCacheRWLock.EnterReadLock();
                        try
                        {
                            _httpClientCaches.TryGetValue(configUniqueString, out _httpClientCache);
                        }
                        finally
                        {
                            _httpClientCacheRWLock.ExitReadLock();
                        }

                        // If a HttpClientCache is not found in the global cache then create one
                        // for this and other service clients to use.
                        if (_httpClientCache == null)
                        {
                            _httpClientCacheRWLock.EnterWriteLock();
                            try
                            {
                                // Check if the HttpClientCache was created by some other thread
                                // while this thread was waiting for the lock.
                                if (!_httpClientCaches.TryGetValue(configUniqueString, out _httpClientCache))
                                {
                                    _httpClientCache = CreateHttpClientCache(_clientConfig);
                                    _httpClientCaches[configUniqueString] = _httpClientCache;
                                }
                            }
                            finally
                            {
                                _httpClientCacheRWLock.ExitWriteLock();
                            }
                        }
                    }
                }

                // Now that we have a HttpClientCache from either the global cache or just created a new HttpClientCache
                // get the next HttpClient to be used for making a web request.
                httpClient = _httpClientCache.GetNextClient();
            }
            else
            {
                httpClient = CreateHttpClient(_clientConfig);
            }

            return(new HttpWebRequestMessage(httpClient, requestUri, _clientConfig));
        }