Skip to content

avoid some allocations when opening a connection #3364

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using Microsoft.Identity.Client;
using Microsoft.SqlServer.Server;
using System.Security.Authentication;
using System.Linq;


#if NETFRAMEWORK
using System.Reflection;
Expand Down Expand Up @@ -762,29 +764,54 @@ internal static Version GetAssemblyVersion()
private const string AZURE_SQL_CHINA = ".database.chinacloudapi.cn";
private const string AZURE_SQL_FABRIC = ".database.fabric.microsoft.com";

internal static bool IsAzureSynapseOnDemandEndpoint(string dataSource)
{
return IsEndpoint(dataSource, ONDEMAND_PREFIX)
|| dataSource.Contains(AZURE_SYNAPSE)
|| dataSource.Contains(FABRIC_DATAWAREHOUSE)
|| dataSource.Contains(PBI_DATAWAREHOUSE)
|| dataSource.Contains(PBI_DATAWAREHOUSE2)
|| dataSource.Contains(PBI_DATAWAREHOUSE3);
}

/// <summary>
/// Represents a collection of Azure SQL Server endpoint URLs for various regions and environments.
/// </summary>
/// <remarks>This array includes endpoint URLs for Azure SQL in global, Germany, US Government,
/// China, and Fabric environments. These endpoints are used to identify and interact with Azure SQL services
/// in their respective regions or environments.</remarks>
internal static readonly string[] s_azureSqlServerEndpoints = { AZURE_SQL,
AZURE_SQL_GERMANY,
AZURE_SQL_USGOV,
AZURE_SQL_CHINA,
AZURE_SQL_FABRIC };


/// <summary>
/// Contains endpoint strings for Azure SQL Server on-demand services.
/// Each entry is a combination of the ONDEMAND_PREFIX and a specific Azure SQL endpoint string.
/// Example format: "ondemand.database.windows.net".
/// </summary>
internal static readonly string[] s_azureSqlServerOnDemandEndpoints = { ONDEMAND_PREFIX + AZURE_SQL,
ONDEMAND_PREFIX + AZURE_SQL_GERMANY,
ONDEMAND_PREFIX + AZURE_SQL_USGOV,
ONDEMAND_PREFIX + AZURE_SQL_CHINA,
ONDEMAND_PREFIX + AZURE_SQL_FABRIC };
/// <summary>
/// Represents a collection of endpoint identifiers for Azure Synapse and related services.
/// </summary>
/// <remarks>This array contains predefined endpoint strings used to identify Azure Synapse and
/// associated services, such as Fabric Data Warehouse and Power BI Data Warehouse.</remarks>
internal static readonly string[] s_azureSynapseEndpoints = { AZURE_SYNAPSE,
FABRIC_DATAWAREHOUSE,
PBI_DATAWAREHOUSE,
PBI_DATAWAREHOUSE2,
PBI_DATAWAREHOUSE3 };

internal static readonly string[] s_azureSynapseOnDemandEndpoints = s_azureSqlServerOnDemandEndpoints
.Concat(s_azureSynapseEndpoints)
.ToArray();
internal static bool IsAzureSynapseOnDemandEndpoint(string dataSource)
{
return IsEndpoint(dataSource, s_azureSynapseOnDemandEndpoints);
}

internal static bool IsAzureSqlServerEndpoint(string dataSource)
{
return IsEndpoint(dataSource, null);
return IsEndpoint(dataSource, s_azureSqlServerEndpoints);
}

// This method assumes dataSource parameter is in TCP connection string format.
private static bool IsEndpoint(string dataSource, string prefix)
private static bool IsEndpoint(string dataSource, string[] endpoints)
{
int length = dataSource.Length;
// remove server port
Expand All @@ -805,8 +832,6 @@ private static bool IsEndpoint(string dataSource, string prefix)
foundIndex = -1;
}



if (foundIndex > 0)
{
length = foundIndex;
Expand All @@ -819,9 +844,8 @@ private static bool IsEndpoint(string dataSource, string prefix)
}

// check if servername ends with any endpoints
for (int index = 0; index < s_azureSqlServerEndpoints.Length; index++)
foreach (var endpoint in endpoints)
{
string endpoint = string.IsNullOrEmpty(prefix) ? s_azureSqlServerEndpoints[index] : prefix + s_azureSqlServerEndpoints[index];
if (length > endpoint.Length)
{
if (string.Compare(dataSource, length - endpoint.Length, endpoint, 0, endpoint.Length, StringComparison.OrdinalIgnoreCase) == 0)
Expand Down
Loading