From 4bdd0b2b93eeedd9e1e819c3dc9558a40a6ca6c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sat, 2 Oct 2021 22:26:07 +0800 Subject: [PATCH] =?UTF-8?q?10=E7=A7=92=E4=B9=8B=E5=90=8E=E5=8A=A0=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.DomainResolve/IPAddressElapsed.cs | 2 +- FastGithub.Http/HttpClientFactory.cs | 58 +++++++++++--------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/FastGithub.DomainResolve/IPAddressElapsed.cs b/FastGithub.DomainResolve/IPAddressElapsed.cs index f5e6f38..60a6cf9 100644 --- a/FastGithub.DomainResolve/IPAddressElapsed.cs +++ b/FastGithub.DomainResolve/IPAddressElapsed.cs @@ -13,7 +13,7 @@ namespace FastGithub.DomainResolve [DebuggerDisplay("Adddress={Adddress} Elapsed={Elapsed}")] struct IPAddressElapsed { - private static readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(10d); + private static readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d); /// /// 获取IP地址 diff --git a/FastGithub.Http/HttpClientFactory.cs b/FastGithub.Http/HttpClientFactory.cs index c8e53d6..61be5a1 100644 --- a/FastGithub.Http/HttpClientFactory.cs +++ b/FastGithub.Http/HttpClientFactory.cs @@ -13,12 +13,17 @@ namespace FastGithub.Http private readonly IDomainResolver domainResolver; /// - /// httpHandler的生命周期 + /// 首次生命周期 /// - private readonly TimeSpan lifeTime = TimeSpan.FromMinutes(1d); + private readonly TimeSpan firstLiftTime = TimeSpan.FromSeconds(10d); /// - /// HttpHandler清理器 + /// 非首次生命周期 + /// + private readonly TimeSpan nextLifeTime = TimeSpan.FromMinutes(1d); + + /// + /// LifetimeHttpHandler清理器 /// private readonly LifetimeHttpHandlerCleaner httpHandlerCleaner = new(); @@ -27,6 +32,7 @@ namespace FastGithub.Http /// private readonly ConcurrentDictionary> httpHandlerLazyCache = new(); + /// /// HttpClient工厂 /// @@ -43,30 +49,16 @@ namespace FastGithub.Http /// public HttpClient CreateHttpClient(DomainConfig domainConfig) { - var lifetimeHttpHandlerLazy = this.httpHandlerLazyCache.GetOrAdd(domainConfig, this.CreateLifetimeHttpHandlerLazy); + var lifetimeHttpHandlerLazy = this.httpHandlerLazyCache.GetOrAdd(domainConfig, CreateLifetimeHttpHandlerLazy); var lifetimeHttpHandler = lifetimeHttpHandlerLazy.Value; return new HttpClient(lifetimeHttpHandler, disposeHandler: false); + + Lazy CreateLifetimeHttpHandlerLazy(DomainConfig domainConfig) + { + return new Lazy(() => this.CreateLifetimeHttpHandler(domainConfig, this.firstLiftTime), true); + } } - /// - /// 创建LazyOf(LifetimeHttpHandler) - /// - /// - /// - private Lazy CreateLifetimeHttpHandlerLazy(DomainConfig domainConfig) - { - return new Lazy(() => this.CreateLifetimeHttpHandler(domainConfig), true); - } - - /// - /// 创建LifetimeHttpHandler - /// - /// - private LifetimeHttpHandler CreateLifetimeHttpHandler(DomainConfig domainConfig) - { - var httpClientHandler = new HttpClientHandler(domainConfig, this.domainResolver); - return new LifetimeHttpHandler(httpClientHandler, this.lifeTime, this.OnLifetimeHttpHandlerDeactivate); - } /// /// 当有httpHandler失效时 @@ -74,10 +66,26 @@ namespace FastGithub.Http /// httpHandler private void OnLifetimeHttpHandlerDeactivate(LifetimeHttpHandler lifetimeHttpHandler) { - // 切换激活状态的记录的实例 var domainConfig = lifetimeHttpHandler.DomainConfig; - this.httpHandlerLazyCache[domainConfig] = this.CreateLifetimeHttpHandlerLazy(domainConfig); + this.httpHandlerLazyCache[domainConfig] = CreateLifetimeHttpHandlerLazy(domainConfig); this.httpHandlerCleaner.Add(lifetimeHttpHandler); + + Lazy CreateLifetimeHttpHandlerLazy(DomainConfig domainConfig) + { + return new Lazy(() => this.CreateLifetimeHttpHandler(domainConfig, this.nextLifeTime), true); + } + } + + /// + /// 创建LifetimeHttpHandler + /// + /// + /// + /// + private LifetimeHttpHandler CreateLifetimeHttpHandler(DomainConfig domainConfig, TimeSpan lifeTime) + { + var httpClientHandler = new HttpClientHandler(domainConfig, this.domainResolver); + return new LifetimeHttpHandler(httpClientHandler, lifeTime, this.OnLifetimeHttpHandlerDeactivate); } } } \ No newline at end of file