Class AsyncUpdateThrottle<T>
- Namespace
- Kampute.HttpClient.Utilities
- Assembly
- Kampute.HttpClient.dll
Manages thread-safe, asynchronous updates to a value, ensuring efficiency by reducing unnecessary update operations.
public sealed class AsyncUpdateThrottle<T> : IDisposable
Type Parameters
T
The type of the value to be managed, preferably immutable for thread safety.
- Inheritance
-
AsyncUpdateThrottle<T>
- Implements
- Inherited Members
Remarks
This class provides a mechanism to update a value asynchronously while ensuring that updates are serialized and efficient. It is designed to prevent multiple, concurrent update operations from being processed if they are requested in quick succession. By employing a timing check before applying updates, the class ensures that only necessary updates proceed when the value has not been recently updated, making it ideal for scenarios where collecting or calculating the updated value is resource-intensive or costly.
Constructors
AsyncUpdateThrottle()
Initializes a new instance of the AsyncUpdateThrottle<T> class with a default value.
public AsyncUpdateThrottle()
AsyncUpdateThrottle(T?)
Initializes a new instance of the AsyncUpdateThrottle<T> class with a specified value.
public AsyncUpdateThrottle(T? initialValue)
Parameters
initialValue
TThe initial value of the type
T
.
Properties
LastUpdateTime
Gets the time of the last successful update operation.
public DateTimeOffset LastUpdateTime { get; }
Property Value
- DateTimeOffset
The time when the last successful update to the value was made. If no update has been applied, the value is MinValue.
Value
Gets the current value.
public T? Value { get; }
Property Value
- T
The current value of type
T
. This value is thread-safe to access and represents the most recent state managed by the AsyncUpdateThrottle<T>.
Methods
Dispose()
Releases all resources used by the AsyncUpdateThrottle<T>.
public void Dispose()
TryUpdateAsync(Func<Task<T?>>, CancellationToken)
Attempts to update the value asynchronously using the provided updater function.
public Task<bool> TryUpdateAsync(Func<Task<T?>> asyncUpdater, CancellationToken cancellationToken = default)
Parameters
asyncUpdater
Func<Task<T>>The asynchronous function used to update the value.
cancellationToken
CancellationTokenA token for canceling the operation.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains a boolean value that indicates whether the value was updated.
Remarks
This method allows for a thread-safe update of the value. The update will only be applied if no other update has been completed since this update attempt was initiated, preventing unnecessary updates or overwrites by concurrent operations.
If the update proceeds and is successful, the method returns true; if another update has already been applied, it returns false. This behavior ensures that the value reflects the most recent update attempt that was actually needed.
Exceptions
- ArgumentNullException
Thrown if
asyncUpdater
is null.- OperationCanceledException
Thrown if the operation is canceled via the cancellation token.