Skip to content

Leadership election and Cancellation token in dotnet #10

@lawrencegripper

Description

@lawrencegripper

I was wondering what your thoughts would be on passing a cancellation token to each of the funcs in the .net implementation. This can then be checked, while the code is running, to understand if leadership has been lost.

I took a look at implementing leadership election in dotnetcore a while back using etcd and used this pattern. Full code here, simple example below.

            var election = new ElectionRunner(
                isNowMaster: (cancellationToken) =>{
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("We're Master!!");
                        Task.Delay(TimeSpan.FromSeconds(3)).Wait();
                    }
                },
                isNowSecondary: (cancellationToken) =>{
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("We're secondary");
                        Task.Delay(TimeSpan.FromSeconds(3)).Wait();                        
                    }
                },
                electionTimeoutSec: 15);

I'm unclear on how feasible this is in the Metaparticle code but if there is agreement that it is an worthwhile addition I can investigate further and hopefully create a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions