Browse Source

Merge 4a4b011c34 into 033d31294f

pull/1073/merge
Christopher F GitHub 7 years ago
parent
commit
5a485782f0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions
  1. +8
    -1
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  2. +3
    -1
      src/Discord.Net.WebSocket/DiscordSocketConfig.cs

+ 8
- 1
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -58,6 +58,7 @@ namespace Discord.WebSocket
internal WebSocketProvider WebSocketProvider { get; private set; }
internal bool AlwaysDownloadUsers { get; private set; }
internal int? HandlerTimeout { get; private set; }
internal bool OffloadAllHandlers { get; private set; }

internal new DiscordSocketApiClient ApiClient => base.ApiClient as DiscordSocketApiClient;
public override IReadOnlyCollection<SocketGuild> Guilds => State.Guilds;
@@ -84,9 +85,13 @@ namespace Discord.WebSocket
WebSocketProvider = config.WebSocketProvider;
AlwaysDownloadUsers = config.AlwaysDownloadUsers;
HandlerTimeout = config.HandlerTimeout;
OffloadAllHandlers = config.OffloadAllHandlers;
State = new ClientState(0, 0);
_heartbeatTimes = new ConcurrentQueue<long>();

if (OffloadAllHandlers && !HandlerTimeout.HasValue)
throw new InvalidOperationException("If OffloadAllHandlers is set, the HandlerTimeout must also be set. To suppress this message, unset OffloadAllHandlers");

_stateLock = new SemaphoreSlim(1, 1);
_gatewayLogger = LogManager.CreateLogger(ShardId == 0 && TotalShards == 1 ? "Gateway" : $"Shard #{ShardId}");
_connection = new ConnectionManager(_stateLock, _gatewayLogger, config.ConnectionTimeout,
@@ -1735,8 +1740,10 @@ namespace Discord.WebSocket
{
try
{
var timeoutTask = Task.Delay(HandlerTimeout.Value);
var handlersTask = action();
if (OffloadAllHandlers)
return;
var timeoutTask = Task.Delay(HandlerTimeout.Value);
if (await Task.WhenAny(timeoutTask, handlersTask).ConfigureAwait(false) == timeoutTask)
{
await _gatewayLogger.WarningAsync($"A {name} handler is blocking the gateway task.").ConfigureAwait(false);


+ 3
- 1
src/Discord.Net.WebSocket/DiscordSocketConfig.cs View File

@@ -1,4 +1,4 @@
using Discord.Net.Udp;
using Discord.Net.Udp;
using Discord.Net.WebSockets;
using Discord.Rest;

@@ -35,6 +35,8 @@ namespace Discord.WebSocket
public bool AlwaysDownloadUsers { get; set; } = false;
/// <summary> Gets or sets the timeout for event handlers, in milliseconds, after which a warning will be logged. Null disables this check. </summary>
public int? HandlerTimeout { get; set; } = 3000;
/// <summary> Gets or sets whether or not offload all event handlers from the gateway task. This can have dangerous consequences. </summary>
public bool OffloadAllHandlers { get; set; } = false;

public DiscordSocketConfig()
{


Loading…
Cancel
Save