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;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Users;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
public class UserLookupCache : MemoryCachingComponent<int, User>
|
|
|
|
{
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
2020-11-09 09:40:16 +08:00
|
|
|
public Task<User> GetUserAsync(int userId, CancellationToken token = default) => GetAsync(userId, token);
|
2020-11-06 15:38:57 +08:00
|
|
|
|
|
|
|
protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
|
2020-11-16 19:35:22 +08:00
|
|
|
=> await queryUser(lookup);
|
|
|
|
|
2020-11-16 19:52:51 +08:00
|
|
|
private readonly Queue<(int id, TaskCompletionSource<User>)> pendingUserTasks = new Queue<(int, TaskCompletionSource<User>)>();
|
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
|
|
|
|
2020-11-16 19:35:22 +08:00
|
|
|
private Task<User> queryUser(int userId)
|
2020-11-06 15:38:57 +08:00
|
|
|
{
|
|
|
|
lock (taskAssignmentLock)
|
|
|
|
{
|
2020-11-16 19:35:22 +08:00
|
|
|
var tcs = new TaskCompletionSource<User>();
|
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-16 20:17:43 +08:00
|
|
|
// userTasks may exceed 50 elements, indicating the existence of duplicate user IDs. All duplicated user IDs must be fulfilled.
|
|
|
|
// userIds contains at most 50 unique user IDs from userTasks, which is used to perform the lookup.
|
2020-11-16 19:35:22 +08:00
|
|
|
var userTasks = new List<(int id, TaskCompletionSource<User> task)>();
|
2020-11-16 20:17:43 +08:00
|
|
|
var userIds = new HashSet<int>();
|
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-16 20:17:43 +08:00
|
|
|
while (pendingUserTasks.Count > 0 && userIds.Count < 50)
|
2020-11-06 15:38:57 +08:00
|
|
|
{
|
2020-11-16 19:52:51 +08:00
|
|
|
(int id, TaskCompletionSource<User> 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-16 19:35:22 +08:00
|
|
|
userTasks.Add(next);
|
2020-11-16 20:17:43 +08:00
|
|
|
userIds.Add(next.id);
|
|
|
|
}
|
2020-11-06 15:38:57 +08:00
|
|
|
}
|
2020-11-16 19:35:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query the users.
|
2020-11-16 20:17:43 +08:00
|
|
|
var request = new GetUsersRequest(userIds.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.
|
|
|
|
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
|
|
|
|
|
|
|
// Notify of completion.
|
|
|
|
foreach (var (id, task) in userTasks)
|
|
|
|
task.SetResult(request.Result?.Users?.FirstOrDefault(u => u.Id == id));
|
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
|
|
|
}
|
|
|
|
}
|