11using System ;
2- using System . Collections . Concurrent ;
32using System . Net . Http ;
43using System . Threading ;
5- using System . Threading . Tasks ;
64
75namespace WebApiClient
86{
@@ -30,32 +28,22 @@ public class HttpApiFactory<TInterface> : IHttpApiFactory<TInterface>, IHttpApiF
3028 private TimeSpan lifeTime = TimeSpan . FromMinutes ( 2d ) ;
3129
3230 /// <summary>
33- /// 清理handler时间间隔
31+ /// 具有生命周期的拦截器延时创建对象
3432 /// </summary>
35- private TimeSpan cleanupInterval = TimeSpan . FromSeconds ( 10d ) ;
33+ private Lazy < LifetimeInterceptor > lifeTimeInterceptorLazy ;
3634
3735 /// <summary>
38- /// 过期的拦截器
36+ /// 拦截器清理器
3937 /// </summary>
40- private readonly ConcurrentQueue < ExpiredInterceptor > expiredInterceptors = new ConcurrentQueue < ExpiredInterceptor > ( ) ;
41-
42- /// <summary>
43- /// 当前过期的拦截器的数量
44- /// </summary>
45- private int expiredCount = 0 ;
46-
47- /// <summary>
48- /// 激活的拦截器
49- /// </summary>
50- private Lazy < ActiveInterceptor > activeInterceptorLazy ;
38+ private readonly InterceptorCleaner interceptorCleaner = new InterceptorCleaner ( ) ;
5139
5240 /// <summary>
5341 /// HttpApi创建工厂
5442 /// </summary>
5543 public HttpApiFactory ( )
5644 {
57- this . activeInterceptorLazy = new Lazy < ActiveInterceptor > (
58- this . CreateActiveInterceptor ,
45+ this . lifeTimeInterceptorLazy = new Lazy < LifetimeInterceptor > (
46+ this . CreateInterceptor ,
5947 LazyThreadSafetyMode . ExecutionAndPublication ) ;
6048 }
6149
@@ -85,7 +73,7 @@ public HttpApiFactory<TInterface> SetCleanupInterval(TimeSpan interval)
8573 {
8674 throw new ArgumentOutOfRangeException ( ) ;
8775 }
88- this . cleanupInterval = interval ;
76+ this . interceptorCleaner . CleanupInterval = interval ;
8977 return this ;
9078 }
9179
@@ -126,15 +114,15 @@ public TInterface CreateHttpApi()
126114 /// <returns></returns>
127115 object IHttpApiFactory . CreateHttpApi ( )
128116 {
129- var interceptor = this . activeInterceptorLazy . Value ;
117+ var interceptor = this . lifeTimeInterceptorLazy . Value ;
130118 return HttpApiClient . Create ( typeof ( TInterface ) , interceptor ) ;
131119 }
132120
133121 /// <summary>
134- /// 创建激活状态的拦截器
122+ /// 创建LifetimeInterceptor
135123 /// </summary>
136124 /// <returns></returns>
137- private ActiveInterceptor CreateActiveInterceptor ( )
125+ private LifetimeInterceptor CreateInterceptor ( )
138126 {
139127 var handler = this . handlerFunc ? . Invoke ( ) ?? new DefaultHttpClientHandler ( ) ;
140128 var httpApiConfig = new HttpApiConfig ( handler , true ) ;
@@ -144,7 +132,7 @@ private ActiveInterceptor CreateActiveInterceptor()
144132 this . configAction . Invoke ( httpApiConfig ) ;
145133 }
146134
147- return new ActiveInterceptor (
135+ return new LifetimeInterceptor (
148136 httpApiConfig ,
149137 this . lifeTime ,
150138 this . OnInterceptorDeactivate ) ;
@@ -153,61 +141,15 @@ private ActiveInterceptor CreateActiveInterceptor()
153141 /// <summary>
154142 /// 当有拦截器失效时
155143 /// </summary>
156- /// <param name="active">激活的拦截器 </param>
157- private void OnInterceptorDeactivate ( ActiveInterceptor active )
144+ /// <param name="interceptor">拦截器 </param>
145+ private void OnInterceptorDeactivate ( LifetimeInterceptor interceptor )
158146 {
159147 // 切换激活状态的记录的实例
160- this . activeInterceptorLazy = new Lazy < ActiveInterceptor > (
161- this . CreateActiveInterceptor ,
148+ this . lifeTimeInterceptorLazy = new Lazy < LifetimeInterceptor > (
149+ this . CreateInterceptor ,
162150 LazyThreadSafetyMode . ExecutionAndPublication ) ;
163151
164- var expired = new ExpiredInterceptor ( active ) ;
165- this . expiredInterceptors . Enqueue ( expired ) ;
166-
167- // 从0变为1,要启动清理作业
168- if ( Interlocked . Increment ( ref this . expiredCount ) == 1 )
169- {
170- this . RegisteCleanup ( ) ;
171- }
172- }
173-
174-
175- /// <summary>
176- /// 注册清理任务
177- /// </summary>
178- private async void RegisteCleanup ( )
179- {
180- while ( this . Cleanup ( ) == false )
181- {
182- await Task . Delay ( this . cleanupInterval ) . ConfigureAwait ( false ) ;
183- }
184- }
185-
186- /// <summary>
187- /// 清理失效的拦截器
188- /// </summary>
189- /// <returns>是否完全清理</returns>
190- private bool Cleanup ( )
191- {
192- var cleanCount = this . expiredInterceptors . Count ;
193- for ( var i = 0 ; i < cleanCount ; i ++ )
194- {
195- this . expiredInterceptors . TryDequeue ( out var expired ) ;
196- if ( expired . CanDispose == false )
197- {
198- this . expiredInterceptors . Enqueue ( expired ) ;
199- }
200- else
201- {
202- expired . Dispose ( ) ;
203- if ( Interlocked . Decrement ( ref this . expiredCount ) == 0 )
204- {
205- return true ;
206- }
207- }
208- }
209-
210- return false ;
152+ this . interceptorCleaner . Add ( interceptor ) ;
211153 }
212154 }
213155}
0 commit comments