Skip to content

Added lock around adding to _enumCache #1858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions src/Nest/CommonAbstractions/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace Nest
internal static class Extensions
{
internal static TReturn InvokeOrDefault<T, TReturn>(this Func<T, TReturn> func, T @default)
where T: class, TReturn where TReturn: class =>
where T : class, TReturn where TReturn : class =>
func?.Invoke(@default) ?? @default;

internal static TReturn InvokeOrDefault<T1, T2, TReturn>(this Func<T1, T2, TReturn> func, T1 @default, T2 param2)
where T1: class, TReturn where TReturn: class =>
where T1 : class, TReturn where TReturn : class =>
func?.Invoke(@default, param2) ?? @default;

internal static QueryContainer InvokeQuery<T>(
this Func<QueryContainerDescriptor<T>, QueryContainer> f,
QueryContainerDescriptor<T> container)
Expand All @@ -33,7 +33,7 @@ internal static QueryContainer InvokeQuery<T>(
return c;

//query is conditionless but the container is marked as strict, throw exception
if (c != null && c.IsStrict)
if (c != null && c.IsStrict)
throw new ArgumentException("Query is conditionless but strict is turned on");

//query is conditionless return an empty container that can later be rewritten
Expand All @@ -53,7 +53,7 @@ internal static string GetStringValue(this Enum enumValue)

return da != null ? da.Value : Enum.GetName(enumValue.GetType(), enumValue);
}

internal static readonly JsonConverter dateConverter = new IsoDateTimeConverter { Culture = CultureInfo.InvariantCulture };
internal static readonly JsonSerializer serializer = new JsonSerializer();
internal static string ToJsonNetString(this DateTime date)
Expand All @@ -78,34 +78,43 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
if (_enumCache.ContainsKey(key))
return (T)_enumCache[key];

foreach (var name in Enum.GetNames(enumType))
// make sure that only one thread can be adding to cache
lock (_enumCache)
{
if (name.Equals(str, StringComparison.OrdinalIgnoreCase)) return (T)Enum.Parse(enumType, name, true);
// Check to see if cache now contains key
if (_enumCache.ContainsKey(key))
return (T)_enumCache[key];

var enumFieldInfo = enumType.GetField(name);
var enumMemberAttribute = enumFieldInfo.GetCustomAttribute<EnumMemberAttribute>();

if (enumMemberAttribute != null)
foreach (var name in Enum.GetNames(enumType))
{
if (enumMemberAttribute.Value == str)
if (name.Equals(str, StringComparison.OrdinalIgnoreCase)) return (T)Enum.Parse(enumType, name, true);

var enumFieldInfo = enumType.GetField(name);
var enumMemberAttribute = enumFieldInfo.GetCustomAttribute<EnumMemberAttribute>();

if (enumMemberAttribute != null)
{
var value = (T)Enum.Parse(enumType, name);
_enumCache.Add(key, value);
return value;
if (enumMemberAttribute.Value == str)
{
var value = (T)Enum.Parse(enumType, name);
_enumCache.Add(key, value);
return value;
}
}
}

var alternativeEnumMemberAttribute = enumFieldInfo.GetCustomAttribute<AlternativeEnumMemberAttribute>();
var alternativeEnumMemberAttribute = enumFieldInfo.GetCustomAttribute<AlternativeEnumMemberAttribute>();

if (alternativeEnumMemberAttribute != null)
{
if (alternativeEnumMemberAttribute.Value == str)
{
var value = (T)Enum.Parse(enumType, name);
_enumCache.Add(key, value);
return value;
if (alternativeEnumMemberAttribute != null)
{
if (alternativeEnumMemberAttribute.Value == str)
{
var value = (T)Enum.Parse(enumType, name);
_enumCache.Add(key, value);
return value;
}
}
}

}

return null;
Expand Down