Skip to content

Instantly share code, notes, and snippets.

@afscrome
Created November 1, 2019 15:20
Show Gist options
  • Save afscrome/bc4174bd70af9579dcc0479040af2503 to your computer and use it in GitHub Desktop.
Save afscrome/bc4174bd70af9579dcc0479040af2503 to your computer and use it in GitHub Desktop.
Cosmos Slow Networking
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
namespace Cosmos
{
class Program
{
private const string DatabaseUri = "https://asoslocal-eun-cus-profile-failover.documents.azure.com:443/";
private const string AuthKey = "";
private const string _databaseId = "Failover";
private const string _collectionId = "Customer";
private const string _documentId = "2405097";
private const string _keyValue = "2405097";
private const int _concurrencyLevel = 2;
private static DocumentClient _client;
static async Task Main(string[] args)
{
_client = Create();
await _client.OpenAsync();
var tasks = new Task[_concurrencyLevel];
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = Loop(i);
}
await Task.WhenAll(tasks);
}
static async Task Loop(int loopId)
{
while (true)
{
DateTime start = DateTime.Now;
try
{
await _client.ReadDocumentAsync(
UriFactory.CreateDocumentUri(_databaseId, _collectionId, _documentId)
, new RequestOptions { PartitionKey = new PartitionKey(_keyValue) });
Console.Write(loopId);
}
catch
{
var elapsed = DateTime.Now - start;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"Task {loopId}: {elapsed.TotalSeconds}");
Console.ResetColor();
}
}
}
public static DocumentClient Create()
{
var connectionPolicy = new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp,
RequestTimeout = TimeSpan.FromMilliseconds(1000),
EnableReadRequestsFallback = true,
EnableEndpointDiscovery = true,
RetryOptions = new RetryOptions
{
MaxRetryAttemptsOnThrottledRequests = 1,
MaxRetryWaitTimeInSeconds = 2
}
};
return new DocumentClient(new Uri(DatabaseUri),
AuthKey,
connectionPolicy
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment