Skip to content

feat(IPRegion): use async increase load speed#536

Merged
ArgoZhang merged 4 commits intomasterfrom
refactor-region
Aug 19, 2025
Merged

feat(IPRegion): use async increase load speed#536
ArgoZhang merged 4 commits intomasterfrom
refactor-region

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Aug 19, 2025

Link issues

fixes #535

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Offload IP2Region xdb file loading to a background task and update the IP lookup method to await initialization before returning results

New Features:

  • Load IP2Region database asynchronously to improve startup speed
  • Make LocateByIp asynchronous and defer lookups until initialization completes

@bb-auto bb-auto Bot added the enhancement New feature or request label Aug 19, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Aug 19, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Aug 19, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Introduce asynchronous initialization of IP2Region searcher to improve load performance by offloading the InitSearch process to a background task and synchronizing lookups via a TaskCompletionSource.

Sequence diagram for asynchronous IP2Region searcher initialization and lookup

sequenceDiagram
    participant Client
    participant IP2RegionService
    participant Searcher
    participant Logger

    Client->>IP2RegionService: LocateByIp(ip)
    IP2RegionService->>IP2RegionService: await _tcs.Task (wait for InitSearch)
    alt InitSearch successful
        IP2RegionService->>Searcher: Search(ip)
        Searcher-->>IP2RegionService: result
        IP2RegionService->>Client: return result
    else InitSearch failed
        IP2RegionService->>Client: return null
    end
Loading

File-Level Changes

Change Details Files
Asynchronous initialization of searcher
  • Invoke InitSearch via Task.Run in constructor
  • Add TaskCompletionSource to signal initialization completion
src/components/BootstrapBlazor.IP2Region/Services/IP2RegionService.cs
Async/await refactor of LocateByIp
  • Change method signature to async Task<string?>
  • Await TaskCompletionSource before executing the search
src/components/BootstrapBlazor.IP2Region/Services/IP2RegionService.cs
Completion signaling in InitSearch
  • Call _tcs.TrySetResult() on successful database load
  • Call _tcs.TrySetException(ex) on initialization failure
src/components/BootstrapBlazor.IP2Region/Services/IP2RegionService.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#535 Refactor IP2RegionService to use asynchronous initialization to increase load speed.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit e91fabf into master Aug 19, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the refactor-region branch August 19, 2025 05:20
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.IP2Region/Services/IP2RegionService.cs:34` </location>
<code_context>
     private readonly IOptions<BootstrapBlazorOptions> _options;
     private readonly IOptions<IP2RegionOptions> _ipOptions;
     private readonly ILogger<IP2RegionService> _logger;
+    private readonly TaskCompletionSource _tcs = new();
     private Searcher? _search;

</code_context>

<issue_to_address>
Specify the generic type for TaskCompletionSource for type safety.

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

<suggested_fix>
<<<<<<< SEARCH
    private readonly TaskCompletionSource _tcs = new();
=======
    private readonly TaskCompletionSource<bool> _tcs = new();
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/components/BootstrapBlazor.IP2Region/Services/IP2RegionService.cs:43` </location>
<code_context>
-    protected override Task<string?> LocateByIp(string ip)
+    protected override async Task<string?> LocateByIp(string ip)
     {
+        await _tcs.Task;
+
         string? result = null;
</code_context>

<issue_to_address>
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.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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();

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(IPRegion): use async increase load speed

1 participant