
Punchclock is the low-level scheduling and prioritization library used by Fusillade to orchestrate pending concurrent operations.
Ok, so you've got a shiny mobile phone app and you've got async/await. Awesome! It's so easy to issue network requests, why not do it all the time? After your users one-:star2: you for your app being slow, you discover that you're issuing way too many requests at the same time.
Then, you try to manage issuing less requests by hand, and it becomes a spaghetti mess as different parts of your app reach into each other to try to figure out who's doing what. Let's figure out a better way.
- Bounded concurrency with a priority-aware semaphore
- Priority scheduling (higher number runs first)
- Key-based serialization (only one operation per key runs at a time)
- Pause/resume with reference counting
- Cancellation via CancellationToken or IObservable
- Task and IObservable friendly API
- NuGet:
dotnet add package Punchclock
using Punchclock;
using System.Net.Http;
var queue = new OperationQueue(maximumConcurrent: 2);
var http = new HttpClient();
// Fire a bunch of downloads – only two will run at a time
var t1 = queue.Enqueue(1, () => http.GetStringAsync("https://example.com/a"));
var t2 = queue.Enqueue(1, () => http.GetStringAsync("https://example.com/b"));
var t3 = queue.Enqueue(1, () => http.GetStringAsync("https://example.com/c"));
await Task.WhenAll(t1, t2, t3);
- Higher numbers win. A priority 10 operation will preempt priority 1 when a slot opens.
await queue.Enqueue(10, () => http.GetStringAsync("https://example.com/urgent"));
- Use a key to ensure only one operation for that key runs at a time.
- Useful to avoid thundering herds against the same resource.
// These will run one-after-another because they share the same key
var k1 = queue.Enqueue(1, key: "user:42", () => LoadUserAsync(42));
var k2 = queue.Enqueue(1, key: "user:42", () => LoadUserPostsAsync(42));
await Task.WhenAll(k1, k2);
- Via CancellationToken:
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
await queue.Enqueue(1, key: "img:1", () => DownloadImageAsync("/1"), cts.Token);
- Via IObservable cancellation signal:
var cancel = new Subject<Unit>();
var obs = queue.EnqueueObservableOperation(1, "slow", cancel, () => Expensive().ToObservable());
cancel.OnNext(Unit.Default); // cancels if not yet running or in-flight
var gate = queue.PauseQueue();
// enqueue work while paused; nothing executes yet
// ...
gate.Dispose(); // resumes and drains respecting priority/keys
queue.SetMaximumConcurrent(8); // increases throughput
await queue.ShutdownQueue(); // completes when outstanding work finishes
-
OperationQueue
- ctor(int maximumConcurrent = 4)
- IObservable EnqueueObservableOperation(int priority, Func<IObservable>)
- IObservable EnqueueObservableOperation(int priority, string key, Func<IObservable>)
- IObservable EnqueueObservableOperation<T, TDontCare>(int priority, string key, IObservable cancel, Func<IObservable>)
- IDisposable PauseQueue()
- void SetMaximumConcurrent(int maximumConcurrent)
- IObservable ShutdownQueue()
-
OperationQueueExtensions
- Task Enqueue(int priority, Func)
- Task Enqueue(int priority, Func<Task>)
- Task Enqueue(int priority, string key, Func)
- Task Enqueue(int priority, string key, Func<Task>)
- Overloads with CancellationToken for all of the above
- Prefer Task-based Enqueue APIs in application code; use observable APIs when composing with Rx.
- Use descriptive keys for shared resources (e.g., "user:{id}", "file:{path}").
- Keep operations idempotent and short; long operations block concurrency slots.
- Use higher priorities sparingly; they jump the queue when a slot opens.
- PauseQueue is ref-counted; always dispose the returned handle exactly once.
- For cancellation via token, reuse CTS per user action to cancel pending work quickly.
- Unkeyed work is prioritized ahead of keyed work internally to keep the pipeline flowing; keys are serialized per group.
- The semaphore releases when an operation completes, errors, or is canceled.
- Cancellation before evaluation prevents invoking the supplied function.
- Image downloader with keys and priorities
var queue = new OperationQueue(3);
Task Download(string url, string dest, int pri, string key) =>
queue.Enqueue(pri, key, async () =>
{
using var http = new HttpClient();
var bytes = await http.GetByteArrayAsync(url);
await File.WriteAllBytesAsync(dest, bytes);
});
var tasks = new[]
{
Download("https://example.com/a.jpg", "a.jpg", 1, "img"),
Download("https://example.com/b.jpg", "b.jpg", 1, "img"),
queue.Enqueue(5, () => Task.Delay(100)), // higher priority misc work
};
await Task.WhenAll(tasks);
- Nothing runs? Ensure you didn't leave the queue paused. Dispose the token from PauseQueue.
- Starvation? Check if you assigned very high priorities to long-running tasks.
- Deadlock-like behavior with keys? Remember keyed operations are strictly serialized; avoid long critical sections.
Punchclock is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use. Because of our Open Collective model for funding and transparency, we are able to funnel support and funds through to our contributors and community. We ❤ the people who are involved in this project, and we’d love to have you on board, especially if you are just getting started or have never contributed to open-source before.
So here's to you, lovely person who wants to join us — this is how you can support us:
- Responding to questions on StackOverflow
- Passing on knowledge and teaching the next generation of developers
- Submitting documentation updates where you see fit or lacking.
- Making contributions to the code base.