From a1cd5351ca77bb20f74f1877a26d809f75e2f0dc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 31 Jan 2022 08:13:28 -0600 Subject: clean up and move Governor --- src/slskd/Governor.cs | 93 ------------------------- src/slskd/Transfers/Governor.cs | 147 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 93 deletions(-) delete mode 100644 src/slskd/Governor.cs create mode 100644 src/slskd/Transfers/Governor.cs diff --git a/src/slskd/Governor.cs b/src/slskd/Governor.cs deleted file mode 100644 index 807fec71..00000000 --- a/src/slskd/Governor.cs +++ /dev/null @@ -1,93 +0,0 @@ -// -// 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/. -// - -using Microsoft.Extensions.Options; - -namespace slskd -{ - using System.Collections.Generic; - using System.Threading; - using System.Threading.Tasks; - using Serilog; - using slskd.Users; - using Soulseek; - - public interface IGovernor - { - Task GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken); - public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes); - } - - public class Governor : IGovernor - { - public Governor( - IUserService userService, - IOptionsMonitor optionsMonitor) - { - UserService = userService; - - OptionsMonitor = optionsMonitor; - OptionsMonitor.OnChange(Configure); - - Configure(OptionsMonitor.CurrentValue); - } - - private IUserService UserService { get; } - private IOptionsMonitor OptionsMonitor { get; } - private ILogger Log { get; set; } = Serilog.Log.ForContext(); - private ITokenBucket DefaultTokenBucket { get; set; } - private Dictionary TokenBuckets { get; set; } = new Dictionary(); - - public Task GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken) - { - var group = UserService.GetGroup(transfer.Username); - var bucket = TokenBuckets.GetValueOrDefault(group, DefaultTokenBucket); - - return bucket.GetAsync(requestedBytes, cancellationToken); - } - - public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes) - { - var group = UserService.GetGroup(transfer.Username); - var bucket = TokenBuckets.GetValueOrDefault(group, DefaultTokenBucket); - - // we don't have enough information to tell whether grantedBytes was reduced by the - // global limiter within Soulseek.NET, so we just return the bytes that we know for sure - // that were wasted, which is grantedBytes - actualBytes. - // example: we grant 1000 bytes. Soulseek.NET grants only 500. 250 bytes are written. - // ideally we would return 750 bytes, but instead we return 250. this discrepancy doesn't - // really matter because Soulseek.NET is the constraint in this scenario and the additional - // tokens we would return would never be used. - bucket.Return(grantedBytes - actualBytes); - } - - private void Configure(Options options) - { - DefaultTokenBucket = new TokenBucket((options.Groups.Default.Upload.SpeedLimit * 1024L) / 10, 100); - - var tokenBuckets = new Dictionary(); - - foreach (var group in options.Groups.UserDefined) - { - tokenBuckets.Add(group.Key, new TokenBucket((group.Value.Upload.SpeedLimit * 1024L) / 10, 100)); - } - - TokenBuckets = tokenBuckets; - Log.Debug("Reconfigured governor for {Count} groups", TokenBuckets.Count); - } - } -} diff --git a/src/slskd/Transfers/Governor.cs b/src/slskd/Transfers/Governor.cs new file mode 100644 index 00000000..f58815b4 --- /dev/null +++ b/src/slskd/Transfers/Governor.cs @@ -0,0 +1,147 @@ +// +// 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/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Transfers +{ + using System; + using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + using Serilog; + using slskd.Users; + using Soulseek; + + /// + /// Governs transfer speed. + /// + public interface IGovernor + { + /// + /// Asynchronously obtains a grant of for the specified . + /// + /// + /// This operation completes when any number of bytes can be granted. The amount returned may be smaller than the + /// requested amount. + /// + /// The transfer for which the grant is requested. + /// The number of requested bytes. + /// The token to monitor for cancellation. + /// The operation context, including the number of bytes granted. + Task GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken); + + /// + /// Returns wasted bytes for redistribution. + /// + /// The transfer which generated the waste. + /// The number of bytes that were attempted to be transferred. + /// The number of bytes granted by all governors in the system. + /// The actual number of bytes transferred. + public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes); + } + + /// + /// Governs transfer speed. + /// + public class Governor : IGovernor + { + /// + /// Initializes a new instance of the class. + /// + /// The UserService instance to use. + /// The OptionsMonitor instance to use. + public Governor( + IUserService userService, + IOptionsMonitor optionsMonitor) + { + Users = userService; + + OptionsMonitor = optionsMonitor; + OptionsMonitor.OnChange(Configure); + + Configure(OptionsMonitor.CurrentValue); + } + + private ITokenBucket DefaultTokenBucket { get; set; } = new TokenBucket((int.MaxValue * 1024L) / 10, 100); + private ILogger Log { get; } = Serilog.Log.ForContext(); + private IOptionsMonitor OptionsMonitor { get; } + private Dictionary TokenBuckets { get; set; } = new Dictionary(); + private IUserService Users { get; } + + /// + /// Asynchronously obtains a grant of for the specified . + /// + /// + /// This operation completes when any number of bytes can be granted. The amount returned may be smaller than the + /// requested amount. + /// + /// The transfer for which the grant is requested. + /// The number of requested bytes. + /// The token to monitor for cancellation. + /// The operation context, including the number of bytes granted. + public Task GetBytesAsync(Transfer transfer, int requestedBytes, CancellationToken cancellationToken) + { + var group = Users.GetGroup(transfer.Username); + var bucket = TokenBuckets.GetValueOrDefault(group, DefaultTokenBucket); + + return bucket.GetAsync(requestedBytes, cancellationToken); + } + + /// + /// Returns wasted bytes for redistribution. + /// + /// The transfer which generated the waste. + /// The number of bytes that were attempted to be transferred. + /// The number of bytes granted by all governors in the system. + /// The actual number of bytes transferred. + public void ReturnBytes(Transfer transfer, int attemptedBytes, int grantedBytes, int actualBytes) + { + var waste = Math.Min(0, grantedBytes - actualBytes); + + if (waste == 0) + { + return; + } + + var group = Users.GetGroup(transfer.Username); + var bucket = TokenBuckets.GetValueOrDefault(group, DefaultTokenBucket); + + // we don't have enough information to tell whether grantedBytes was reduced by the global limiter within + // Soulseek.NET, so we just return the bytes that we know for sure that were wasted, which is grantedBytes - actualBytes. + // example: we grant 1000 bytes. Soulseek.NET grants only 500. 250 bytes are written. ideally we would return 750 + // bytes, but instead we return 250. this discrepancy doesn't really matter because Soulseek.NET is the constraint in + // this scenario and the additional tokens we would return would never be used. + bucket.Return(waste); + } + + private void Configure(Options options) + { + DefaultTokenBucket = new TokenBucket((options.Groups.Default.Upload.SpeedLimit * 1024L) / 10, 100); + + var tokenBuckets = new Dictionary(); + + foreach (var group in options.Groups.UserDefined) + { + tokenBuckets.Add(group.Key, new TokenBucket((group.Value.Upload.SpeedLimit * 1024L) / 10, 100)); + } + + TokenBuckets = tokenBuckets; + Log.Debug("Reconfigured governor for {Count} groups", TokenBuckets.Count); + } + } +} \ No newline at end of file -- cgit v1.2.3