From c46dbf14a69674044756f4068f86439b1e0e79fb Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 12 Feb 2017 21:51:36 +0100 Subject: [PATCH] Redis fixes --- .../Timers/CompletionTimer.cs | 20 +++++++------- .../Config/Identity/IdentityServices.cs | 27 ++++++++++++++++--- src/Squidex/appsettings.json | 2 +- src/Squidex/project.json | 1 + 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/Squidex.Infrastructure/Timers/CompletionTimer.cs b/src/Squidex.Infrastructure/Timers/CompletionTimer.cs index bcfb6b38c..94b94b640 100644 --- a/src/Squidex.Infrastructure/Timers/CompletionTimer.cs +++ b/src/Squidex.Infrastructure/Timers/CompletionTimer.cs @@ -16,9 +16,9 @@ namespace Squidex.Infrastructure.Timers { public sealed class CompletionTimer : DisposableObject { - private readonly CancellationTokenSource disposeCancellationTokenSource = new CancellationTokenSource(); + private readonly CancellationTokenSource disposeToken = new CancellationTokenSource(); private readonly Task runTask; - private CancellationTokenSource delayCancellationSource; + private CancellationTokenSource delayToken; public CompletionTimer(int delay, Func callback) { @@ -30,15 +30,18 @@ namespace Squidex.Infrastructure.Timers private async Task RunInternal(int delay, Func callback) { - while (!disposeCancellationTokenSource.IsCancellationRequested) + while (!disposeToken.IsCancellationRequested) { try { - await callback(disposeCancellationTokenSource.Token).ConfigureAwait(false); + await callback(disposeToken.Token).ConfigureAwait(false); - delayCancellationSource = new CancellationTokenSource(); + delayToken = new CancellationTokenSource(); - await Task.Delay(delay, delayCancellationSource.Token).ConfigureAwait(false); + using (var cts = CancellationTokenSource.CreateLinkedTokenSource(disposeToken.Token, delayToken.Token)) + { + await Task.Delay(delay, cts.Token).ConfigureAwait(false); + } } catch (TaskCanceledException) { @@ -51,8 +54,7 @@ namespace Squidex.Infrastructure.Timers { if (disposing) { - delayCancellationSource?.Cancel(); - disposeCancellationTokenSource.Cancel(); + disposeToken.Cancel(); runTask.Wait(); } @@ -62,7 +64,7 @@ namespace Squidex.Infrastructure.Timers { ThrowIfDisposed(); - delayCancellationSource?.Cancel(); + delayToken?.Cancel(); } } } diff --git a/src/Squidex/Config/Identity/IdentityServices.cs b/src/Squidex/Config/Identity/IdentityServices.cs index 4ce228006..d3462be1f 100644 --- a/src/Squidex/Config/Identity/IdentityServices.cs +++ b/src/Squidex/Config/Identity/IdentityServices.cs @@ -6,6 +6,7 @@ // All rights reserved. // ========================================================================== +using System; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -17,7 +18,9 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity.MongoDB; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Squidex.Infrastructure; using Squidex.Infrastructure.Security; +using StackExchange.Redis; namespace Squidex.Config.Identity { @@ -27,11 +30,29 @@ namespace Squidex.Config.Identity { var dataProtection = services.AddDataProtection().SetApplicationName("Squidex"); - var keysFolder = configuration.GetValue("squidex:identity:keysFolder"); + var clustererType = configuration.GetValue("squidex:clusterer:type"); - if (!string.IsNullOrWhiteSpace(keysFolder)) + if (clustererType.Equals("redis", StringComparison.OrdinalIgnoreCase)) { - dataProtection.PersistKeysToFileSystem(new DirectoryInfo(keysFolder)); + var connectionString = configuration.GetValue("squidex:clusterer:redis:connectionString"); + + if (string.IsNullOrWhiteSpace(connectionString) || !Uri.IsWellFormedUriString(connectionString, UriKind.Absolute)) + { + throw new ConfigurationException("You must specify the Redis connection string in the 'squidex:clusterer:redis:connectionString' configuration section."); + } + + var connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString); + + dataProtection.PersistKeysToRedis(connectionMultiplexer); + } + else + { + var keysFolder = configuration.GetValue("squidex:identity:keysFolder"); + + if (!string.IsNullOrWhiteSpace(keysFolder)) + { + dataProtection.PersistKeysToFileSystem(new DirectoryInfo(keysFolder)); + } } return services; diff --git a/src/Squidex/appsettings.json b/src/Squidex/appsettings.json index ad31902ba..50170131d 100644 --- a/src/Squidex/appsettings.json +++ b/src/Squidex/appsettings.json @@ -6,7 +6,7 @@ "clusterer": { "type": "redis", "redis": { - "connectionString": "localhost:6379" + "connectionString": "localhost:6379,resolveDns=1" } }, "eventStore": { diff --git a/src/Squidex/project.json b/src/Squidex/project.json index 7f3be446a..7efda9a78 100644 --- a/src/Squidex/project.json +++ b/src/Squidex/project.json @@ -8,6 +8,7 @@ "Microsoft.AspNetCore.Authentication.Cookies": "1.1.0", "Microsoft.AspNetCore.Authentication.Google": "1.1.0", "Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.1.0", + "Microsoft.AspNetCore.DataProtection.Redis": "0.1.0", "Microsoft.AspNetCore.Diagnostics": "1.1.0", "Microsoft.AspNetCore.HttpOverrides": "1.1.0", "Microsoft.AspNetCore.Identity": "1.1.0",