Comments (2)
Notes:
-
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. -
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.
-
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.
}
- 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.
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)
- Compliance: Unit test SDK depends on Newtonsoft.Json 9.0.1 HOT 2
- AKS no telemetry HOT 3
- Upgrade KubernetesClient to >= 10.0.16 to remove dependency on System.IO.Abstractions HOT 16
- backlog
- Add wiki for supporting Azure Function in K8s HOT 1
- Use AKS instead of local kubernetes - How HOT 4
- No insight output as indicated in Zero Code light up Example HOT 4
- Can I use DefaultAzureCredential with Zero User Code Lightup? HOT 5
- Enhancement: Capture container image name HOT 6
- No container id found by container id providers after AKS upgrade 1.23->1.25 HOT 15
- Investigation: Using a label to locate the current container HOT 1
- Decouple Enhancement and container readiness HOT 2
- Cannot get pod info because of proxy configuration HOT 9
- As of now the package doesn HOT 1
- As of now the package doesn't seem to add the properties anymore? HOT 3
- Can't register Enrichers when using KeyedServices HOT 1
- Upgrade of `KubernetesClient` dependency HOT 18
- how to customize k8s properties name HOT 3
- Warning in pod logs re failed to get container id HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from applicationinsights-kubernetes.