From d138cb947ae72e256c62184c788c714d41f4ce6b Mon Sep 17 00:00:00 2001 From: Itamar Amith Date: Fri, 18 Aug 2023 19:44:07 +1000 Subject: [PATCH] Introduce cancellation token --- .../DiscordGatewayClient.cs | 4 +- .../Handlers/TokenAuthorizationHandler.cs | 2 +- .../Remora.Discord.Rest/IAsyncTokenStore.cs | 4 +- Backend/Remora.Discord.Rest/LazyTokenStore.cs | 53 -------------- .../Remora.Discord.Rest/StaticTokenStore.cs | 3 +- .../Core/TokenStore/LazyTokenStoreTests.cs | 72 ------------------- .../Core/TokenStore/StaticTokenStoreTests.cs | 2 +- 7 files changed, 9 insertions(+), 131 deletions(-) delete mode 100644 Backend/Remora.Discord.Rest/LazyTokenStore.cs delete mode 100644 Tests/Remora.Discord.Tests/Tests/Core/TokenStore/LazyTokenStoreTests.cs diff --git a/Backend/Remora.Discord.Gateway/DiscordGatewayClient.cs b/Backend/Remora.Discord.Gateway/DiscordGatewayClient.cs index 87e618e024..45ef73b30a 100644 --- a/Backend/Remora.Discord.Gateway/DiscordGatewayClient.cs +++ b/Backend/Remora.Discord.Gateway/DiscordGatewayClient.cs @@ -705,7 +705,7 @@ private async Task CreateNewSessionAsync(CancellationToken ct = default) ( new Identify ( - await _tokenStore.GetTokenAsync(), + await _tokenStore.GetTokenAsync(ct), _gatewayOptions.ConnectionProperties, false, _gatewayOptions.LargeThreshold, @@ -811,7 +811,7 @@ private async Task ResumeExistingSessionAsync(CancellationToken ct = def ( new Resume ( - await _tokenStore.GetTokenAsync(), + await _tokenStore.GetTokenAsync(ct), _sessionInformation.SessionID, _sessionInformation.SequenceNumber ) diff --git a/Backend/Remora.Discord.Rest/Handlers/TokenAuthorizationHandler.cs b/Backend/Remora.Discord.Rest/Handlers/TokenAuthorizationHandler.cs index 4bcebe6b64..c1886dcb4d 100644 --- a/Backend/Remora.Discord.Rest/Handlers/TokenAuthorizationHandler.cs +++ b/Backend/Remora.Discord.Rest/Handlers/TokenAuthorizationHandler.cs @@ -53,7 +53,7 @@ protected override async Task SendAsync CancellationToken cancellationToken ) { - var token = await _tokenStore.GetTokenAsync(); + var token = await _tokenStore.GetTokenAsync(cancellationToken); var tokenType = _tokenStore.TokenType; if (string.IsNullOrWhiteSpace(token)) diff --git a/Backend/Remora.Discord.Rest/IAsyncTokenStore.cs b/Backend/Remora.Discord.Rest/IAsyncTokenStore.cs index a3b137e563..fe264f24e2 100644 --- a/Backend/Remora.Discord.Rest/IAsyncTokenStore.cs +++ b/Backend/Remora.Discord.Rest/IAsyncTokenStore.cs @@ -20,6 +20,7 @@ // along with this program. If not, see . // +using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; @@ -34,8 +35,9 @@ public interface IAsyncTokenStore /// /// Gets the token. /// + /// A cancellation token to cancel operation. /// The token's value. - ValueTask GetTokenAsync(); + ValueTask GetTokenAsync(CancellationToken cancellationToken); /// /// Gets the type of the token. diff --git a/Backend/Remora.Discord.Rest/LazyTokenStore.cs b/Backend/Remora.Discord.Rest/LazyTokenStore.cs deleted file mode 100644 index a91ecb69e9..0000000000 --- a/Backend/Remora.Discord.Rest/LazyTokenStore.cs +++ /dev/null @@ -1,53 +0,0 @@ -// -// LazyTokenStore.cs -// -// Author: -// Jarl Gullberg -// -// Copyright (c) Jarl Gullberg -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see . -// - -using System; -using System.Threading.Tasks; -using JetBrains.Annotations; - -namespace Remora.Discord.Rest; - -/// -/// Represents a storage class for a token fetched asynchronously. -/// -[PublicAPI] -public class LazyTokenStore : IAsyncTokenStore -{ - private readonly Lazy> _token; - - /// - public async ValueTask GetTokenAsync() => await _token.Value; - - /// - public DiscordTokenType TokenType { get; } - - /// - /// Initializes a new instance of the class. - /// - /// A func that returns a task resolving to the token. - /// The type of token to store. - public LazyTokenStore(Func> func, DiscordTokenType tokenType) - { - _token = new(func); - this.TokenType = tokenType; - } -} diff --git a/Backend/Remora.Discord.Rest/StaticTokenStore.cs b/Backend/Remora.Discord.Rest/StaticTokenStore.cs index 83cf28cca3..0ee5d3652a 100644 --- a/Backend/Remora.Discord.Rest/StaticTokenStore.cs +++ b/Backend/Remora.Discord.Rest/StaticTokenStore.cs @@ -20,6 +20,7 @@ // along with this program. If not, see . // +using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; @@ -34,7 +35,7 @@ public class StaticTokenStore : IAsyncTokenStore private readonly ValueTask _token; /// - public ValueTask GetTokenAsync() => _token; + public ValueTask GetTokenAsync(CancellationToken cancellationToken) => _token; /// public DiscordTokenType TokenType { get; } diff --git a/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/LazyTokenStoreTests.cs b/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/LazyTokenStoreTests.cs deleted file mode 100644 index d6856ba899..0000000000 --- a/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/LazyTokenStoreTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -// -// LazyTokenStoreTests.cs -// -// Author: -// Jarl Gullberg -// -// Copyright (c) Jarl Gullberg -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see . -// - -using System.Threading.Tasks; -using Remora.Discord.Rest; -using Xunit; - -// ReSharper disable SA1600 -#pragma warning disable 1591, SA1600 - -namespace Remora.Discord.Tests.Tests.Core; - -/// -/// Tests the class. -/// -public class LazyTokenStoreTests -{ - /// - /// Tests the property. - /// - public class Token - { - [Fact] - public async Task ReturnsCorrectValue() - { - var tokenStore = new LazyTokenStore(() => new("Hello world!"), DiscordTokenType.Bearer); - - Assert.Equal("Hello world!", await tokenStore.GetTokenAsync()); - } - } - - /// - /// Tests the property. - /// - public class TokenType - { - [Fact] - public void ReturnsCorrectValueForBotTokenType() - { - var tokenStore = new LazyTokenStore(() => new("Hello world!"), DiscordTokenType.Bot); - - Assert.Equal(DiscordTokenType.Bot, tokenStore.TokenType); - } - - [Fact] - public void ReturnsCorrectValueForBearerTokenType() - { - var tokenStore = new LazyTokenStore(() => new("Hello world!"), DiscordTokenType.Bearer); - - Assert.Equal(DiscordTokenType.Bearer, tokenStore.TokenType); - } - } -} diff --git a/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/StaticTokenStoreTests.cs b/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/StaticTokenStoreTests.cs index 267556c7bb..7887489cad 100644 --- a/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/StaticTokenStoreTests.cs +++ b/Tests/Remora.Discord.Tests/Tests/Core/TokenStore/StaticTokenStoreTests.cs @@ -44,7 +44,7 @@ public async Task ReturnsCorrectValue() { var tokenStore = new StaticTokenStore("Hello world!", DiscordTokenType.Bearer); - Assert.Equal("Hello world!", await tokenStore.GetTokenAsync()); + Assert.Equal("Hello world!", await tokenStore.GetTokenAsync(default)); } }