fix: Incomplete Ready, DownloadUsersAsync, and optimize AlwaysDownloadUsers (#1548)

* Fix Ready and AlwaysDownloadUsers

Ready could fire before downloading all guild data and downloading guild users one guild per time without gateway intents is a waste of a gateway request that can support up to 1000.

* Reduce batchSize and fix count

* Fix typo

* Split xml docs line

Co-authored-by: Christopher Felegy <cfelegy@riseup.net>
This commit is contained in:
Paulo
2020-06-18 01:00:10 -03:00
committed by GitHub
parent a51cdf60a2
commit dc8c95931e
3 changed files with 39 additions and 5 deletions

View File

@@ -169,7 +169,7 @@ namespace Discord.WebSocket
GuildAvailable += g =>
{
if (ConnectionState == ConnectionState.Connected && AlwaysDownloadUsers && !g.HasAllMembers)
if (_guildDownloadTask?.IsCompleted == true && ConnectionState == ConnectionState.Connected && AlwaysDownloadUsers && !g.HasAllMembers)
{
var _ = g.DownloadUsersAsync();
}
@@ -370,7 +370,7 @@ namespace Discord.WebSocket
{
var cachedGuilds = guilds.ToImmutableArray();
const short batchSize = 50;
const short batchSize = 100; //TODO: Gateway Intents will limit to a maximum of 1 guild_id
ulong[] batchIds = new ulong[Math.Min(batchSize, cachedGuilds.Length)];
Task[] batchTasks = new Task[batchIds.Length];
int batchCount = (cachedGuilds.Length + (batchSize - 1)) / batchSize;
@@ -378,7 +378,7 @@ namespace Discord.WebSocket
for (int i = 0, k = 0; i < batchCount; i++)
{
bool isLast = i == batchCount - 1;
int count = isLast ? (batchIds.Length - (batchCount - 1) * batchSize) : batchSize;
int count = isLast ? (cachedGuilds.Length - (batchCount - 1) * batchSize) : batchSize;
for (int j = 0; j < count; j++, k++)
{
@@ -578,6 +578,9 @@ namespace Discord.WebSocket
}
else if (_connection.CancelToken.IsCancellationRequested)
return;
if (BaseConfig.AlwaysDownloadUsers)
_ = DownloadUsersAsync(Guilds.Where(x => x.IsAvailable && !x.HasAllMembers));
await TimedInvokeAsync(_readyEvent, nameof(Ready)).ConfigureAwait(false);
await _gatewayLogger.InfoAsync("Ready").ConfigureAwait(false);
@@ -1772,7 +1775,7 @@ namespace Discord.WebSocket
try
{
await logger.DebugAsync("GuildDownloader Started").ConfigureAwait(false);
while ((_unavailableGuildCount != 0) && (Environment.TickCount - _lastGuildAvailableTime < 2000))
while ((_unavailableGuildCount != 0) && (Environment.TickCount - _lastGuildAvailableTime < BaseConfig.MaxWaitBetweenGuildAvailablesBeforeReady))
await Task.Delay(500, cancelToken).ConfigureAwait(false);
await logger.DebugAsync("GuildDownloader Stopped").ConfigureAwait(false);
}