forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifetimeHttpHandler.cs
More file actions
49 lines (44 loc) · 1.54 KB
/
LifetimeHttpHandler.cs
File metadata and controls
49 lines (44 loc) · 1.54 KB
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
using FastGithub.DomainResolve;
using System;
using System.Net.Http;
using System.Threading;
namespace FastGithub.Http
{
/// <summary>
/// 表示自主管理生命周期的的HttpMessageHandler
/// </summary>
sealed class LifetimeHttpHandler : DelegatingHandler
{
private readonly Timer timer;
public LifeTimeKey LifeTimeKey { get; }
/// <summary>
/// 具有生命周期的HttpHandler
/// </summary>
/// <param name="domainResolver"></param>
/// <param name="lifeTimeKey"></param>
/// <param name="lifeTime"></param>
/// <param name="deactivateAction"></param>
public LifetimeHttpHandler(IDomainResolver domainResolver, LifeTimeKey lifeTimeKey, TimeSpan lifeTime, Action<LifetimeHttpHandler> deactivateAction)
{
this.LifeTimeKey = lifeTimeKey;
this.InnerHandler = new HttpClientHandler(lifeTimeKey.DomainConfig, domainResolver);
this.timer = new Timer(this.OnTimerCallback, deactivateAction, lifeTime, Timeout.InfiniteTimeSpan);
}
/// <summary>
/// timer触发时
/// </summary>
/// <param name="state"></param>
private void OnTimerCallback(object? state)
{
this.timer.Dispose();
((Action<LifetimeHttpHandler>)(state!))(this);
}
/// <summary>
/// 这里不释放资源
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
}
}
}