coalescing

package module
v0.0.0-...-92edfa0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2025 License: MIT Imports: 2 Imported by: 0

README

Coalescing Queue

A coalescing queue allows multiple operations to be combined into a single operation. This is useful when a heavy operation might be executed multiple times, but only needs to be performed once if it hasn't started yet. For instance, if an operation (OP) is currently being executed and a series of additional requests to execute OP are received (e.g., {OP, OP, OP}), these requests can be consolidated into a single operation ({op}).

Usage

Example
package main

import (
	"context"
	"fmt"
	"github.com/gavraz/async/coalescing"
	"time"
)

func printHello(_ context.Context) {
	fmt.Println("Hello")
	time.Sleep(2 * time.Second)
}

func main() {
	ctx := context.Background()
	execute := coalescing.Queue(ctx, printHello)

	execute(ctx)         // first is executed 
	execute(ctx)         // second will wait for first
	done := execute(ctx) // third will be dropped
	<-done               // will return after the second execution completes

	// output:
	// Hello
	// Hello
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Queue

func Queue(ctx context.Context, fn func(ctx context.Context)) (execute func(ctx context.Context) (done <-chan struct{}))

Queue is used for coalescing repeated executions of the same function (fn). it returns a non-blocking execute function to be called for every execution request. the execution function returns an optional done channel to await completion.

Guarantees: - If the first call to execute is yet to complete, the second will be executed right after the first completes. - If two calls are to be executed but yet to complete, any subsequent execution will be discarded. - Discarded executions complete upon completion of the second queue.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL