Skip to content

Instantly share code, notes, and snippets.

@Romiko
Created November 1, 2011 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Romiko/1332327 to your computer and use it in GitHub Desktop.
Save Romiko/1332327 to your computer and use it in GitHub Desktop.
Sample Circuit breaker usage with Neo4j and thread optimization
void CreateClients(MigrationMessage migrationMessage)
{
var agencyUniqueId = migrationMessage.AgencyUniqueId;
var agency = agencyService.GetAgencyByUniqueId(agencyUniqueId);
if (agency == null)
throw new ApplicationException("Agency unique id provided for the import process does not match any existing agencies.");
using (var connection = new SqlCeConnection(connectionString))
using (var canonicalDb = new ImportDbContext(connection))
{
var clients = canonicalDb.Clients;
var count = 0;
var total = clients.Count();
Log("Importing {0} clients into the database", total);
var startedAt = DateTimeOffset.UtcNow;
var lastLoggedAt = DateTimeOffset.UtcNow;
var statusLogLock = new object();
circuitBreaker.StateChanged += CircuitBreakerStateChanged;
circuitBreaker.ServiceLevelChanged += CircuitBreakerServiceLevelChanged;
const ushort allowedRetries = 20;
var retryInterval = TimeSpan.FromSeconds(30);
var processorCount = Environment.ProcessorCount;
const int neo4JDefaultThreadsPerCore = 10;
const double percentageOfNeo4JThreadsToAllocate = 0.9;
var threadsToAllocate = (int)Math.Ceiling(processorCount*neo4JDefaultThreadsPerCore * percentageOfNeo4JThreadsToAllocate);
var options = new ParallelOptions { MaxDegreeOfParallelism = threadsToAllocate };
Parallel.ForEach(clients, options, client =>
{
Interlocked.Increment(ref count);
try
{
circuitBreaker.ExecuteWithRetries(() => importClientService.ImportClient(client, agency),
allowedRetries,
retryInterval);
}
catch (Exception ex)
{
Log("Failed importing client\r\n" +
"Client object was: {0}\r\n" +
"Exception was: {1}",
SerializationHelper.Serialize(client),
ex.ToString());
throw;
}
LogStatusEveryThousandClientsOrEveryThirtySeconds(ref count, total, statusLogLock, startedAt, ref lastLoggedAt);
});
Log("All {0} clients imported", total);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment