forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClientFactory.cs
90 lines (78 loc) · 3.23 KB
/
HttpClientFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using FastGithub.Configuration;
using FastGithub.DomainResolve;
using System;
using System.Collections.Concurrent;
namespace FastGithub.Http
{
/// <summary>
/// HttpClient工厂
/// </summary>
sealed class HttpClientFactory : IHttpClientFactory
{
private readonly IDomainResolver domainResolver;
/// <summary>
/// 首次生命周期
/// </summary>
private readonly TimeSpan firstLiftTime = TimeSpan.FromSeconds(10d);
/// <summary>
/// 非首次生命周期
/// </summary>
private readonly TimeSpan nextLifeTime = TimeSpan.FromSeconds(100d);
/// <summary>
/// LifetimeHttpHandler清理器
/// </summary>
private readonly LifetimeHttpHandlerCleaner httpHandlerCleaner = new();
/// <summary>
/// LazyOf(LifetimeHttpHandler)缓存
/// </summary>
private readonly ConcurrentDictionary<LifeTimeKey, Lazy<LifetimeHttpHandler>> httpHandlerLazyCache = new();
/// <summary>
/// HttpClient工厂
/// </summary>
/// <param name="domainResolver"></param>
public HttpClientFactory(IDomainResolver domainResolver)
{
this.domainResolver = domainResolver;
}
/// <summary>
/// 创建httpClient
/// </summary>
/// <param name="domain"></param>
/// <param name="domainConfig"></param>
/// <returns></returns>
public HttpClient CreateHttpClient(string domain, DomainConfig domainConfig)
{
var lifeTimeKey = new LifeTimeKey(domain, domainConfig);
var lifetimeHttpHandler = this.httpHandlerLazyCache.GetOrAdd(lifeTimeKey, CreateLifetimeHttpHandlerLazy).Value;
return new HttpClient(lifetimeHttpHandler, disposeHandler: false);
Lazy<LifetimeHttpHandler> CreateLifetimeHttpHandlerLazy(LifeTimeKey lifeTimeKey)
{
return new Lazy<LifetimeHttpHandler>(() => this.CreateLifetimeHttpHandler(lifeTimeKey, this.firstLiftTime), true);
}
}
/// <summary>
/// 创建LifetimeHttpHandler
/// </summary>
/// <param name="lifeTimeKey"></param>
/// <param name="lifeTime"></param>
/// <returns></returns>
private LifetimeHttpHandler CreateLifetimeHttpHandler(LifeTimeKey lifeTimeKey, TimeSpan lifeTime)
{
return new LifetimeHttpHandler(this.domainResolver, lifeTimeKey, lifeTime, this.OnLifetimeHttpHandlerDeactivate);
}
/// <summary>
/// 当有httpHandler失效时
/// </summary>
/// <param name="lifetimeHttpHandler">httpHandler</param>
private void OnLifetimeHttpHandlerDeactivate(LifetimeHttpHandler lifetimeHttpHandler)
{
var lifeTimeKey = lifetimeHttpHandler.LifeTimeKey;
this.httpHandlerLazyCache[lifeTimeKey] = CreateLifetimeHttpHandlerLazy(lifeTimeKey);
this.httpHandlerCleaner.Add(lifetimeHttpHandler);
Lazy<LifetimeHttpHandler> CreateLifetimeHttpHandlerLazy(LifeTimeKey lifeTimeKey)
{
return new Lazy<LifetimeHttpHandler>(() => this.CreateLifetimeHttpHandler(lifeTimeKey, this.nextLifeTime), true);
}
}
}
}