diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-01-30 21:10:45 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-01-30 21:10:45 -0600 |
| commit | d592695e40b23e4bffbea2651313c2f674a19ec3 (patch) | |
| tree | 9f13ee2424b07721bcd136dfe33c40407321b7f6 /src | |
| parent | ee21f4514305fb1ff213aae016817b7ff25d628d (diff) | |
| download | slskd-d592695e40b23e4bffbea2651313c2f674a19ec3.tar.gz | |
stub Governor
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 10 | ||||
| -rw-r--r-- | src/slskd/Governor.cs | 93 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 2 |
3 files changed, 104 insertions, 1 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f2d81471..1d3d1b33 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -68,6 +68,7 @@ namespace slskd IRoomService roomService, ISharedFileCache sharedFileCache, IPushbulletService pushbulletService, + IGovernor governor, IHubContext<ApplicationHub> applicationHub, IHubContext<LogsHub> logHub) { @@ -89,6 +90,8 @@ namespace slskd ConversationTracker = conversationTracker; Pushbullet = pushbulletService; + Governor = governor; + RoomService = roomService; ApplicationHub = applicationHub; @@ -125,6 +128,7 @@ namespace slskd private IRoomService RoomService { get; set; } private IBrowseTracker BrowseTracker { get; set; } private IConversationTracker ConversationTracker { get; set; } + private IGovernor Governor { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext<Application>(); private ConcurrentDictionary<string, ILogger> Loggers { get; } = new ConcurrentDictionary<string, ILogger>(); private Options Options => OptionsMonitor.CurrentValue; @@ -547,7 +551,11 @@ namespace slskd // create a new cancellation token source so that we can cancel the upload from the UI. var cts = new CancellationTokenSource(); - var topts = new TransferOptions(stateChanged: (e) => tracker.AddOrUpdate(e, cts), progressUpdated: (e) => tracker.AddOrUpdate(e, cts)); + var topts = new TransferOptions( + stateChanged: (e) => tracker.AddOrUpdate(e, cts), + progressUpdated: (e) => tracker.AddOrUpdate(e, cts), + governor: (tx, req, ct) => Governor.GetBytes(tx, req, ct), + reporter: (tx, att, grant, act) => Governor.ReturnBytes(tx, att, grant, act)); // accept all download requests, and begin the upload immediately. normally there would be an internal queue, and // uploads would be handled separately. diff --git a/src/slskd/Governor.cs b/src/slskd/Governor.cs new file mode 100644 index 00000000..7b61d73a --- /dev/null +++ b/src/slskd/Governor.cs @@ -0,0 +1,93 @@ +// <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> GetBytes(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> GetBytes(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); + } + } +} diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 658f62f8..15baa98d 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -259,6 +259,8 @@ namespace slskd services.AddSingleton<IFTPService, FTPService>(); services.AddSingleton<IPushbulletService, PushbulletService>(); + + services.AddSingleton<IGovernor, Governor>(); } /// <summary> |
