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/Users | |
| 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/Users')
| -rw-r--r-- | src/slskd/Users/IUserService.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 58 |
2 files changed, 60 insertions, 5 deletions
diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index 469acef6..ad368c65 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -26,6 +26,13 @@ namespace slskd.Users public interface IUserService { /// <summary> + /// Gets the name of the group for the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the peer.</param> + /// <returns>The group for the specified username.</returns> + string GetGroup(string username); + + /// <summary> /// Retrieves peer <see cref="Info"/>. /// </summary> /// <param name="username">The username of the peer.</param> diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index f52f19f1..cb56e6dd 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -15,13 +15,18 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // </copyright> +using Microsoft.Extensions.Options; + namespace slskd.Users { using System; + using System.Collections.Concurrent; + using System.Collections.Generic; + using System.Linq; using System.Net; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; - using Microsoft.Extensions.Logging; + using Serilog; using Soulseek; /// <summary> @@ -36,20 +41,37 @@ namespace slskd.Users /// </summary> /// <param name="soulseekClient"></param> /// <param name="contextFactory">The database context to use.</param> - /// <param name="log">The logger.</param> + /// <param name="optionsMonitor"></param> public UserService( ISoulseekClient soulseekClient, IDbContextFactory<UserDbContext> contextFactory, - ILogger<UserService> log) + IOptionsMonitor<Options> optionsMonitor) { Client = soulseekClient; ContextFactory = contextFactory; - Log = log; + + OptionsMonitor = optionsMonitor; + OptionsMonitor.OnChange(Configure); + + Configure(OptionsMonitor.CurrentValue); } private ISoulseekClient Client { get; } private IDbContextFactory<UserDbContext> ContextFactory { get; } - private ILogger<UserService> Log { get; set; } + private string LastOptionsHash { get; set; } + private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>(); + private ConcurrentDictionary<string, string> Map { get; set; } = new ConcurrentDictionary<string, string>(); + private IOptionsMonitor<Options> OptionsMonitor { get; } + + /// <summary> + /// Gets the name of the group for the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the peer.</param> + /// <returns>The group for the specified username.</returns> + public string GetGroup(string username) + { + return Map.GetValueOrDefault(username ?? string.Empty, Application.DefaultGroup); + } /// <summary> /// Retrieves peer <see cref="Info"/>. @@ -134,5 +156,31 @@ namespace slskd.Users { await Client.AddUserAsync(username); } + + private void Configure(Options options) + { + var optionsHash = Compute.Sha1Hash(options.Groups.UserDefined.ToJson()); + + if (optionsHash == LastOptionsHash) + { + return; + } + + var map = new ConcurrentDictionary<string, string>(); + + // sort by priority, ascending + foreach (var group in options.Groups.UserDefined.OrderBy(kvp => kvp.Value.Upload.Priority)) + { + foreach (var user in group.Value.Members) + { + // if the key already exists, leave the existing entry. if a user appears in more than one group, the higher + // priority (lower numbered) group is their effective group. + map.TryAdd(user, group.Key); + } + } + + Map = map; + LastOptionsHash = optionsHash; + } } }
\ No newline at end of file |
