取消启动等待DnscryptProxy,因为影响到windows服务启动

This commit is contained in:
xljiulang 2021-07-24 02:45:26 +08:00
parent eaf9d2bbad
commit ec06b11d7e
5 changed files with 2 additions and 90 deletions

View File

@ -13,7 +13,6 @@ namespace FastGithub.DnscryptProxy
{ {
private readonly DnscryptProxyService dnscryptProxyService; private readonly DnscryptProxyService dnscryptProxyService;
private readonly ILogger<DnscryptProxyHostedService> logger; private readonly ILogger<DnscryptProxyHostedService> logger;
private readonly TimeSpan dnsOKTimeout = TimeSpan.FromSeconds(60d);
/// <summary> /// <summary>
/// DnscryptProxy后台服务 /// DnscryptProxy后台服务
@ -50,7 +49,6 @@ namespace FastGithub.DnscryptProxy
{ {
process.EnableRaisingEvents = true; process.EnableRaisingEvents = true;
process.Exited += this.OnProcessExit; process.Exited += this.OnProcessExit;
await this.WaitForDnsOKAsync(cancellationToken);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -59,31 +57,6 @@ namespace FastGithub.DnscryptProxy
} }
} }
/// <summary>
/// 等待dns服务初始化
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task WaitForDnsOKAsync(CancellationToken cancellationToken)
{
this.logger.LogInformation($"{this.dnscryptProxyService}正在初始化");
using var timeoutTokenSource = new CancellationTokenSource(this.dnsOKTimeout);
try
{
using var linkeTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
await this.dnscryptProxyService.WaitForDnsOKAsync(linkeTokenSource.Token);
this.logger.LogInformation($"{this.dnscryptProxyService}初始化完成");
}
catch (Exception)
{
if (timeoutTokenSource.IsCancellationRequested)
{
this.logger.LogWarning($"{this.dnscryptProxyService}在{this.dnsOKTimeout.TotalSeconds}秒内未能初始化完成");
}
}
}
/// <summary> /// <summary>
/// 进程退出时 /// 进程退出时
/// </summary> /// </summary>

View File

@ -1,6 +1,4 @@
using DNS.Client; using System;
using DNS.Protocol;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -14,7 +12,6 @@ namespace FastGithub.DnscryptProxy
sealed class DnscryptProxyService sealed class DnscryptProxyService
{ {
private const string name = "dnscrypt-proxy"; private const string name = "dnscrypt-proxy";
private const string testDomain = "api.github.com";
private readonly FastGithubConfig fastGithubConfig; private readonly FastGithubConfig fastGithubConfig;
/// <summary> /// <summary>
@ -65,62 +62,6 @@ namespace FastGithub.DnscryptProxy
} }
} }
/// <summary>
/// 等待dns服务OK
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task WaitForDnsOKAsync(CancellationToken cancellationToken)
{
var process = this.Process;
if (process == null || process.HasExited || this.ControllState != ControllState.Started)
{
return;
}
using var processExitTokenSource = new CancellationTokenSource();
process.EnableRaisingEvents = true;
process.Exited += OnProcessExited;
try
{
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token);
await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token);
}
finally
{
process.Exited -= OnProcessExited;
}
void OnProcessExited(object? sender, EventArgs eventArgs)
{
processExitTokenSource.Cancel();
}
}
/// <summary>
/// 等待dns服务OK
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task WaitForDnsOKCoreAsync(CancellationToken cancellationToken)
{
while (true)
{
try
{
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1d));
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
var dnsClient = new DnsClient(this.fastGithubConfig.PureDns);
await dnsClient.Lookup(testDomain, RecordType.A, linkedTokenSource.Token);
break;
}
catch (Exception)
{
cancellationToken.ThrowIfCancellationRequested();
}
}
}
/// <summary> /// <summary>
/// 停止dnscrypt-proxy /// 停止dnscrypt-proxy

View File

@ -5,7 +5,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DNS" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" /> <ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -71,7 +71,7 @@ namespace FastGithub.ReverseProxy
} }
catch (Exception) catch (Exception)
{ {
this.logger.LogWarning($"由于{pureDns}解析{domain}失败,本次使用{fastDns}"); this.logger.LogTrace($"由于{pureDns}解析{domain}失败,本次使用{fastDns}");
return await LookupCoreAsync(fastDns, domain, cancellationToken); return await LookupCoreAsync(fastDns, domain, cancellationToken);
} }
} }

View File

@ -26,7 +26,6 @@ namespace FastGithub
{ {
return Host return Host
.CreateDefaultBuilder(args) .CreateDefaultBuilder(args)
.UseConsoleLifetime()
.UseWindowsService() .UseWindowsService()
.UseBinaryPathContentRoot() .UseBinaryPathContentRoot()
.UseDefaultServiceProvider(c => .UseDefaultServiceProvider(c =>