From 189e61ede01f66285e299a7691f61adfe905d422 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 14 Aug 2023 12:03:01 -0400 Subject: [PATCH] Add HttpClientProvider --- OptimizelySDK/OptimizelySDK.csproj | 1 + OptimizelySDK/Utils/HttpClientProvider.cs | 80 +++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 OptimizelySDK/Utils/HttpClientProvider.cs diff --git a/OptimizelySDK/OptimizelySDK.csproj b/OptimizelySDK/OptimizelySDK.csproj index 1812a2ad..585d0b0a 100644 --- a/OptimizelySDK/OptimizelySDK.csproj +++ b/OptimizelySDK/OptimizelySDK.csproj @@ -173,6 +173,7 @@ + diff --git a/OptimizelySDK/Utils/HttpClientProvider.cs b/OptimizelySDK/Utils/HttpClientProvider.cs new file mode 100644 index 00000000..7507d76b --- /dev/null +++ b/OptimizelySDK/Utils/HttpClientProvider.cs @@ -0,0 +1,80 @@ +/* + * Copyright 2023 Optimizely + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Net.Http; +using System.Threading.Tasks; + +namespace OptimizelySDK.Utils +{ + /// + /// Provides a singleton instance of HttpClient to prevent socket exhaustion. + /// + public class HttpClientProvider + { + private static readonly object lockObject = new object(); + private Lazy _httpClient = new Lazy(CreateNewHttpClient); + + /// + /// Gets a thread-safe, singleton instance of HttpClient. + /// + /// HttpClient instance + public HttpClient GetClient() + { + if (IsDisposed(_httpClient.Value)) + { + lock (lockObject) + { + if (IsDisposed(_httpClient.Value)) + { + _httpClient = new Lazy(CreateNewHttpClient); + } + } + } + + return _httpClient.Value; + } + + /// + /// Creates a new HttpClient instance. + /// + /// HttpClient instance + private static HttpClient CreateNewHttpClient() + { + return new HttpClient(); + } + + /// + /// Checks if the HttpClient is disposed by attempting to access a property that + /// will throw an exception if the client is disposed. + /// + /// Client to check + /// True if the client is disposed, false otherwise. + private static bool IsDisposed(HttpClient httpClient) + { + try + { + _ = httpClient.BaseAddress; + } + catch (ObjectDisposedException) + { + return true; + } + + return false; + } + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy