From d2df6d038d53c737ca4d40fdddb3844f98e5f440 Mon Sep 17 00:00:00 2001 From: RogueException Date: Mon, 13 Jun 2016 20:05:11 -0300 Subject: [PATCH] Added gateway url caching --- src/Discord.Net/API/DiscordAPIClient.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net/API/DiscordAPIClient.cs b/src/Discord.Net/API/DiscordAPIClient.cs index 82fc625c1..dd8118b11 100644 --- a/src/Discord.Net/API/DiscordAPIClient.cs +++ b/src/Discord.Net/API/DiscordAPIClient.cs @@ -36,6 +36,7 @@ namespace Discord.API private readonly SemaphoreSlim _connectionLock; private CancellationTokenSource _loginCancelToken, _connectCancelToken; private string _authToken; + private string _gatewayUrl; private bool _isDisposed; public LoginState LoginState { get; private set; } @@ -199,14 +200,18 @@ namespace Discord.API if (_gatewayClient != null) _gatewayClient.SetCancelToken(_connectCancelToken.Token); - var gatewayResponse = await GetGatewayAsync().ConfigureAwait(false); - var url = $"{gatewayResponse.Url}?v={DiscordConfig.GatewayAPIVersion}&encoding={DiscordConfig.GatewayEncoding}"; - await _gatewayClient.ConnectAsync(url).ConfigureAwait(false); + if (_gatewayUrl == null) + { + var gatewayResponse = await GetGatewayAsync().ConfigureAwait(false); + _gatewayUrl = $"{gatewayResponse.Url}?v={DiscordConfig.GatewayAPIVersion}&encoding={DiscordConfig.GatewayEncoding}"; + } + await _gatewayClient.ConnectAsync(_gatewayUrl).ConfigureAwait(false); ConnectionState = ConnectionState.Connected; } catch (Exception) { + _gatewayUrl = null; //Uncache in case the gateway url changed await DisconnectInternalAsync().ConfigureAwait(false); throw; }