Skip to content

Commit 8f0ff91

Browse files
author
Meyn
committed
Fix descriptions
Add RequestContainer IRequest.StartRequestAsync()
1 parent 67bd80f commit 8f0ff91

21 files changed

+450
-284
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Shard
3+
Copyright (c) 2024 Shard
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
[![NuGet](https://img.shields.io/nuget/vpre/Shard.Requests)](https://www.nuget.org/packages/Shard.Requests) [![Downloads](https://img.shields.io/nuget/dt/Shard.Requests)](https://www.nuget.org/packages/Shard.Requests) [![License](https://img.shields.io/github/license/TypNull/requests.svg)](https://github.com/TypNull/requests/blob/master/LICENSE) ![Maintainability](https://img.shields.io/badge/Maintainability%20Index-86%25-brightgreen)
22
# Requests
3+
## 🌟 What Is Requests?
34

4-
Requests is a software library for C# .NET 6 that enables handling of requests in a parallel asynchronous state as Request objects.
5-
The library utilizes a priority channel to efficiently and systematically handle requests.
6-
The priority channel ensures that high-priority requests are processed before low-priority requests.
7-
This library is versatile and can be used for HTTP requests or other CPU-intensive tasks such as directory searching.
5+
**Requests** is library for C# .NET 6; it's your trusty sidekick in the world of handling requests. Imagine a friendly companion that takes care of your requests, ensuring they're processed efficiently and systematically. Whether you're dealing with HTTP requests or tackling CPU-intensive tasks like directory searching.
86

9-
It has been specifically designed to be flexible and customizable, allowing developers to tailor the library to their specific needs.
10-
Requests is an efficient and organized solution that simplifies the process of handling requests in C# .NET 6-based applications.
7+
## 🚀 Why Choose Requests?
118

12-
## Installation
9+
- **Priority Magic**: Our priority channel ensures that high-priority requests get the VIP treatment—they're processed before the rest. No more waiting in line!
10+
- **Flexibility at Its Best**: Requests is designed to be as flexible as possible. Customize it to fit your specific needs, whatever you're building.
11+
- **Parallel Asynchronous Awesomeness**: Handle requests in parallel, like a symphony of asynchronous harmony. 🎶
12+
13+
## 📦 Installation
14+
15+
Getting started with **Requests** is a breeze:
16+
1. Open your NuGet Package Manager.
17+
2. Search for "Shard.Requests"
18+
3. Install it. Voilà! 🎉
1319

14-
You can install Requests by searching for "Requests" in the NuGet Package Manager.
1520
- [Nuget](https://www.nuget.org/packages/Shard.Requests)
1621
- [GitHub](https://github.com/TypNull/Requests)
1722

1823
## Usage
1924

20-
To utilize the **Requests** library in C#, begin by importing it:
25+
To utilize the Requests library in C#, begin by importing it:
2126

2227
```csharp
2328
using Shard.Requests;
2429
```
2530

26-
Next, instantiate a `Request` object, and it will automatically be included in the `RequestHandler`. If a request encounters an error, the `RequestHandler` will automatically retry the request based on the specified retry settings. For additional information, refer to the Requests [Wiki](https://github.com/TypNull/Requests/wiki/).
31+
Next, instantiate a `Request` object, and it will automatically be included in the `RequestHandler`. If a request encounters an error, the `RequestHandler` will automatically retry the request based on the specified retry settings.
2732

2833
## Classes
2934

@@ -39,11 +44,14 @@ This library includes the following classes:
3944
- **ProgressableContainer:** A container class to merge requests together that are using a `Progress` object to report the progress.
4045
- **RequestHandler:** A class to handle requests. Every handler is independent of any other handler.
4146

42-
> Expand and use as you like!
47+
> Expand and use as you like!<br />
48+
> Because handling requests should be as delightful as a warm cup of cocoa on a winter day.
49+
50+
For additional information, refer to the Requests [Wiki](https://github.com/TypNull/Requests/wiki/).
4351

4452
## Examples
4553

46-
Here is an example of creating a child class of `Request`, called `OwnRequest`:
54+
Meet our star, the `OwnRequest` class:
4755

4856
```cs
4957
public class OwnRequest : Request<RequestOptions<VoidStruct, VoidStruct>, VoidStruct, VoidStruct>
@@ -62,7 +70,8 @@ public class OwnRequest : Request<RequestOptions<VoidStruct, VoidStruct>, VoidSt
6270
}
6371
}
6472
```
65-
To create an `OwnRequest`, use the following code:
73+
74+
OwnRequest is a straightforward implementation of a child class of Request. It doesn’t overwhelm you with complexity, but it’s incredibly useful for quick implementations:
6675

6776
```cs
6877
// Create an object and pass as a parameter an action that uses a CancellationToken
@@ -82,10 +91,15 @@ new OwnRequest(async (token) =>
8291
});
8392
```
8493

85-
## Contributing
94+
Create your own requests with a sprinkle of magic! ✨
95+
96+
## 🌟 Contributing
97+
98+
Join our quest! If you'd like to contribute to this library, submit a pull request or open an issue. We appreciate your help in making **Requests** the best it can be!
8699

87-
If you would like to contribute to this library, please submit a pull request or open an issue. We welcome all contributions and appreciate your help in making Requests the best library it can be!
100+
## 📜 License
88101

89-
## License
102+
**Requests** is licensed under the MIT license.
90103

91-
Requests is licensed under the MIT license.
104+
## **Free Code** and **Free to Use**
105+
#### Have fun!

Requests/Channel/AsyncOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private static void QueueUserWorkItem(Action<object?> action, object? state) =>
3939
Task.Factory.StartNew(action, state,
4040
CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
4141

42-
private static CancellationTokenRegistration UnsafeRegister(CancellationToken cancellationToken, Action<object?> action, object? state) =>
42+
private static CancellationTokenRegistration UnsafeRegister(Action<object?> action, object? state, CancellationToken cancellationToken) =>
4343
cancellationToken.Register(action, state);
4444
/// <summary>Registration with a provided cancellation token.</summary>
4545
private readonly CancellationTokenRegistration _registration;
@@ -92,11 +92,11 @@ public AsyncOperation(bool runContinuationsAsynchronously, bool pooled = false,
9292
if (cancellationToken.CanBeCanceled)
9393
{
9494
CancellationToken = cancellationToken;
95-
_registration = UnsafeRegister(cancellationToken, static s =>
95+
_registration = UnsafeRegister(static s =>
9696
{
9797
AsyncOperation<TResult> thisRef = (AsyncOperation<TResult>)s!;
9898
thisRef.TrySetCanceled(thisRef.CancellationToken);
99-
}, this);
99+
}, this, cancellationToken);
100100
}
101101
}
102102

Requests/Channel/ChannelUtilities.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
namespace Requests.Channel
44
{
5-
/// <summary>Provides internal helper methods for implementing channels.</summary>
5+
/// <summary>
6+
/// Provides helper methods for implementing channels.
7+
/// </summary>
68
internal static class ChannelUtilities
79
{
8-
/// <summary>Sentinel object used to indicate being done writing.</summary>
10+
/// <summary>
11+
/// A sentinel object used to indicate the completion of writing.
12+
/// </summary>
913
internal static readonly Exception s_doneWritingSentinel = new(nameof(s_doneWritingSentinel));
1014

11-
/// <summary>Completes the specified TaskCompletionSource.</summary>
12-
/// <param name="tcs">The source to complete.</param>
15+
/// <summary>
16+
/// Completes the specified TaskCompletionSource.
17+
/// </summary>
18+
/// <param name="tcs">The TaskCompletionSource to complete.</param>
1319
/// <param name="error">
14-
/// The optional exception with which to complete.
15-
/// If this is null or the DoneWritingSentinel, the source will be completed successfully.
20+
/// The optional exception to complete the TaskCompletionSource with.
21+
/// If this is null or the DoneWritingSentinel, the TaskCompletionSource will be completed successfully.
1622
/// If this is an OperationCanceledException, it'll be completed with the exception's token.
1723
/// Otherwise, it'll be completed as faulted with the exception.
1824
/// </param>
@@ -26,10 +32,12 @@ internal static void Complete(TaskCompletionSource tcs, Exception? error = null)
2632
tcs.TrySetResult();
2733
}
2834

29-
/// <summary>Gets a value task representing an error.</summary>
35+
/// <summary>
36+
/// Gets a ValueTask that represents an error.
37+
/// </summary>
3038
/// <typeparam name="T">Specifies the type of the value that would have been returned.</typeparam>
31-
/// <param name="error">The error. This may be <see cref="s_doneWritingSentinel"/>.</param>
32-
/// <returns>The failed task.</returns>
39+
/// <param name="error">The error. This may be <see cref="s_doneWritingSentinel"/>.</param>
40+
/// <returns>The failed ValueTask.</returns>
3341
internal static ValueTask<T> GetInvalidCompletionValueTask<T>(Exception error)
3442
{
3543
if (error == s_doneWritingSentinel)
@@ -39,11 +47,12 @@ internal static ValueTask<T> GetInvalidCompletionValueTask<T>(Exception error)
3947
return new(error is OperationCanceledException oce ? Task.FromCanceled<T>(oce.CancellationToken.IsCancellationRequested ? oce.CancellationToken : new CancellationToken(true)) : Task.FromException<T>(CreateInvalidCompletionException(error)));
4048

4149
}
50+
4251
/// <summary>
43-
/// Sets the Queue waite if it is null
52+
/// Queues a waiter if it is null.
4453
/// </summary>
45-
/// <param name="tail"></param>
46-
/// <param name="waiter"></param>
54+
/// <param name="tail">The tail of the queue.</param>
55+
/// <param name="waiter">The waiter to queue.</param>
4756
internal static void QueueWaiter(ref AsyncOperation<bool>? tail, AsyncOperation<bool> waiter)
4857
{
4958
AsyncOperation<bool>? c = tail;
@@ -58,11 +67,11 @@ internal static void QueueWaiter(ref AsyncOperation<bool>? tail, AsyncOperation<
5867
}
5968

6069
/// <summary>
61-
/// Goes throu all asyncOperation waiters and sets the result
70+
/// Iterates through all AsyncOperation waiters and sets the result.
6271
/// </summary>
63-
/// <param name="listTail"></param>
64-
/// <param name="result"></param>
65-
/// <param name="error"></param>
72+
/// <param name="listTail">The tail of the list.</param>
73+
/// <param name="result">The result to set.</param>
74+
/// <param name="error">The error to set, if any.</param>
6675
internal static void WakeUpWaiters(ref AsyncOperation<bool>? listTail, bool result, Exception? error = null)
6776
{
6877
AsyncOperation<bool>? tail = listTail;
@@ -82,16 +91,21 @@ internal static void WakeUpWaiters(ref AsyncOperation<bool>? listTail, bool resu
8291
}
8392
}
8493

85-
/// <summary>Removes all operations from the queue, failing each.</summary>
86-
/// <param name="operations">The queue of operations to complete.</param>
87-
/// <param name="error">The error with which to complete each operations.</param>
94+
/// <summary>
95+
/// Removes all operations from the queue and fails each one.
96+
/// </summary>
97+
/// <param name="operations">The queue of operations to be completed.</param>
98+
/// <param name="error">The error to complete each operation with.</param>
8899
internal static void FailOperations<T, TInner>(Deque<T> operations, Exception error) where T : AsyncOperation<TInner>
89100
{
90101
while (!operations.IsEmpty)
91102
operations.DequeueHead().TrySetException(error);
92103
}
93104

94-
/// <summary>Creates and returns an exception object to indicate that a channel has been closed.</summary>
105+
/// <summary>
106+
/// Creates and returns an exception to indicate that a channel has been closed.
107+
/// </summary>
108+
/// <param name="inner">Optional inner exception. If it's an OperationCanceledException, it's returned as is. If it's not null and not DoneWritingSentinel, a new ChannelClosedException is created with it. Otherwise, a new ChannelClosedException is created without an inner exception.</param>
95109
internal static Exception CreateInvalidCompletionException(Exception? inner = null) =>
96110
inner is OperationCanceledException ? inner :
97111
inner != null && inner != s_doneWritingSentinel ? new ChannelClosedException(inner) :

Requests/Channel/Deque.cs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,46 @@
33
// Licensed to the .NET Foundation under one or more agreements.
44
// The .NET Foundation licenses this file to you under the MIT license
55

6-
/// <summary>Provides a double-ended queue data structure.</summary>
7-
/// <typeparam name="T">Type of the data stored in the dequeue.</typeparam>
6+
/// <summary>
7+
/// Represents a double-ended queue (deque) data structure.
8+
/// </summary>
9+
/// <typeparam name="T">Specifies the type of elements in the deque.</typeparam>
810
internal class Deque<T>
911
{
12+
/// <summary>
13+
/// The array to hold the elements of the deque.
14+
/// </summary>
1015
private T[] _array = Array.Empty<T>();
11-
private int _head; // First valid element in the queue
12-
private int _tail; // First open slot in the dequeue, unless the dequeue is full
13-
private int _size; // Number of elements.
1416

17+
/// <summary>
18+
/// The position of the first valid element in the deque.
19+
/// </summary>
20+
private int _head;
21+
22+
/// <summary>
23+
/// The position of the first open slot in the deque, unless the deque is full.
24+
/// </summary>
25+
private int _tail;
26+
27+
/// <summary>
28+
/// The number of elements in the deque.
29+
/// </summary>
30+
private int _size;
31+
32+
/// <summary>
33+
/// Gets the number of elements contained in the <see cref="Deque{T}"/>.
34+
/// </summary>
1535
public int Count => _size;
1636

37+
/// <summary>
38+
/// Gets a value indicating whether the <see cref="Deque{T}"/> is empty.
39+
/// </summary>
1740
public bool IsEmpty => _size == 0;
1841

42+
/// <summary>
43+
/// Adds an item to the tail of the <see cref="Deque{T}"/>.
44+
/// </summary>
45+
/// <param name="item">The object to add to the <see cref="Deque{T}"/>.</param>
1946
public void EnqueueTail(T item)
2047
{
2148
if (_size == _array.Length)
@@ -27,6 +54,10 @@ public void EnqueueTail(T item)
2754
_size++;
2855
}
2956

57+
/// <summary>
58+
/// Removes and returns the object at the head of the <see cref="Deque{T}"/>.
59+
/// </summary>
60+
/// <returns>The object that is removed from the head of the <see cref="Deque{T}"/>.</returns>
3061
public T DequeueHead()
3162
{
3263
T item = _array[_head];
@@ -38,9 +69,17 @@ public T DequeueHead()
3869

3970
return item;
4071
}
41-
public T PeekHead() => _array[_head];
4272

73+
/// <summary>
74+
/// Returns the object at the head of the <see cref="Deque{T}"/> without removing it.
75+
/// </summary>
76+
/// <returns>The object at the head of the <see cref="Deque{T}"/>.</returns>
77+
public T PeekHead() => _array[_head];
4378

79+
/// <summary>
80+
/// Returns the object at the tail of the <see cref="Deque{T}"/> without removing it.
81+
/// </summary>
82+
/// <returns>The object at the tail of the <see cref="Deque{T}"/>.</returns>
4483
public T PeekTail()
4584
{
4685
int index = _tail - 1;
@@ -49,6 +88,10 @@ public T PeekTail()
4988
return _array[index];
5089
}
5190

91+
/// <summary>
92+
/// Removes and returns the object at the tail of the <see cref="Deque{T}"/>.
93+
/// </summary>
94+
/// <returns>The object that is removed from the tail of the <see cref="Deque{T}"/>.</returns>
5295
public T DequeueTail()
5396
{
5497
if (--_tail == -1)
@@ -61,7 +104,11 @@ public T DequeueTail()
61104
return item;
62105
}
63106

64-
public IEnumerator<T> GetEnumerator() // meant for debug purposes only
107+
/// <summary>
108+
/// Returns an enumerator that iterates through the <see cref="Deque{T}"/>.
109+
/// </summary>
110+
/// <returns>An enumerator for the <see cref="Deque{T}"/>.</returns>
111+
public IEnumerator<T> GetEnumerator()
65112
{
66113
int pos = _head;
67114
int count = _size;
@@ -72,6 +119,9 @@ public T DequeueTail()
72119
}
73120
}
74121

122+
/// <summary>
123+
/// Increases the capacity of the <see cref="Deque{T}"/> to accommodate additional elements.
124+
/// </summary>
75125
private void Grow()
76126
{
77127
const int MinimumGrow = 4;

Requests/Channel/ParallelChannelOptions.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
namespace Requests.Channel
22
{
33
/// <summary>
4-
/// Stores options that configure the degree of max parallism in for the channel
4+
/// Represents a configuration class for setting the maximum degree of parallelism in a channel.
55
/// </summary>
66
public class ParallelChannelOptions : ParallelOptions
77
{
8+
/// <summary>
9+
/// The maximum degree of parallelism for the channel.
10+
/// </summary>
811
private int _maxDegreeOfParallelism = Environment.ProcessorCount;
912

1013
/// <summary>
11-
/// Event that notifys the changed degree of parallelism
14+
/// Occurs when the maximum degree of parallelism changes.
1215
/// </summary>
1316
public event EventHandler<int>? DegreeOfParallelismChangedDelta;
1417

1518
/// <summary>
16-
/// Pause token
19+
/// Gets or sets the pause token that can be used to easily end the process.
1720
/// </summary>
1821
public PauseToken EasyEndToken { get; set; }
1922

2023
/// <summary>
21-
/// Main contructor
24+
/// Initializes a new instance of the <see cref="ParallelChannelOptions"/> class.
2225
/// </summary>
2326
public ParallelChannelOptions() => base.MaxDegreeOfParallelism = int.MaxValue;
2427

2528
/// <summary>
26-
/// Maximal degree of parallelism of the channel
29+
/// Gets or sets the maximum degree of parallelism for the channel.
2730
/// </summary>
31+
/// <value>
32+
/// The maximum degree of parallelism.
33+
/// </value>
34+
/// <exception cref="ArgumentOutOfRangeException">
35+
/// Thrown when the value is less than zero.
36+
/// </exception>
2837
public new int MaxDegreeOfParallelism
2938
{
3039
get => _maxDegreeOfParallelism;

0 commit comments

Comments
 (0)