Giter Site home page Giter Site logo

Comments (2)

xiaomi7732 avatar xiaomi7732 commented on July 4, 2024

Notes:

  1. Lazy for ExecuteAsync() - that the task never creates a second time: The issue is cancellation token handling. Lazy doesn't take in a cancellation token; To implement it, it may be a Lazy<Func<CancellationToken, Task>>, and then, it starts to look like an AsyncLazy library - that feels a bit heavy for this case; It is also an intention not to own tricky implementations in this library.

  2. Use an AsyncLazy library - don't need to worry about owning the complex logic. I went over some open-source libraries, and most of them are more than just AsyncLazy, and not all libraries are always dealing with all the issues. It is doable, it is probably not worth introducing the dependency for this scenario.

  3. Going back to the origin of the issue - do not run the code 2nd time, and a stupidly simple solution would be: Have a ref counter, start at 0, and drop the execution when it is increased to 1. Here's a pseudo:

int _refCount = 0;

async Task ExecuteAsync(CancenllationToken cancellationToken)
{
    if ( refCount >= 1 ) { return; } // Already executed;
    Interlocked.Increment(ref refCount);
    if ( refCount != 1 ) { return; } // Only thread got refCount == 1 survives.
    // Start to run the logic. No cancellation token passing around.
}
  1. Introduce a SemaphoreSlim(1,1), one thread grabs the only available resource and never releases it, anything coming in afterward got rejected. - seems a bit to use SemaphoreSlim by force.

I am currently leaning toward 3. I think it is simple enough and easy to understand the semantics, and it doesn't have additional dependencies and is lightweight.

from applicationinsights-kubernetes.

xiaomi7732 avatar xiaomi7732 commented on July 4, 2024

Another approach suggested by @karol-ms: just take a lock, considering this isn't any hot path

static readonly _lock = new object();
bool _executed;

async Task ExecuteAsync(CancenllationToken cancellationToken)
{
    lock(_lock)
    {
        if (_executed) { return; } // Already executed.
        _executed = true;
    }
    // The rest of the code.
}

A consideration: use a Task to replace the boolean.

from applicationinsights-kubernetes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.