From e3d0f06c5fc732c068eb5d135143fac3c2a6b01e Mon Sep 17 00:00:00 2001 From: Austin Drenski Date: Mon, 8 Jan 2024 11:35:11 -0500 Subject: [PATCH] perf: Cleanup allocations + missing ConfigureAwait's (#124) Signed-off-by: Austin Drenski --- .../FlagdConfig.cs | 8 +-- .../FlagdProvider.cs | 51 +++++++++---------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/OpenFeature.Contrib.Providers.Flagd/FlagdConfig.cs b/src/OpenFeature.Contrib.Providers.Flagd/FlagdConfig.cs index b6360448..7a048464 100644 --- a/src/OpenFeature.Contrib.Providers.Flagd/FlagdConfig.cs +++ b/src/OpenFeature.Contrib.Providers.Flagd/FlagdConfig.cs @@ -68,7 +68,7 @@ internal FlagdConfig() _socketPath = Environment.GetEnvironmentVariable(EnvVarSocketPath) ?? ""; var cacheStr = Environment.GetEnvironmentVariable(EnvVarCache) ?? ""; - if (cacheStr.ToUpper().Equals("LRU")) + if (string.Equals(cacheStr, "LRU", StringComparison.OrdinalIgnoreCase)) { _cache = true; _maxCacheSize = int.Parse(Environment.GetEnvironmentVariable(EnvVarMaxCacheSize) ?? $"{CacheSizeDefault}"); @@ -85,12 +85,12 @@ internal FlagdConfig(Uri url) _host = url.Host; _port = url.Port.ToString(); - _useTLS = url.Scheme.ToLower().Equals("https"); + _useTLS = string.Equals(url.Scheme, "https", StringComparison.OrdinalIgnoreCase); _cert = Environment.GetEnvironmentVariable(EnvCertPart) ?? ""; - _socketPath = url.Scheme.ToLower().Equals("unix") ? url.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Scheme, UriFormat.UriEscaped) : ""; + _socketPath = string.Equals(url.Scheme, "unix", StringComparison.OrdinalIgnoreCase) ? url.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Scheme, UriFormat.UriEscaped) : ""; var cacheStr = Environment.GetEnvironmentVariable(EnvVarCache) ?? ""; - if (cacheStr.ToUpper().Equals("LRU")) + if (string.Equals(cacheStr, "LRU", StringComparison.OrdinalIgnoreCase)) { _cache = true; _maxCacheSize = int.Parse(Environment.GetEnvironmentVariable(EnvVarMaxCacheSize) ?? $"{CacheSizeDefault}"); diff --git a/src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs b/src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs index 33d6c28c..cdb5f26d 100644 --- a/src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs +++ b/src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs @@ -91,7 +91,7 @@ public FlagdProvider(FlagdConfig config) _cache = new LRUCache(_config.MaxCacheSize); Task.Run(async () => { - await HandleEvents(); + await HandleEvents().ConfigureAwait(false); }); } } @@ -108,7 +108,7 @@ internal FlagdProvider(Service.ServiceClient client, FlagdConfig config, ICache< { Task.Run(async () => { - await HandleEvents(); + await HandleEvents().ConfigureAwait(false); }); } } @@ -149,7 +149,7 @@ public override async Task> ResolveBooleanValue(string f { Context = contextStruct, FlagKey = flagKey - }); + }).ConfigureAwait(false); return new ResolutionDetails( flagKey: flagKey, @@ -157,7 +157,7 @@ public override async Task> ResolveBooleanValue(string f reason: resolveBooleanResponse.Reason, variant: resolveBooleanResponse.Variant ); - }, context); + }, context).ConfigureAwait(false); } /// @@ -175,7 +175,7 @@ public override async Task> ResolveStringValue(string { Context = contextStruct, FlagKey = flagKey - }); + }).ConfigureAwait(false); return new ResolutionDetails( flagKey: flagKey, @@ -183,7 +183,7 @@ public override async Task> ResolveStringValue(string reason: resolveStringResponse.Reason, variant: resolveStringResponse.Variant ); - }, context); + }, context).ConfigureAwait(false); } /// @@ -201,7 +201,7 @@ public override async Task> ResolveIntegerValue(string fl { Context = contextStruct, FlagKey = flagKey - }); + }).ConfigureAwait(false); return new ResolutionDetails( flagKey: flagKey, @@ -209,7 +209,7 @@ public override async Task> ResolveIntegerValue(string fl reason: resolveIntResponse.Reason, variant: resolveIntResponse.Variant ); - }, context); + }, context).ConfigureAwait(false); } /// @@ -227,7 +227,7 @@ public override async Task> ResolveDoubleValue(string { Context = contextStruct, FlagKey = flagKey - }); + }).ConfigureAwait(false); return new ResolutionDetails( flagKey: flagKey, @@ -235,7 +235,7 @@ public override async Task> ResolveDoubleValue(string reason: resolveDoubleResponse.Reason, variant: resolveDoubleResponse.Variant ); - }, context); + }, context).ConfigureAwait(false); } /// @@ -253,7 +253,7 @@ public override async Task> ResolveStructureValue(strin { Context = contextStruct, FlagKey = flagKey - }); + }).ConfigureAwait(false); return new ResolutionDetails( flagKey: flagKey, @@ -261,7 +261,7 @@ public override async Task> ResolveStructureValue(strin reason: resolveObjectResponse.Reason, variant: resolveObjectResponse.Variant ); - }, context); + }, context).ConfigureAwait(false); } private async Task> ResolveValue(string flagKey, Func>> resolveDelegate, EvaluationContext context = null) @@ -277,9 +277,9 @@ private async Task> ResolveValue(string flagKey, Func)value; } } - var result = await resolveDelegate.Invoke(ConvertToContext(context)); + var result = await resolveDelegate.Invoke(ConvertToContext(context)).ConfigureAwait(false); - if (result.Reason.Equals("STATIC") && _config.CacheEnabled) + if (string.Equals(result.Reason, "STATIC", StringComparison.Ordinal) && _config.CacheEnabled) { _cache.Add(flagKey, result); } @@ -320,27 +320,24 @@ private async Task HandleEvents() try { // Read the response stream asynchronously - while (await call.ResponseStream.MoveNext()) + while (await call.ResponseStream.MoveNext().ConfigureAwait(false)) { var response = call.ResponseStream.Current; - switch (response.Type.ToLower()) + if (string.Equals(response.Type, "configuration_change", StringComparison.OrdinalIgnoreCase)) + { + HandleConfigurationChangeEvent(response.Data); + } + else if (string.Equals(response.Type, "provider_ready", StringComparison.OrdinalIgnoreCase)) { - case "configuration_change": - HandleConfigurationChangeEvent(response.Data); - break; - case "provider_ready": - HandleProviderReadyEvent(); - break; - default: - break; + HandleProviderReadyEvent(); } } } catch (RpcException ex) when (ex.StatusCode == StatusCode.Unavailable) { // Handle the dropped connection by reconnecting and retrying the stream - await HandleErrorEvent(); + await HandleErrorEvent().ConfigureAwait(false); } } } @@ -395,7 +392,7 @@ private async Task HandleErrorEvent() } _eventStreamRetryBackoff = _eventStreamRetryBackoff * 2; _mtx.ReleaseMutex(); - await Task.Delay(_eventStreamRetryBackoff * 1000); + await Task.Delay(_eventStreamRetryBackoff * 1000).ConfigureAwait(false); } /// @@ -519,7 +516,7 @@ private static Value ConvertToPrimitiveValue(ProtoValue value) private Service.ServiceClient BuildClientForPlatform(Uri url) { - var useUnixSocket = url.ToString().StartsWith("unix://"); + var useUnixSocket = string.Equals(url.Scheme, "unix", StringComparison.Ordinal); if (!useUnixSocket) {