-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(IPRegion): use async increase load speed #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// 默认 IP2Region 实现 | ||
| /// 默认 IP2Region 实现 | ||
| /// </summary> | ||
| class IP2RegionService : DefaultIpLocatorProvider | ||
| { | ||
|
|
@@ -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(); | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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.