Replies: 1 comment 1 reply
-
Hey @estahn, thanks for reaching out! Another options is to implement this feature with a standalone function that wraps a task and guarantees it's only executed once, something like this (oversimplified): func Dedupe(idempotencyKey string, task func()) func() {
return func() {
// If task is already running, do not execute
if (isRunning(idempotencyKey)) {
return
}
// Mark task as running
markRunning(idempotencyKey)
// Mark task as completed
defer markCompleted(idempotencyKey)
// Execute task
task()
}
} Which could then be used as follows: pool.Submit(Dedupe("Task 1", fun(){ ... }));
pool.Submit(Dedupe("Task 2", fun(){ ... }));
pool.Submit(Dedupe("Task 1", fun(){ ... })); The benefit of this approach is it works with just about any worker pool implementation, so it's not tied to a particular concurrent execution pattern. In fact, it could very well be a library on its own 🙂 Please let me know your thoughts. Have a nice day! |
Beta Was this translation helpful? Give feedback.
-
I would like some sort of dedupe strategy, e.g.
Task 1
Task 2
Task 1
While
Task 1
is in the pool no other tasks of the same kind should be added to the queue.Beta Was this translation helpful? Give feedback.
All reactions