Skip to content

Add ResizableSemaphore#71

Merged
Tyrrrz merged 12 commits intoprimefrom
copilot/add-resizable-semaphore-utility
Apr 19, 2026
Merged

Add ResizableSemaphore#71
Tyrrrz merged 12 commits intoprimefrom
copilot/add-resizable-semaphore-utility

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 19, 2026

Ports ResizableSemaphore from YoutubeDownloader into PowerKit — a semaphore whose max concurrency count can be adjusted at runtime.

Implementation

  • PowerKit/ResizableSemaphore.cs

    • #if NET40_OR_GREATER || NETSTANDARD || NET file guard
    • Lock + EnterScope() (PolyShim) instead of object + lock
    • Refresh() acquires _lock itself; all callers release the lock before calling Refresh(), avoiding re-entrancy issues with .NET 9's non-reentrant System.Threading.Lock
    • _maxCount backing field inlined using C# 13 field keyword; getter is lock-free (get => field) so Refresh() can read MaxCount without re-entering the lock
    • AcquiredAccess file class replaced with Disposable.Create(Release) — no separate type needed
    • await using on CancellationTokenRegistration replaced with using varIAsyncDisposable on that type is .NET 6+ only
    • ConfigureAwait(false) on the awaited task
    • Two partial class declarations merged into one
  • PowerKit.Tests/ResizableSemaphoreTests.cs — 4 consolidated tests covering blocking at limit, cancellation, MaxCount changes unblocking waiters, and disposal behaviour

Usage

using var semaphore = new ResizableSemaphore { MaxCount = 3 };

// Blocks if 3 accesses are already active
using var access = await semaphore.AcquireAsync(cancellationToken);
// ... do work ...
// access released on dispose

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/22a8dd53-6d26-41cf-8f63-f07663947b4d

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
@Tyrrrz Tyrrrz added the enhancement New feature or request label Apr 19, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review April 19, 2026 10:20
Copilot AI review requested due to automatic review settings April 19, 2026 10:20
Comment thread PowerKit.Tests/ResizableSemaphoreTests.cs
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/cd5ae709-0ce3-4397-b725-d8a7a9f7938f

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a ResizableSemaphore concurrency primitive to PowerKit, allowing the maximum concurrency level to be adjusted at runtime.

Changes:

  • Introduces ResizableSemaphore with async acquire/release semantics and dynamic MaxCount updates.
  • Adds unit tests covering acquisition limits, blocking behavior, cancellation, disposal behavior, and unblocking via MaxCount changes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
PowerKit/ResizableSemaphore.cs Implements the resizable semaphore and its disposable access handle.
PowerKit.Tests/ResizableSemaphoreTests.cs Adds behavioral tests for acquire/release, cancellation, disposal, and resizing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/ResizableSemaphore.cs
Comment thread PowerKit/ResizableSemaphore.cs
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Comment thread PowerKit.Tests/ResizableSemaphoreTests.cs Outdated
Comment thread PowerKit/ResizableSemaphore.cs Outdated
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/d0027c65-1201-42d6-b9da-3b4dd425d371

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 19, 2026

@copilot ResizableSemaphoreAccess can be replaced with Disposable.Create

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 19, 2026

Codecov Report

❌ Patch coverage is 96.49123% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.99%. Comparing base (35b770e) to head (da409cf).
⚠️ Report is 2 commits behind head on prime.

Files with missing lines Patch % Lines
PowerKit/ResizableSemaphore.cs 94.87% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            prime      #71      +/-   ##
==========================================
+ Coverage   93.93%   93.99%   +0.06%     
==========================================
  Files         130      132       +2     
  Lines        2557     2616      +59     
  Branches      202      205       +3     
==========================================
+ Hits         2402     2459      +57     
- Misses        112      113       +1     
- Partials       43       44       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread PowerKit.Tests/ResizableSemaphoreTests.cs
Copilot AI and others added 3 commits April 19, 2026 11:19
…e-semaphore-utility

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 19, 2026

@copilot ResizableSemaphoreAccess can be replaced with Disposable.Create

ResizableSemaphoreAccess was already replaced with Disposable.Create(Release) in a previous commit (abfff4b). No ResizableSemaphoreAccess type remains in the code.

Copilot AI requested a review from Tyrrrz April 19, 2026 11:21
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 19, 2026

@copilot fix formatting please

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 19, 2026

@copilot fix formatting please

Fixed in 1a5d6a5. CSharpier collapsed the multiline cancellationToken.Register(...) call to a single line, and restored the null-forgiving ! on waiter!.TrySetResult() (needed for netstandard2.0). Build now passes cleanly.

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.

3 participants