Home > Software engineering >  What happens if you don't set ConnectionPolicy.ConnectionProtocol when using ConnectionMode.Dir
What happens if you don't set ConnectionPolicy.ConnectionProtocol when using ConnectionMode.Dir

Time:11-14

I was reading this article about Azure CosmosDB connection policies.

It says if you're using ConnectionMode = ConnectionMode.Direct, then you also need to set the ConnectionProtocol.

This article says ConnectionMode.Direct uses TCP, so I assume I should set ConnectionProtocol to Protocol.Tcp

Since the default value for the Protocol enum is Https, what would happen if you don't set it, would it default to Https?

If it does default to Https, would this cause any issues?

Here's my code:

var connectionPolicy = new ConnectionPolicy
{
    //ConnectionProtocol = Protocol.Tcp, // This is not set!
    ConnectionMode = ConnectionMode.Direct,
    RequestTimeout = TimeSpan.FromSeconds(10),
    UserAgentSuffix = "ING",
    RetryOptions = new RetryOptions
    {
        MaxRetryAttemptsOnThrottledRequests = 5,
        MaxRetryWaitTimeInSeconds = 2
    }
};

var client = new DocumentClient(new Uri(endpoint), authKey, connectionPolicy, ConsistencyLevel.Session);

Here's the Protocol enum for reference:

public enum Protocol
{           
    Https,            
    Tcp
}

We've started getting ServiceUnavailableException and SNAT port exhaustion errors, so am wondering if this has anything to do with it.

CodePudding user response:

For V2 SDK the default would be Https. You would end up in Direct/HTTPS mode, not Direct/TCP nor Gateway.

Direct/HTTPS while valid and would work, is no longer part of any of the recommendations on the docs (https://docs.microsoft.com/azure/cosmos-db/sql/sql-sdk-connection-modes). If you need to use HTTPs, use Gateway, if you want to use Direct, use Direct/TCP.

The V3 SDK removed the possibility of selecting Direct/HTTPS and it's just Direct (TCP) or Gateway (HTTP).

Regarding SNAT, please follow https://docs.microsoft.com/azure/cosmos-db/sql/troubleshoot-service-unavailable

Direct mode will use a higher number of connections by design, make sure the machine can support that and that you are following the Singleton pattern to avoid creating more connections instead of reusing them.

  • Related