From d592695e40b23e4bffbea2651313c2f674a19ec3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 30 Jan 2022 21:10:45 -0600 Subject: stub Governor --- src/slskd/Application.cs | 10 +++++- src/slskd/Governor.cs | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ src/slskd/Startup.cs | 2 ++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/slskd/Governor.cs (limited to 'src') 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, IHubContext 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(); private ConcurrentDictionary Loggers { get; } = new ConcurrentDictionary(); 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 (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 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 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 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(); + + 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(); services.AddSingleton(); + + services.AddSingleton(); } /// -- cgit v1.2.3