Skip to content

Instantly share code, notes, and snippets.

@Im5tu
Last active January 7, 2018 21:53
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 Im5tu/51ce3280674e7b8f03939c5aaa0dd52f to your computer and use it in GitHub Desktop.
Save Im5tu/51ce3280674e7b8f03939c5aaa0dd52f to your computer and use it in GitHub Desktop.
Correlation Part 1: The Context
public static class CorrelationContext
{
public static Stack<Guid> GetCorrelationStack() {}
  public static Guid? GetCorrelationId() {}
  public static IDisposable CreateCorrelation(Guid? correlationId = null) {}
  public static IDisposable CreateCorrelationIfNotExists(Guid? correlationId = null) {}
}
class Program
{
static void Main(string[] args)
{
using(CorrelationContext.CreateCorrelationIfNotExists())
{
Console.WriteLine(CorrelationContext.GetCorrelationId());
using(CorrelationContext.CreateCorrelationIfNotExists())
{
Console.WriteLine(CorrelationContext.GetCorrelationId());
}
}
}
}
public static class CorrelationContext
{
private static readonly AsyncLocal<Stack<Guid>> _correlationStack = new AsyncLocal<Stack<Guid>>();
public static Stack<Guid> GetCorrelationStack() => _correlationStack.Value ?? new Stack<Guid>();
public static Guid? GetCorrelationId()
{
var stack = GetCorrelationStack();
if(stack.Count > 0)
return stack.Peek();
return null; // TODO :: decide what you are going to do
}
public static IDisposable CreateCorrelation(Guid? correlationId = null)
{
lock(_correlationStack)
{
if (_correlationStack.Value == null)
_correlationStack.Value = new Stack<Guid>();
_correlationStack.Value.Push(correlationId ?? Guid.NewGuid());
}
return new DisposeCorrelation(_correlationStack);
}
public static IDisposable CreateCorrelationIfNotExists(Guid? correlationId = null)
{
if(GetCorrelationStack().Count > 0)
return new EmptyDisposable();
return CreateCorrelation(correlationId);
}
private class EmptyDisposable : IDisposable { public void Dispose() { } }
private class DisposeCorrelation : IDisposable
{
private readonly AsyncLocal<Stack<Guid>> _localStack;
public DisposeCorrelation(AsyncLocal<Stack<Guid>> localStack)
{
_localStack = localStack;
}
public void Dispose()
{
var stack = _localStack.Value ?? new Stack<Guid>();
if(stack.Count > 0)
stack.Pop();
}
}
}
public static IDisposable CreateCorrelation(Guid? correlationId = null)
{
lock(_correlationStack)
{
if (_correlationStack.Value == null)
_correlationStack.Value = new Stack<Guid>();
_correlationStack.Value.Push(correlationId ?? Guid.NewGuid());
}
return new DisposeCorrelation(_correlationStack);
}
public static IDisposable CreateCorrelationIfNotExists(Guid? correlationId = null)
{
if(GetCorrelationStack().Count > 0)
return new EmptyDisposable();
return CreateCorrelation(correlationId);
}
private class DisposeCorrelation : IDisposable
{
private readonly AsyncLocal<Stack<Guid>> _localStack;
public DisposeCorrelation(AsyncLocal<Stack<Guid>> localStack)
{
_localStack = localStack;
}
public void Dispose()
{
var stack = _localStack.Value ?? new Stack<Guid>();
if(stack.Count > 0)
stack.Pop();
}
}
public static Guid? GetCorrelationId()
{
var stack = GetCorrelationStack();
if(stack.Count > 0)
return stack.Peek();
return null; // TODO :: decide what you are going to do
}
private static readonly AsyncLocal<Stack<Guid>> _correlationStack = new AsyncLocal<Stack<Guid>>();
public static Stack<Guid> GetCorrelationStack() => _correlationStack.Value ?? new Stack<Guid>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment