2020-11-06 15:38:57 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-12-28 19:13:24 +08:00
|
|
|
using JetBrains.Annotations;
|
2020-11-06 15:38:57 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
2021-11-04 17:02:44 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-11-06 15:38:57 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
public class UserLookupCache : MemoryCachingComponent<int, APIUser>
|
2020-11-06 15:38:57 +08:00
|
|
|
{
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
2020-12-28 19:05:48 +08:00
|
|
|
/// <summary>
|
2021-11-04 17:02:44 +08:00
|
|
|
/// Perform an API lookup on the specified user, populating a <see cref="APIUser"/> model.
|
2020-12-28 19:05:48 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to lookup.</param>
|
|
|
|
/// <param name="token">An optional cancellation token.</param>
|
|
|
|
/// <returns>The populated user, or null if the user does not exist or the request could not be satisfied.</returns>
|
2020-12-28 19:13:24 +08:00
|
|
|
[ItemCanBeNull]
|
2021-11-04 17:02:44 +08:00
|
|
|
public Task<APIUser> GetUserAsync(int userId, CancellationToken token = default) => GetAsync(userId, token);
|
2020-11-06 15:38:57 +08:00
|
|
|
|
2021-07-05 18:56:16 +08:00
|
|
|
/// <summary>
|
2021-11-04 17:02:44 +08:00
|
|
|
/// Perform an API lookup on the specified users, populating a <see cref="APIUser"/> model.
|
2021-07-05 18:56:16 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="userIds">The users to lookup.</param>
|
|
|
|
/// <param name="token">An optional cancellation token.</param>
|
2021-07-05 20:30:24 +08:00
|
|
|
/// <returns>The populated users. May include null results for failed retrievals.</returns>
|
2021-11-04 17:02:44 +08:00
|
|
|
public Task<APIUser[]> GetUsersAsync(int[] userIds, CancellationToken token = default)
|
2021-07-05 18:56:16 +08:00
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
var userLookupTasks = new List<Task<APIUser>>();
|
2021-07-05 18:56:16 +08:00
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach (int u in userIds)
|
2021-07-05 18:56:16 +08:00
|
|
|
{
|
|
|
|
userLookupTasks.Add(GetUserAsync(u, token).ContinueWith(task =>
|
|
|
|
{
|
|
|
|
if (!task.IsCompletedSuccessfully)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return task.Result;
|
|
|
|
}, token));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.WhenAll(userLookupTasks);
|
|
|
|
}
|
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
protected override async Task<APIUser> ComputeValueAsync(int lookup, CancellationToken token = default)
|
2021-03-08 11:57:16 +08:00
|
|
|
=> await queryUser(lookup).ConfigureAwait(false);
|
2020-11-16 19:35:22 +08:00
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
private readonly Queue<(int id, TaskCompletionSource<APIUser>)> pendingUserTasks = new Queue<(int, TaskCompletionSource<APIUser>)>();
|
2020-11-16 19:35:22 +08:00
|
|
|
private Task pendingRequestTask;
|
|
|
|
private readonly object taskAssignmentLock = new object();
|
2020-11-06 15:38:57 +08:00
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
private Task<APIUser> queryUser(int userId)
|
2020-11-06 15:38:57 +08:00
|
|
|
{
|
|
|
|
lock (taskAssignmentLock)
|
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
var tcs = new TaskCompletionSource<APIUser>();
|
2020-11-06 15:38:57 +08:00
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
// Add to the queue.
|
2020-11-16 19:52:51 +08:00
|
|
|
pendingUserTasks.Enqueue((userId, tcs));
|
2020-11-06 15:38:57 +08:00
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
// Create a request task if there's not already one.
|
|
|
|
if (pendingRequestTask == null)
|
|
|
|
createNewTask();
|
2020-11-06 15:38:57 +08:00
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
return tcs.Task;
|
2020-11-06 15:38:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
private void performLookup()
|
2020-11-06 15:38:57 +08:00
|
|
|
{
|
2020-11-17 09:57:11 +08:00
|
|
|
// contains at most 50 unique user IDs from userTasks, which is used to perform the lookup.
|
2021-11-04 17:02:44 +08:00
|
|
|
var userTasks = new Dictionary<int, List<TaskCompletionSource<APIUser>>>();
|
2020-11-16 19:35:22 +08:00
|
|
|
|
2020-11-16 20:17:43 +08:00
|
|
|
// Grab at most 50 unique user IDs from the queue.
|
2020-11-06 15:38:57 +08:00
|
|
|
lock (taskAssignmentLock)
|
|
|
|
{
|
2020-11-17 09:57:11 +08:00
|
|
|
while (pendingUserTasks.Count > 0 && userTasks.Count < 50)
|
2020-11-06 15:38:57 +08:00
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
(int id, TaskCompletionSource<APIUser> task) next = pendingUserTasks.Dequeue();
|
2020-11-16 19:35:22 +08:00
|
|
|
|
|
|
|
// Perform a secondary check for existence, in case the user was queried in a previous batch.
|
|
|
|
if (CheckExists(next.id, out var existing))
|
|
|
|
next.task.SetResult(existing);
|
|
|
|
else
|
2020-11-16 20:17:43 +08:00
|
|
|
{
|
2020-11-17 09:57:11 +08:00
|
|
|
if (userTasks.TryGetValue(next.id, out var tasks))
|
|
|
|
tasks.Add(next.task);
|
|
|
|
else
|
2021-11-04 17:02:44 +08:00
|
|
|
userTasks[next.id] = new List<TaskCompletionSource<APIUser>> { next.task };
|
2020-11-16 20:17:43 +08:00
|
|
|
}
|
2020-11-06 15:38:57 +08:00
|
|
|
}
|
2020-11-16 19:35:22 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 16:47:16 +08:00
|
|
|
if (userTasks.Count == 0)
|
|
|
|
return;
|
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
// Query the users.
|
2020-11-17 09:57:11 +08:00
|
|
|
var request = new GetUsersRequest(userTasks.Keys.ToArray());
|
2020-11-06 15:38:57 +08:00
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
// rather than queueing, we maintain our own single-threaded request stream.
|
2020-12-28 13:56:29 +08:00
|
|
|
// todo: we probably want retry logic here.
|
2020-11-16 19:35:22 +08:00
|
|
|
api.Perform(request);
|
|
|
|
|
|
|
|
// Create a new request task if there's still more users to query.
|
|
|
|
lock (taskAssignmentLock)
|
|
|
|
{
|
|
|
|
pendingRequestTask = null;
|
|
|
|
if (pendingUserTasks.Count > 0)
|
|
|
|
createNewTask();
|
2020-11-06 15:38:57 +08:00
|
|
|
}
|
2020-11-16 19:35:22 +08:00
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
List<APIUser> foundUsers = request.Response?.Users;
|
2020-12-28 13:56:29 +08:00
|
|
|
|
|
|
|
if (foundUsers != null)
|
2020-11-17 09:57:11 +08:00
|
|
|
{
|
2020-12-28 13:56:29 +08:00
|
|
|
foreach (var user in foundUsers)
|
2020-11-17 09:57:11 +08:00
|
|
|
{
|
2020-12-28 13:56:29 +08:00
|
|
|
if (userTasks.TryGetValue(user.Id, out var tasks))
|
|
|
|
{
|
|
|
|
foreach (var task in tasks)
|
|
|
|
task.SetResult(user);
|
2020-11-17 09:57:11 +08:00
|
|
|
|
2020-12-28 13:56:29 +08:00
|
|
|
userTasks.Remove(user.Id);
|
|
|
|
}
|
2020-11-17 09:57:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if any tasks remain which were not satisfied, return null.
|
|
|
|
foreach (var tasks in userTasks.Values)
|
|
|
|
{
|
|
|
|
foreach (var task in tasks)
|
|
|
|
task.SetResult(null);
|
|
|
|
}
|
2020-11-06 15:38:57 +08:00
|
|
|
}
|
2020-11-16 19:35:22 +08:00
|
|
|
|
|
|
|
private void createNewTask() => pendingRequestTask = Task.Run(performLookup);
|
2020-11-06 15:38:57 +08:00
|
|
|
}
|
|
|
|
}
|