Skip to content
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

cache all appcontext switches #2227

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal static partial class LocalAppContextSwitches

private static bool? s_legacyRowVersionNullBehavior;
private static bool? s_suppressInsecureTLSWarning;
private static bool s_makeReadAsyncBlocking;
private static bool s_useMinimumLoginTimeout;
private static bool? s_makeReadAsyncBlocking;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool? is a structure with two fields. It is not updated atomically. The lock-free reads and writes below have race conditions.

If you want the reads to be lock-free, you can either:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've updated all the variables to use the tristate enum pattern.

private static bool? s_useMinimumLoginTimeout;

#if !NETFRAMEWORK
static LocalAppContextSwitches()
Expand All @@ -36,7 +36,7 @@ static LocalAppContextSwitches()

#if NETFRAMEWORK
internal const string DisableTNIRByDefaultString = @"Switch.Microsoft.Data.SqlClient.DisableTNIRByDefaultInConnectionString";
private static bool s_disableTNIRByDefault;
private static bool? s_disableTNIRByDefault;

/// <summary>
/// Transparent Network IP Resolution (TNIR) is a revision of the existing MultiSubnetFailover feature.
Expand All @@ -54,7 +54,17 @@ static LocalAppContextSwitches()
/// This app context switch defaults to 'false'.
/// </summary>
public static bool DisableTNIRByDefault
=> AppContext.TryGetSwitch(DisableTNIRByDefaultString, out s_disableTNIRByDefault) && s_disableTNIRByDefault;
{
get
{
if (s_disableTNIRByDefault is null)
{
bool result = AppContext.TryGetSwitch(DisableTNIRByDefaultString, out bool returnedValue) && returnedValue;
s_disableTNIRByDefault = result;
}
return s_disableTNIRByDefault.Value;
}
}
#endif

/// <summary>
Expand Down Expand Up @@ -101,14 +111,34 @@ public static bool LegacyRowVersionNullBehavior
/// This app context switch defaults to 'false'.
/// </summary>
public static bool MakeReadAsyncBlocking
=> AppContext.TryGetSwitch(MakeReadAsyncBlockingString, out s_makeReadAsyncBlocking) && s_makeReadAsyncBlocking;
{
get
{
if (s_makeReadAsyncBlocking is null)
{
bool result = AppContext.TryGetSwitch(MakeReadAsyncBlockingString, out bool returnedValue) && returnedValue;
s_makeReadAsyncBlocking = result;
}
return s_makeReadAsyncBlocking.Value;
}
}

/// <summary>
/// Specifies minimum login timeout to be set to 1 second instead of 0 seconds,
/// to prevent a login attempt from waiting indefinitely.
/// This app context switch defaults to 'true'.
/// </summary>
public static bool UseMinimumLoginTimeout
=> !AppContext.TryGetSwitch(UseMinimumLoginTimeoutString, out s_useMinimumLoginTimeout) || s_useMinimumLoginTimeout;
{
get
{
if (s_useMinimumLoginTimeout is null)
{
bool result = AppContext.TryGetSwitch(UseMinimumLoginTimeoutString, out bool returnedValue) && returnedValue;
s_useMinimumLoginTimeout = result;
}
return s_useMinimumLoginTimeout.Value;
}
}
}
}
Loading