Browse Source

Wait identify before opening the connection

pull/1537/head
Paulo 4 years ago
parent
commit
baaa434994
3 changed files with 28 additions and 23 deletions
  1. +24
    -21
      src/Discord.Net.Rest/Net/Queue/RequestQueue.cs
  2. +2
    -2
      src/Discord.Net.WebSocket/DiscordShardedClient.cs
  3. +2
    -0
      src/Discord.Net.WebSocket/DiscordSocketClient.cs

+ 24
- 21
src/Discord.Net.Rest/Net/Queue/RequestQueue.cs View File

@@ -136,27 +136,6 @@ namespace Discord.Net.Queue
var requestBucket = GatewayBucket.Get(request.Options.BucketId);
if (requestBucket.Type == GatewayBucketType.Unbucketed)
return;
//Identify is per-account so we won't trigger global until we can actually go for it
if (requestBucket.Type == GatewayBucketType.Identify)
{
if (_masterIdentifySemaphore == null)
throw new InvalidOperationException("Not a RequestQueue with WebSocket data.");

if (_identifySemaphore == null)
await _masterIdentifySemaphore.WaitAsync(request.CancelToken);
else
{
bool master;
while (!(master = _masterIdentifySemaphore.Wait(0)) && !_identifySemaphore.Wait(0)) //To not block the thread
await Task.Delay(100, request.CancelToken);
if (master && _identifySemaphoreMaxConcurrency > 1)
_identifySemaphore.Release(_identifySemaphoreMaxConcurrency - 1);
}
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Acquired identify ticket");
#endif
}

//It's not a global request, so need to remove one from global (per-session)
var globalBucketType = GatewayBucket.Get(GatewayBucketType.Unbucketed);
@@ -179,6 +158,30 @@ namespace Discord.Net.Queue
#endif
}

public async Task AcquireIdentifyTicket(CancellationToken cancellationToken)
{
try
{
if (_masterIdentifySemaphore == null)
throw new InvalidOperationException("Not a RequestQueue with WebSocket data.");

if (_identifySemaphore == null)
await _masterIdentifySemaphore.WaitAsync(cancellationToken);
else
{
bool master;
while (!(master = _masterIdentifySemaphore.Wait(0)) && !_identifySemaphore.Wait(0)) //To not block the thread
await Task.Delay(100, cancellationToken);
if (master && _identifySemaphoreMaxConcurrency > 1)
_identifySemaphore.Release(_identifySemaphoreMaxConcurrency - 1);
}
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Acquired identify ticket");
#endif
}
catch(OperationCanceledException) { }
}

private RequestBucket GetOrCreateBucket(RequestOptions options, IRequest request)
{
var bucketId = options.BucketId;


+ 2
- 2
src/Discord.Net.WebSocket/DiscordShardedClient.cs View File

@@ -86,7 +86,7 @@ namespace Discord.WebSocket
_shardIdsToIndex.Add(_shardIds[i], i);
var newConfig = config.Clone();
newConfig.ShardId = _shardIds[i];
_shards[i] = new DiscordSocketClient(newConfig, i != 0 ? _shards[0] : null, masterIdentifySemaphore, config.IdentifyMaxConcurrency > 1 ? null : identifySemaphores[i / config.IdentifyMaxConcurrency], config.IdentifyMaxConcurrency);
_shards[i] = new DiscordSocketClient(newConfig, i != 0 ? _shards[0] : null, masterIdentifySemaphore, config.IdentifyMaxConcurrency == 1 ? null : identifySemaphores[i / config.IdentifyMaxConcurrency], config.IdentifyMaxConcurrency);
RegisterEvents(_shards[i], i == 0);
}
}
@@ -120,7 +120,7 @@ namespace Discord.WebSocket
var newConfig = _baseConfig.Clone();
newConfig.ShardId = _shardIds[i];
newConfig.TotalShards = _totalShards;
_shards[i] = new DiscordSocketClient(newConfig, i != 0 ? _shards[0] : null, masterIdentifySemaphore, maxConcurrency > 1 ? null : identifySemaphores[i / maxConcurrency], maxConcurrency);
_shards[i] = new DiscordSocketClient(newConfig, i != 0 ? _shards[0] : null, masterIdentifySemaphore, maxConcurrency == 1 ? null : identifySemaphores[i / maxConcurrency], maxConcurrency);
RegisterEvents(_shards[i], i == 0);
}
}


+ 2
- 0
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -228,6 +228,8 @@ namespace Discord.WebSocket

private async Task OnConnectingAsync()
{
await ApiClient.RequestQueue.AcquireIdentifyTicket(_connection.CancelToken);

await _gatewayLogger.DebugAsync("Connecting ApiClient").ConfigureAwait(false);
await ApiClient.ConnectAsync().ConfigureAwait(false);



Loading…
Cancel
Save