coalesce

package
v0.1.0-alpha.12 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package coalesce provides utilities for event coalescing in controller components.

Event coalescing implements the "latest wins" pattern where intermediate events are skipped when newer events of the same type are available. This prevents queue backlog when events arrive faster than they can be processed.

Only events that implement CoalescibleEvent and return Coalescible() == true are coalesced. Other events are passed to the handleOther callback.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrainLatest

func DrainLatest[T busevents.Event](
	eventChan <-chan busevents.Event,
	handleOther func(busevents.Event),
) (latest T, supersededCount int)

DrainLatest drains the event channel and returns the latest coalescible event of type T. Non-coalescible events and events of other types are passed to handleOther. Returns the zero value of T and 0 if no coalescible events were found.

An event is coalescible if:

  1. It matches type T
  2. It implements CoalescibleEvent interface
  3. Its Coalescible() method returns true

Usage pattern:

func (c *Component) handleSomeEvent(event *events.SomeEvent) {
    c.performWork(event)

    // After work completes, drain for latest coalescible event
    for {
        latest, superseded := coalesce.DrainLatest[*events.SomeEvent](
            c.eventChan,
            c.handleEvent, // Handle non-coalescible and other event types
        )
        if latest == nil {
            return
        }
        c.logger.Debug("Processing coalesced event",
            "superseded_count", superseded)
        c.performWork(latest)
    }
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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