diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-02-18 07:05:03 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-18 07:05:03 -0600 |
| commit | fccc9c6440af11fd400f3d1dee243d2d18ea1651 (patch) | |
| tree | 0ba68479205c192e630c985f5025c67d2ca4c715 /src/slskd/Common | |
| parent | dee1b7315ff56194f7868803b876cabe4ab165d7 (diff) | |
| parent | 58e865993eb507e31dca3771fac0ea3829880d02 (diff) | |
| download | slskd-fccc9c6440af11fd400f3d1dee243d2d18ea1651.tar.gz | |
Merge pull request #346 from slskd/groups
Add upload slot and bandwidth limiting for user groups
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 19 | ||||
| -rw-r--r-- | src/slskd/Common/TokenBucket.cs | 228 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/ValidateAttribute.cs | 26 |
3 files changed, 273 insertions, 0 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 90f0b659..8b1ead8c 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -18,12 +18,14 @@ namespace slskd { using System; + using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Sockets; using System.Numerics; using System.Reflection; + using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -308,6 +310,23 @@ namespace slskd } /// <summary> + /// Determines whether the given object is a <see cref="Dictionary{TKey, TValue}"/>. + /// </summary> + /// <param name="obj">The object to check.</param> + /// <returns>A value indicating whether the given object is a dictionary.</returns> + public static bool IsDictionary(this object obj) + { + if (obj == null) + { + return false; + } + + return obj is IDictionary && + obj.GetType().IsGenericType && + obj.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(Dictionary<,>)); + } + + /// <summary> /// Redacts this instance of Options, replacing properties marked with <see cref="SecretAttribute"/> with '*****'. /// </summary> /// <remarks> diff --git a/src/slskd/Common/TokenBucket.cs b/src/slskd/Common/TokenBucket.cs new file mode 100644 index 00000000..51a0281b --- /dev/null +++ b/src/slskd/Common/TokenBucket.cs @@ -0,0 +1,228 @@ +// <copyright file="TokenBucket.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + using System.Threading; + using System.Threading.Tasks; + + /// <summary> + /// Implements the 'token bucket' or 'leaky bucket' rate limiting algorithm. + /// </summary> + public interface ITokenBucket + { + /// <summary> + /// Gets the bucket capacity. + /// </summary> + public long Capacity { get; } + + /// <summary> + /// Asynchronously retrieves the specified token <paramref name="count"/> from the bucket. + /// </summary> + /// <remarks> + /// <para> + /// If the requested <paramref name="count"/> exceeds the bucket <see cref="Capacity"/>, the request is lowered to + /// the capacity of the bucket. + /// </para> + /// <para>If the bucket has tokens available, but fewer than the requested amount, the available tokens are returned.</para> + /// <para> + /// If the bucket has no tokens available, execution waits for the bucket to be replenished before servicing the request. + /// </para> + /// </remarks> + /// <param name="count">The number of tokens to retrieve.</param> + /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> + /// <returns>A Task that completes when tokens have been provided.</returns> + Task<int> GetAsync(int count, CancellationToken cancellationToken = default); + + /// <summary> + /// Returns the specified token <paramref name="count"/> to the bucket. + /// </summary> + /// <remarks> + /// <para>This method should only be called if tokens were retrieved from the bucket, but were not used.</para> + /// <para> + /// If the specified count exceeds the bucket capacity, the count is lowered to the capacity. Effectively this + /// allows the bucket to 'burst' up to 2x capacity to 'catch up' to the desired rate if tokens were wastefully + /// retrieved. + /// </para> + /// <para>If the specified count is negative, no change is made to the available count.</para> + /// </remarks> + /// <param name="count">The number of tokens to return.</param> + void Return(int count); + + /// <summary> + /// Sets the bucket capacity to the supplied <paramref name="capacity"/>. + /// </summary> + /// <remarks>Change takes effect on the next reset.</remarks> + /// <param name="capacity">The bucket capacity.</param> + void SetCapacity(long capacity); + } + + /// <summary> + /// Implements the 'token bucket' or 'leaky bucket' rate limiting algorithm. + /// </summary> + public class TokenBucket : ITokenBucket, IDisposable + { + private TaskCompletionSource<bool> waitForReset = new(); + + /// <summary> + /// Initializes a new instance of the <see cref="TokenBucket"/> class. + /// </summary> + /// <param name="capacity">The bucket capacity.</param> + /// <param name="interval">The interval at which tokens are replenished.</param> + public TokenBucket(long capacity, int interval) + { + if (capacity < 1) + { + throw new ArgumentOutOfRangeException(nameof(capacity), "Bucket capacity must be greater than or equal to 1"); + } + + if (interval < 1) + { + throw new ArgumentOutOfRangeException(nameof(interval), "Interval must be greater than or equal to 1"); + } + + Capacity = capacity; + CurrentCount = Capacity; + + Clock = new System.Timers.Timer(interval); + Clock.Elapsed += (sender, e) => Reset(); + Clock.Start(); + } + + /// <summary> + /// Gets the bucket capacity. + /// </summary> + public long Capacity { get; private set; } + + private System.Timers.Timer Clock { get; set; } + private long CurrentCount { get; set; } + private bool Disposed { get; set; } + private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + + /// <summary> + /// Disposes this instance. + /// </summary> + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + /// <summary> + /// Asynchronously retrieves the specified token <paramref name="count"/> from the bucket. + /// </summary> + /// <remarks> + /// <para> + /// If the requested <paramref name="count"/> exceeds the bucket <see cref="Capacity"/>, the request is lowered to + /// the capacity of the bucket. + /// </para> + /// <para>If the bucket has tokens available, but fewer than the requested amount, the available tokens are returned.</para> + /// <para> + /// If the bucket has no tokens available, execution waits for the bucket to be replenished before servicing the request. + /// </para> + /// </remarks> + /// <param name="count">The number of tokens to retrieve.</param> + /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> + /// <returns>A Task that completes when tokens have been provided.</returns> + public Task<int> GetAsync(int count, CancellationToken cancellationToken = default) + { + return GetInternalAsync(Math.Min(count, (int)Math.Min(int.MaxValue, Capacity)), cancellationToken); + } + + /// <summary> + /// Returns the specified token <paramref name="count"/> to the bucket. + /// </summary> + /// <remarks> + /// <para>This method should only be called if tokens were retrieved from the bucket, but were not used.</para> + /// <para> + /// If the specified count exceeds the bucket capacity, the count is lowered to the capacity. Effectively this + /// allows the bucket to 'burst' up to 2x capacity to 'catch up' to the desired rate if tokens were wastefully + /// retrieved. + /// </para> + /// <para>If the specified count is negative, no change is made to the available count.</para> + /// </remarks> + /// <param name="count">The number of tokens to return.</param> + public void Return(int count) + { + CurrentCount += Math.Min(Math.Max(count, 0), Capacity); + } + + /// <summary> + /// Sets the bucket capacity to the supplied <paramref name="capacity"/>. + /// </summary> + /// <remarks>Change takes effect on the next reset.</remarks> + /// <param name="capacity">The bucket capacity.</param> + public void SetCapacity(long capacity) + { + if (capacity < 1) + { + throw new ArgumentOutOfRangeException(nameof(capacity), "Bucket capacity must be greater than or equal to 1"); + } + + Capacity = capacity; + } + + /// <summary> + /// Disposes this instance. + /// </summary> + /// <param name="disposing">A value indicating whether disposal is in progress.</param> + protected virtual void Dispose(bool disposing) + { + if (!Disposed) + { + if (disposing) + { + Clock.Dispose(); + SyncRoot.Dispose(); + } + + Disposed = true; + } + } + + private async Task<int> GetInternalAsync(int count, CancellationToken cancellationToken = default) + { + await SyncRoot.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + // if the bucket is empty, wait for a reset, then replenish it before continuing + // this ensures tokens are distributed in the order in which callers obtain the semaphore, + // which is as close to a FIFO as .NET synchronization primitives will allow + if (CurrentCount == 0) + { + await waitForReset.Task.ConfigureAwait(false); + CurrentCount = Capacity; + } + + // take the minimum of requested count or CurrentCount, deduct it from + // CurrentCount (potentially zeroing the bucket), and return it + var availableCount = Math.Min(CurrentCount, count); + CurrentCount -= availableCount; + return (int)availableCount; + } + finally + { + SyncRoot.Release(); + } + } + + private void Reset() + => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource<bool>()).SetResult(true); + } +} diff --git a/src/slskd/Common/Validation/ValidateAttribute.cs b/src/slskd/Common/Validation/ValidateAttribute.cs index d731eb0e..c7efaa92 100644 --- a/src/slskd/Common/Validation/ValidateAttribute.cs +++ b/src/slskd/Common/Validation/ValidateAttribute.cs @@ -17,8 +17,10 @@ namespace slskd.Validation { + using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; + using System.Linq; /// <summary> /// Indicates that attributed properties should be recursively validated. @@ -35,6 +37,30 @@ namespace slskd.Validation var results = new List<ValidationResult>(); var context = new ValidationContext(value, null, null); + if (value.IsDictionary()) + { + var compositeResults = new CompositeValidationResult(validationContext.DisplayName); + + // given the context in this application, key type should never be anything but a string + foreach (string key in ((IDictionary)value).Keys) + { + var val = ((IDictionary)value)[key]; + var result = IsValid(val, new ValidationContext(val, null, null)); + + if (result != ValidationResult.Success) + { + compositeResults.AddResult(new CompositeValidationResult(key, new[] { result }.ToList())); + } + } + + if (compositeResults.Results.Any()) + { + return compositeResults; + } + + return ValidationResult.Success; + } + Validator.TryValidateObject(value, context, results, true); if (results.Count != 0) |
