From 3770d23fff767be48a4b81bd030ef3f638531858 Mon Sep 17 00:00:00 2001 From: Nathan Willoughby Date: Thu, 30 Nov 2023 11:36:37 +1000 Subject: [PATCH] Enable Halibut TCP Keep Alives by default --- .../TentacleCommunicationsModuleFixture.cs | 90 +++++++++++++++++++ .../TentacleCommunicationsModule.cs | 7 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 source/Octopus.Tentacle.Tests/Communications/TentacleCommunicationsModuleFixture.cs diff --git a/source/Octopus.Tentacle.Tests/Communications/TentacleCommunicationsModuleFixture.cs b/source/Octopus.Tentacle.Tests/Communications/TentacleCommunicationsModuleFixture.cs new file mode 100644 index 000000000..9efad79ed --- /dev/null +++ b/source/Octopus.Tentacle.Tests/Communications/TentacleCommunicationsModuleFixture.cs @@ -0,0 +1,90 @@ +using System; +using Autofac; +using FluentAssertions; +using Halibut; +using NUnit.Framework; +using Octopus.Tentacle.Communications; +using Octopus.Tentacle.Configuration; +using Octopus.Tentacle.Tests.Commands; +using Octopus.Tentacle.Variables; + +namespace Octopus.Tentacle.Tests.Communications +{ + public class TentacleCommunicationsModuleFixture + { + [Test] + public void HalibutTcpKeepAlivesShouldBeEnabledByDefault() + { + var container = BuildContainer(); + + var halibutRuntime = container.Resolve(); + + halibutRuntime.TimeoutsAndLimits.TcpKeepAliveEnabled.Should().BeTrue(); + } + + [Test] + [NonParallelizable] + public void HalibutTcpKeepAlivesCanBeDisabledWithAnEnvironmentVariable() + { + try + { + Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled, "False"); + + var container = BuildContainer(); + + var halibutRuntime = container.Resolve(); + + halibutRuntime.TimeoutsAndLimits.TcpKeepAliveEnabled.Should().BeFalse(); + } + finally + { + Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled, ""); + } + } + + [Test] + [NonParallelizable] + public void AnEmptyHalibutTcpKeepAlivesEnvironmentVariableIsIgnored() + { + Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled, ""); + + var container = BuildContainer(); + + var halibutRuntime = container.Resolve(); + + halibutRuntime.TimeoutsAndLimits.TcpKeepAliveEnabled.Should().BeTrue(); + } + + [Test] + [NonParallelizable] + public void AnInvalidHalibutTcpKeepAlivesEnvironmentVariableIsIgnored() + { + try + { + Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled, "NOTABOOL"); + + var container = BuildContainer(); + + var halibutRuntime = container.Resolve(); + + halibutRuntime.TimeoutsAndLimits.TcpKeepAliveEnabled.Should().BeTrue(); + } + finally + { + Environment.SetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled, ""); + } + } + + static IContainer BuildContainer() + { + var builder = new ContainerBuilder(); + var configuration = new StubTentacleConfiguration(); + configuration.TentacleCertificate = configuration.GenerateNewCertificate(); + configuration.AddOrUpdateTrustedOctopusServer(new OctopusServerConfiguration("NOPE")); + builder.RegisterInstance(configuration).As(); + builder.RegisterModule(); + var container = builder.Build(); + return container; + } + } +} \ No newline at end of file diff --git a/source/Octopus.Tentacle/Communications/TentacleCommunicationsModule.cs b/source/Octopus.Tentacle/Communications/TentacleCommunicationsModule.cs index 1b3c3f114..95e34c10a 100644 --- a/source/Octopus.Tentacle/Communications/TentacleCommunicationsModule.cs +++ b/source/Octopus.Tentacle/Communications/TentacleCommunicationsModule.cs @@ -25,7 +25,12 @@ protected override void Load(ContainerBuilder builder) var configuration = c.Resolve(); var services = c.Resolve(); - bool.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled), out var tcpKeepAliveEnabled); + if (!bool.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariables.TentacleTcpKeepAliveEnabled), out var tcpKeepAliveEnabled)) + { + // Default to enabled if the environment variable is not provided + tcpKeepAliveEnabled = true; + } + bool.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariables.TentacleUseRecommendedTimeoutsAndLimits), out var useRecommendedTimeoutsAndLimits); var halibutTimeoutsAndLimits = useRecommendedTimeoutsAndLimits