Skip to content

Commit

Permalink
perf: Cleanup allocations + missing ConfigureAwait's (#124)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Drenski <[email protected]>
  • Loading branch information
austindrenski authored Jan 8, 2024
1 parent 2386562 commit e3d0f06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/OpenFeature.Contrib.Providers.Flagd/FlagdConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand All @@ -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}");
Expand Down
51 changes: 24 additions & 27 deletions src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public FlagdProvider(FlagdConfig config)
_cache = new LRUCache<string, object>(_config.MaxCacheSize);
Task.Run(async () =>
{
await HandleEvents();
await HandleEvents().ConfigureAwait(false);
});
}
}
Expand All @@ -108,7 +108,7 @@ internal FlagdProvider(Service.ServiceClient client, FlagdConfig config, ICache<
{
Task.Run(async () =>
{
await HandleEvents();
await HandleEvents().ConfigureAwait(false);
});
}
}
Expand Down Expand Up @@ -149,15 +149,15 @@ public override async Task<ResolutionDetails<bool>> ResolveBooleanValue(string f
{
Context = contextStruct,
FlagKey = flagKey
});
}).ConfigureAwait(false);
return new ResolutionDetails<bool>(
flagKey: flagKey,
value: (bool)resolveBooleanResponse.Value,
reason: resolveBooleanResponse.Reason,
variant: resolveBooleanResponse.Variant
);
}, context);
}, context).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -175,15 +175,15 @@ public override async Task<ResolutionDetails<string>> ResolveStringValue(string
{
Context = contextStruct,
FlagKey = flagKey
});
}).ConfigureAwait(false);
return new ResolutionDetails<string>(
flagKey: flagKey,
value: resolveStringResponse.Value,
reason: resolveStringResponse.Reason,
variant: resolveStringResponse.Variant
);
}, context);
}, context).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -201,15 +201,15 @@ public override async Task<ResolutionDetails<int>> ResolveIntegerValue(string fl
{
Context = contextStruct,
FlagKey = flagKey
});
}).ConfigureAwait(false);
return new ResolutionDetails<int>(
flagKey: flagKey,
value: (int)resolveIntResponse.Value,
reason: resolveIntResponse.Reason,
variant: resolveIntResponse.Variant
);
}, context);
}, context).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -227,15 +227,15 @@ public override async Task<ResolutionDetails<double>> ResolveDoubleValue(string
{
Context = contextStruct,
FlagKey = flagKey
});
}).ConfigureAwait(false);
return new ResolutionDetails<double>(
flagKey: flagKey,
value: resolveDoubleResponse.Value,
reason: resolveDoubleResponse.Reason,
variant: resolveDoubleResponse.Variant
);
}, context);
}, context).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -253,15 +253,15 @@ public override async Task<ResolutionDetails<Value>> ResolveStructureValue(strin
{
Context = contextStruct,
FlagKey = flagKey
});
}).ConfigureAwait(false);
return new ResolutionDetails<Value>(
flagKey: flagKey,
value: ConvertObjectToValue(resolveObjectResponse.Value),
reason: resolveObjectResponse.Reason,
variant: resolveObjectResponse.Variant
);
}, context);
}, context).ConfigureAwait(false);
}

private async Task<ResolutionDetails<T>> ResolveValue<T>(string flagKey, Func<Struct, Task<ResolutionDetails<T>>> resolveDelegate, EvaluationContext context = null)
Expand All @@ -277,9 +277,9 @@ private async Task<ResolutionDetails<T>> ResolveValue<T>(string flagKey, Func<St
return (ResolutionDetails<T>)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);
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit e3d0f06

Please sign in to comment.