Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.2</Version>
<Version>9.0.3</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace BootstrapBlazor.Components;

/// <summary>
/// 默认 IP2Region 实现
/// 默认 IP2Region 实现
/// </summary>
class IP2RegionService : DefaultIpLocatorProvider
{
Expand All @@ -25,26 +25,29 @@ public IP2RegionService(IOptions<BootstrapBlazorOptions> options, IOptions<IP2Re
_ipOptions = ipRegionOptions;
_logger = logger;

InitSearch();
Task.Run(InitSearch, CancellationToken.None).ConfigureAwait(false);
}

private readonly IOptions<BootstrapBlazorOptions> _options;
private readonly IOptions<IP2RegionOptions> _ipOptions;
private readonly ILogger<IP2RegionService> _logger;
private readonly TaskCompletionSource _tcs = new();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Specify the generic type for TaskCompletionSource for type safety.

Use TaskCompletionSource<object?> or TaskCompletionSource as appropriate to ensure type safety and prevent misuse.

Suggested change
private readonly TaskCompletionSource _tcs = new();
private readonly TaskCompletionSource<bool> _tcs = new();

private Searcher? _search;

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="ip"></param>
protected override Task<string?> LocateByIp(string ip)
protected override async Task<string?> LocateByIp(string ip)
{
await _tcs.Task;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Consider handling exceptions from _tcs.Task to avoid unhandled exceptions in LocateByIp.

If InitSearch sets an exception on _tcs, awaiting _tcs.Task will throw in LocateByIp. Confirm this is intentional, or catch and log the exception to avoid unexpected failures.


string? result = null;
if (_search != null && _options.Value.WebClientOptions.EnableIpLocator)
{
result = _search.Search(ip);
}
return Task.FromResult(result);
return result;
}

private void InitSearch()
Expand All @@ -60,9 +63,11 @@ private void InitSearch()
{
_search = new Searcher(CachePolicy.Content, xdbPath);
_logger.LogInformation("IP2Region xdb file {dbPath} loaded", xdbPath);
_tcs.TrySetResult();
}
catch (Exception ex)
{
_tcs.TrySetException(ex);
_logger.LogError(ex, "IP2Region xdb file path: {dbPath}", xdbPath);
}
}
Expand Down