diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-01-31 08:13:28 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-01-31 08:13:28 -0600 |
| commit | a1cd5351ca77bb20f74f1877a26d809f75e2f0dc (patch) | |
| tree | 4ef7df6dd4cda1d468c15b0c043a135f352227f2 /src/slskd/Governor.cs | |
| parent | e6ca0002ca73ee5e6ba87fdc669d5c0c3e8327a7 (diff) | |
| download | slskd-a1cd5351ca77bb20f74f1877a26d809f75e2f0dc.tar.gz | |
clean up and move Governor
Diffstat (limited to 'src/slskd/Governor.cs')
| -rw-r--r-- | src/slskd/Governor.cs | 93 |
1 files changed, 0 insertions, 93 deletions
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 file="Governor.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> - -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<int> 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<Options> optionsMonitor) - { - UserService = userService; - - OptionsMonitor = optionsMonitor; - OptionsMonitor.OnChange(Configure); - - Configure(OptionsMonitor.CurrentValue); - } - - private IUserService UserService { get; } - private IOptionsMonitor<Options> OptionsMonitor { get; } - private ILogger Log { get; set; } = Serilog.Log.ForContext<Governor>(); - private ITokenBucket DefaultTokenBucket { get; set; } - private Dictionary<string, ITokenBucket> TokenBuckets { get; set; } = new Dictionary<string, ITokenBucket>(); - - public Task<int> 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<string, ITokenBucket>(); - - 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); - } - } -} |
