using FastGithub.DomainResolve;
using System;
using System.Net.Http;
using System.Threading;
namespace FastGithub.Http
{
    /// 
    /// 表示自主管理生命周期的的HttpMessageHandler
    /// 
    sealed class LifetimeHttpHandler : DelegatingHandler
    {
        private readonly Timer timer;
        public LifeTimeKey LifeTimeKey { get; }
        /// 
        /// 具有生命周期的HttpHandler
        /// 
        /// 
        /// 
        /// 
        /// 
        public LifetimeHttpHandler(IDomainResolver domainResolver, LifeTimeKey lifeTimeKey, TimeSpan lifeTime, Action deactivateAction)
        {
            this.LifeTimeKey = lifeTimeKey;
            this.InnerHandler = new HttpClientHandler(lifeTimeKey.DomainConfig, domainResolver);
            this.timer = new Timer(this.OnTimerCallback, deactivateAction, lifeTime, Timeout.InfiniteTimeSpan);
        }
        /// 
        /// timer触发时
        /// 
        /// 
        private void OnTimerCallback(object? state)
        {
            this.timer.Dispose();
            ((Action)(state!))(this);
        }
        /// 
        /// 这里不释放资源
        /// 
        /// 
        protected override void Dispose(bool disposing)
        {
        }
    }
}