From ffb5eb294a43e0f672cdebce8c462b4f40c77016 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 30 Sep 2024 15:23:14 +0200 Subject: [PATCH] Fix kubernetes port name issue --- Framework/KubernetesWorkflow/K8sNameUtils.cs | 2 +- Framework/Utils/RandomUtils.cs | 25 +++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Framework/KubernetesWorkflow/K8sNameUtils.cs b/Framework/KubernetesWorkflow/K8sNameUtils.cs index f620e7d..2c9b1e5 100644 --- a/Framework/KubernetesWorkflow/K8sNameUtils.cs +++ b/Framework/KubernetesWorkflow/K8sNameUtils.cs @@ -24,8 +24,8 @@ .Replace("]", "-") .Replace(",", "-"); - result = result.Trim('-'); if (result.Length > maxLength) result = result.Substring(0, maxLength); + result = result.Trim('-'); return result; } diff --git a/Framework/Utils/RandomUtils.cs b/Framework/Utils/RandomUtils.cs index acece43..ae93c41 100644 --- a/Framework/Utils/RandomUtils.cs +++ b/Framework/Utils/RandomUtils.cs @@ -3,24 +3,31 @@ public static class RandomUtils { private static readonly Random random = new Random(); + private static readonly object @lock = new object(); public static T PickOneRandom(this List remainingItems) { - var i = random.Next(0, remainingItems.Count); - var result = remainingItems[i]; - remainingItems.RemoveAt(i); - return result; + lock (@lock) + { + var i = random.Next(0, remainingItems.Count); + var result = remainingItems[i]; + remainingItems.RemoveAt(i); + return result; + } } public static T[] Shuffled(T[] items) { - var result = new List(); - var source = items.ToList(); - while (source.Any()) + lock (@lock) { - result.Add(RandomUtils.PickOneRandom(source)); + var result = new List(); + var source = items.ToList(); + while (source.Any()) + { + result.Add(RandomUtils.PickOneRandom(source)); + } + return result.ToArray(); } - return result.ToArray(); } } }