From 87459c228f251f7d5497906639f0a560ef32f3a5 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 24 Feb 2020 20:04:29 +0100 Subject: [PATCH] Files moved to correct place. --- .../Notifications/INotificationEmailSender.cs | 2 +- .../NoopNotificationEmailSender.cs | 2 +- .../NotificationEmailEventConsumer.cs | 2 +- .../Notifications/NotificationEmailSender.cs | 2 +- .../NotificationEmailTextOptions.cs | 2 +- .../Contents/ContextExtensions.cs | 10 +-- .../GraphQL/GraphQLExecutionContext.cs | 4 +- .../Email/SmtpEmailSender.cs | 45 +++++++++++--- .../Log/JsonLogWriterFactory.cs | 61 +++++++++++-------- .../Squidex.Infrastructure.csproj | 1 + .../Config/Domain/NotificationsServices.cs | 2 +- .../NotificationEmailEventConsumerTests.cs | 2 +- .../NotificationEmailSenderTests.cs | 2 +- 13 files changed, 90 insertions(+), 47 deletions(-) rename backend/src/Squidex.Domain.Apps.Entities/{History => Apps/Invitation}/Notifications/INotificationEmailSender.cs (90%) rename backend/src/Squidex.Domain.Apps.Entities/{History => Apps/Invitation}/Notifications/NoopNotificationEmailSender.cs (91%) rename backend/src/Squidex.Domain.Apps.Entities/{History => Apps/Invitation}/Notifications/NotificationEmailEventConsumer.cs (98%) rename backend/src/Squidex.Domain.Apps.Entities/{History => Apps/Invitation}/Notifications/NotificationEmailSender.cs (98%) rename backend/src/Squidex.Domain.Apps.Entities/{History => Apps/Invitation}/Notifications/NotificationEmailTextOptions.cs (90%) rename backend/tests/Squidex.Domain.Apps.Entities.Tests/{History => Apps/Invitation}/Notifications/NotificationEmailEventConsumerTests.cs (98%) rename backend/tests/Squidex.Domain.Apps.Entities.Tests/{History => Apps/Invitation}/Notifications/NotificationEmailSenderTests.cs (98%) diff --git a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/INotificationEmailSender.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/INotificationEmailSender.cs similarity index 90% rename from backend/src/Squidex.Domain.Apps.Entities/History/Notifications/INotificationEmailSender.cs rename to backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/INotificationEmailSender.cs index 9b837cb6f..052ffd06f 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/INotificationEmailSender.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/INotificationEmailSender.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Squidex.Shared.Users; -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public interface INotificationEmailSender { diff --git a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NoopNotificationEmailSender.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NoopNotificationEmailSender.cs similarity index 91% rename from backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NoopNotificationEmailSender.cs rename to backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NoopNotificationEmailSender.cs index 306b9d90b..e78b83f0f 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NoopNotificationEmailSender.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NoopNotificationEmailSender.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Squidex.Shared.Users; -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public sealed class NoopNotificationEmailSender : INotificationEmailSender { diff --git a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailEventConsumer.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailEventConsumer.cs similarity index 98% rename from backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailEventConsumer.cs rename to backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailEventConsumer.cs index a5d942914..67d89fc15 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailEventConsumer.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailEventConsumer.cs @@ -13,7 +13,7 @@ using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Log; using Squidex.Shared.Users; -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public sealed class NotificationEmailEventConsumer : IEventConsumer { diff --git a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailSender.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailSender.cs similarity index 98% rename from backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailSender.cs rename to backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailSender.cs index f76c6087a..66b74e8c0 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailSender.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailSender.cs @@ -12,7 +12,7 @@ using Squidex.Infrastructure.Email; using Squidex.Infrastructure.Log; using Squidex.Shared.Users; -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public sealed class NotificationEmailSender : INotificationEmailSender { diff --git a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailTextOptions.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailTextOptions.cs similarity index 90% rename from backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailTextOptions.cs rename to backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailTextOptions.cs index cb56fe86d..1d2bdfbbf 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/History/Notifications/NotificationEmailTextOptions.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Invitation/Notifications/NotificationEmailTextOptions.cs @@ -5,7 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public sealed class NotificationEmailTextOptions { diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/ContextExtensions.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/ContextExtensions.cs index 5447c230e..64b521929 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/ContextExtensions.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/ContextExtensions.cs @@ -39,6 +39,11 @@ namespace Squidex.Domain.Apps.Entities.Contents cache.AddHeader(HeaderUnpublished); } + public static SearchScope Scope(this Context context) + { + return context.ShouldProvideUnpublished() || context.IsFrontendClient ? SearchScope.All : SearchScope.Published; + } + public static bool ShouldCleanup(this Context context) { return !context.Headers.ContainsKey(HeaderNoCleanup); @@ -99,11 +104,6 @@ namespace Squidex.Domain.Apps.Entities.Contents return SetBoolean(context, HeaderNoResolveLanguages, value); } - public static SearchScope Scope(this Context context) - { - return context.ShouldProvideUnpublished() || context.IsFrontendClient ? SearchScope.All : SearchScope.Published; - } - public static IEnumerable AssetUrls(this Context context) { if (context.Headers.TryGetValue(HeaderResolveUrls, out var value)) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs index d8963d46b..2b4ae34bf 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs @@ -31,7 +31,9 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL public ISemanticLog Log { get; } public GraphQLExecutionContext(Context context, IDependencyResolver resolver) - : base(context, + : base(context + .WithoutCleanup() + .WithoutContentEnrichment(), resolver.Resolve(), resolver.Resolve()) { diff --git a/backend/src/Squidex.Infrastructure/Email/SmtpEmailSender.cs b/backend/src/Squidex.Infrastructure/Email/SmtpEmailSender.cs index e73b87dc2..d7b13b81d 100644 --- a/backend/src/Squidex.Infrastructure/Email/SmtpEmailSender.cs +++ b/backend/src/Squidex.Infrastructure/Email/SmtpEmailSender.cs @@ -9,6 +9,7 @@ using System.Diagnostics.CodeAnalysis; using System.Net; using System.Net.Mail; using System.Threading.Tasks; +using Microsoft.Extensions.ObjectPool; using Microsoft.Extensions.Options; namespace Squidex.Infrastructure.Email @@ -17,27 +18,55 @@ namespace Squidex.Infrastructure.Email public sealed class SmtpEmailSender : IEmailSender { private readonly SmptOptions options; + private readonly ObjectPool pool; + + internal sealed class SmtpClientPolicy : PooledObjectPolicy + { + private readonly SmptOptions options; + + public SmtpClientPolicy(SmptOptions options) + { + this.options = options; + } + + public override SmtpClient Create() + { + return new SmtpClient(options.Server, options.Port) + { + Credentials = new NetworkCredential( + options.Username, + options.Password), + + EnableSsl = options.EnableSsl + }; + } + + public override bool Return(SmtpClient obj) + { + return true; + } + } public SmtpEmailSender(IOptions options) { Guard.NotNull(options); this.options = options.Value; + + pool = new DefaultObjectPoolProvider().Create(new SmtpClientPolicy(options.Value)); } public async Task SendAsync(string recipient, string subject, string body) { - using (var smtpClient = new SmtpClient(options.Server, options.Port) - { - Credentials = new NetworkCredential( - options.Username, - options.Password), - - EnableSsl = options.EnableSsl - }) + var smtpClient = pool.Get(); + try { await smtpClient.SendMailAsync(options.Sender, recipient, subject, body); } + finally + { + pool.Return(smtpClient); + } } } } diff --git a/backend/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs b/backend/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs index f499d0b01..4ee0bdc6f 100644 --- a/backend/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs +++ b/backend/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs @@ -5,24 +5,49 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -using System.Collections.Concurrent; using System.Text.Json; +using Microsoft.Extensions.ObjectPool; namespace Squidex.Infrastructure.Log { public sealed class JsonLogWriterFactory : IObjectWriterFactory { - private const int MaxPoolSize = 10; - private const int MaxCapacity = 5000; - private readonly ConcurrentStack pool = new ConcurrentStack(); - private readonly JsonWriterOptions formatting; - private readonly bool formatLine; + private readonly ObjectPool pool; - public JsonLogWriterFactory(bool indended = false, bool formatLine = false) + internal sealed class JsonLogWriterPolicy : PooledObjectPolicy { - formatting.Indented = indended; + private const int MaxCapacity = 5000; + private readonly JsonWriterOptions formatting; + private readonly bool formatLine; + + public JsonLogWriterPolicy(bool indended = false, bool formatLine = false) + { + formatting.Indented = indended; + + this.formatLine = formatLine; + } + + public override JsonLogWriter Create() + { + return new JsonLogWriter(formatting, formatLine); + } + + public override bool Return(JsonLogWriter obj) + { + if (obj.BufferSize > MaxCapacity) + { + return false; + } + + obj.Reset(); + + return true; + } + } - this.formatLine = formatLine; + public JsonLogWriterFactory(bool indended = false, bool formatLine = false) + { + pool = new DefaultObjectPoolProvider().Create(new JsonLogWriterPolicy(indended, formatLine)); } public static JsonLogWriterFactory Default() @@ -37,26 +62,12 @@ namespace Squidex.Infrastructure.Log public IObjectWriter Create() { - if (pool.TryPop(out var writer)) - { - writer.Reset(); - } - else - { - writer = new JsonLogWriter(formatting, formatLine); - } - - return writer; + return pool.Get(); } public void Release(IObjectWriter writer) { - var jsonWriter = (JsonLogWriter)writer; - - if (pool.Count < MaxPoolSize && jsonWriter.BufferSize < MaxCapacity) - { - pool.Push(jsonWriter); - } + pool.Return((JsonLogWriter)writer); } } } diff --git a/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj b/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj index 359ab434e..fd5850e99 100644 --- a/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj +++ b/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj @@ -19,6 +19,7 @@ + all diff --git a/backend/src/Squidex/Config/Domain/NotificationsServices.cs b/backend/src/Squidex/Config/Domain/NotificationsServices.cs index 951eaa4a1..3a9889866 100644 --- a/backend/src/Squidex/Config/Domain/NotificationsServices.cs +++ b/backend/src/Squidex/Config/Domain/NotificationsServices.cs @@ -8,7 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using Squidex.Domain.Apps.Entities.History.Notifications; +using Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications; using Squidex.Infrastructure.Email; using Squidex.Infrastructure.EventSourcing; diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/History/Notifications/NotificationEmailEventConsumerTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Invitation/Notifications/NotificationEmailEventConsumerTests.cs similarity index 98% rename from backend/tests/Squidex.Domain.Apps.Entities.Tests/History/Notifications/NotificationEmailEventConsumerTests.cs rename to backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Invitation/Notifications/NotificationEmailEventConsumerTests.cs index 954f2bef3..137cab05c 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/History/Notifications/NotificationEmailEventConsumerTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Invitation/Notifications/NotificationEmailEventConsumerTests.cs @@ -16,7 +16,7 @@ using Squidex.Infrastructure.Log; using Squidex.Shared.Users; using Xunit; -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public class NotificationEmailEventConsumerTests { diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/History/Notifications/NotificationEmailSenderTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Invitation/Notifications/NotificationEmailSenderTests.cs similarity index 98% rename from backend/tests/Squidex.Domain.Apps.Entities.Tests/History/Notifications/NotificationEmailSenderTests.cs rename to backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Invitation/Notifications/NotificationEmailSenderTests.cs index ef142e071..0ef451ae6 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/History/Notifications/NotificationEmailSenderTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Invitation/Notifications/NotificationEmailSenderTests.cs @@ -18,7 +18,7 @@ using Squidex.Shared.Identity; using Squidex.Shared.Users; using Xunit; -namespace Squidex.Domain.Apps.Entities.History.Notifications +namespace Squidex.Domain.Apps.Entities.Apps.Invitation.Notifications { public class NotificationEmailSenderTests {