From 645e4d8112cad40d5716325f2cea021acd5d341c Mon Sep 17 00:00:00 2001 From: Nick Craver Date: Wed, 9 Feb 2022 00:00:21 -0500 Subject: [PATCH 1/2] Client name: append library version suffix This changes the default client name to append "(v2.5.x)" on the end (no spaces because Redis doesn't support spaces in client names). It won't append if someone is setting their client name explicitly in options, only for our default, so should be a safe change. --- Directory.Build.props | 2 +- .../ConnectionMultiplexer.cs | 7 +++++-- src/StackExchange.Redis/ExceptionFactory.cs | 13 +------------ src/StackExchange.Redis/PhysicalConnection.cs | 2 +- src/StackExchange.Redis/Utils.cs | 19 +++++++++++++++++++ tests/StackExchange.Redis.Tests/Config.cs | 4 ++-- .../ExceptionFactoryTests.cs | 2 +- 7 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/StackExchange.Redis/Utils.cs diff --git a/Directory.Build.props b/Directory.Build.props index 6227de316..e66dd6b0a 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -15,7 +15,7 @@ https://github.com/StackExchange/StackExchange.Redis/ MIT - 9.0 + 10.0 git https://github.com/StackExchange/StackExchange.Redis/ diff --git a/src/StackExchange.Redis/ConnectionMultiplexer.cs b/src/StackExchange.Redis/ConnectionMultiplexer.cs index 3b827f7f8..880e598c6 100644 --- a/src/StackExchange.Redis/ConnectionMultiplexer.cs +++ b/src/StackExchange.Redis/ConnectionMultiplexer.cs @@ -149,12 +149,15 @@ public ServerCounters GetCounters() private static string defaultClientName; + /// + /// Gets the client name for a connection, with the library version appended. + /// private static string GetDefaultClientName() { - return defaultClientName ??= TryGetAzureRoleInstanceIdNoThrow() + return defaultClientName ??= (TryGetAzureRoleInstanceIdNoThrow() ?? Environment.MachineName ?? Environment.GetEnvironmentVariable("ComputerName") - ?? "StackExchange.Redis"; + ?? "StackExchange.Redis") + "(v" + Utils.GetLibVersion() + ")"; } /// diff --git a/src/StackExchange.Redis/ExceptionFactory.cs b/src/StackExchange.Redis/ExceptionFactory.cs index 598517d4b..92bbb2a0e 100644 --- a/src/StackExchange.Redis/ExceptionFactory.cs +++ b/src/StackExchange.Redis/ExceptionFactory.cs @@ -199,17 +199,6 @@ internal static Exception NoCursor(RedisCommand command) return new RedisCommandException("Command cannot be used with a cursor: " + s); } - private static string _libVersion; - internal static string GetLibVersion() - { - if (_libVersion == null) - { - var assembly = typeof(ConnectionMultiplexer).Assembly; - _libVersion = ((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute)))?.Version - ?? assembly.GetName().Version.ToString(); - } - return _libVersion; - } private static void Add(List> data, StringBuilder sb, string lk, string sk, string v) { if (v != null) @@ -365,7 +354,7 @@ ServerEndPoint server Add(data, sb, "Local-CPU", "Local-CPU", PerfCounterHelper.GetSystemCpuPercent()); } - Add(data, sb, "Version", "v", GetLibVersion()); + Add(data, sb, "Version", "v", Utils.GetLibVersion()); } private static void AddExceptionDetail(Exception exception, Message message, ServerEndPoint server, string label) diff --git a/src/StackExchange.Redis/PhysicalConnection.cs b/src/StackExchange.Redis/PhysicalConnection.cs index 27f17a6ba..10da21fe1 100644 --- a/src/StackExchange.Redis/PhysicalConnection.cs +++ b/src/StackExchange.Redis/PhysicalConnection.cs @@ -434,7 +434,7 @@ void add(string lk, string sk, string v) } } - add("Version", "v", ExceptionFactory.GetLibVersion()); + add("Version", "v", Utils.GetLibVersion()); outerException = new RedisConnectionException(failureType, exMessage.ToString(), innerException); diff --git a/src/StackExchange.Redis/Utils.cs b/src/StackExchange.Redis/Utils.cs new file mode 100644 index 000000000..b5ec20670 --- /dev/null +++ b/src/StackExchange.Redis/Utils.cs @@ -0,0 +1,19 @@ +using System; +using System.Reflection; + +namespace StackExchange.Redis; + +internal static class Utils +{ + private static string _libVersion; + internal static string GetLibVersion() + { + if (_libVersion == null) + { + var assembly = typeof(ConnectionMultiplexer).Assembly; + _libVersion = ((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute)))?.Version + ?? assembly.GetName().Version.ToString(); + } + return _libVersion; + } +} diff --git a/tests/StackExchange.Redis.Tests/Config.cs b/tests/StackExchange.Redis.Tests/Config.cs index 99ef60996..271e8a386 100644 --- a/tests/StackExchange.Redis.Tests/Config.cs +++ b/tests/StackExchange.Redis.Tests/Config.cs @@ -243,12 +243,12 @@ public void DefaultClientName() { using (var muxer = Create(allowAdmin: true, caller: null)) // force default naming to kick in { - Assert.Equal(Environment.MachineName, muxer.ClientName); + Assert.Equal($"{Environment.MachineName}(v{Utils.GetLibVersion()})", muxer.ClientName); var conn = muxer.GetDatabase(); conn.Ping(); var name = (string)GetAnyMaster(muxer).Execute("CLIENT", "GETNAME"); - Assert.Equal(Environment.MachineName, name); + Assert.Equal($"{Environment.MachineName}(v{Utils.GetLibVersion()})", name); } } diff --git a/tests/StackExchange.Redis.Tests/ExceptionFactoryTests.cs b/tests/StackExchange.Redis.Tests/ExceptionFactoryTests.cs index 31585159f..8c84227be 100644 --- a/tests/StackExchange.Redis.Tests/ExceptionFactoryTests.cs +++ b/tests/StackExchange.Redis.Tests/ExceptionFactoryTests.cs @@ -23,7 +23,7 @@ public void NullLastException() [Fact] public void CanGetVersion() { - var libVer = ExceptionFactory.GetLibVersion(); + var libVer = Utils.GetLibVersion(); Assert.Matches(@"2\.[0-9]+\.[0-9]+(\.[0-9]+)?", libVer); } From 2dc916d185f56494236c8b765b3587bf59978b39 Mon Sep 17 00:00:00 2001 From: Nick Craver Date: Wed, 9 Feb 2022 00:03:05 -0500 Subject: [PATCH 2/2] Add release notes --- docs/ReleaseNotes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 64b26cac4..396525601 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -14,6 +14,7 @@ - Fixes a race in subscribing immediately before a publish - Fixes subscription routing on clusters (spreading instead of choosing 1 node) - More correctly reconnects subscriptions on connection failures, including to other endpoints +- Adds "(vX.X.X)" version suffix to the default client ID so server-side `CLIENT LIST` can more easily see what's connected (#1985 via NickCraver) ## 2.2.88