window

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package window provides window assigners for grouping unbounded streams into finite chunks based on time.

A WindowAssigner takes a Record's timestamp and determines which window(s) it belongs to. When a watermark passes a window's end time, that window closes and its accumulated records are emitted downstream.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WindowStart

func WindowStart(ts time.Time, size, offset time.Duration) time.Time

WindowStart calculates the start of the window that contains the given timestamp, accounting for offset alignment.

Types

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session assigns records to variable-size windows that close after a gap of inactivity (the gap duration). Records that arrive within the gap of the previous record belong to the same session.

Session windows are dynamic — they expand when new records arrive within the gap, and merge when two sessions grow close enough to overlap.

Example with gap=30s:

Record at 00:00 → session window [00:00, 00:30)
Record at 00:15 → session window expands to [00:00, 00:45)
Record at 01:30 → new session window [01:30, 02:00)

func NewSession

func NewSession(gap time.Duration) *Session

NewSession creates a session window assigner with the given inactivity gap. A session window stays open as long as records arrive within the gap. When no record arrives for gap duration, the session closes.

func (*Session) AssignWindows

func (s *Session) AssignWindows(timestamp time.Time) []Window

AssignWindows returns a single session window starting at the record's timestamp and ending at timestamp + gap. The WindowOperator merges overlapping sessions as new records arrive.

func (*Session) IsSession

func (s *Session) IsSession() bool

IsSession returns true — session windows merge when overlapping.

func (*Session) Name added in v0.3.0

func (s *Session) Name() string

Name returns the window type for the dashboard.

func (*Session) WindowSize

func (s *Session) WindowSize() time.Duration

WindowSize returns the session gap duration. Note: actual window size varies per session; this returns the minimum (the gap).

type Sliding

type Sliding struct {
	// contains filtered or unexported fields
}

Sliding assigns records to overlapping fixed-size windows that slide by a fixed interval. A single record can belong to multiple windows.

Example with size=5min, slide=1min, aligned to :00:

Window [00:00, 00:05) contains records from 00:00 to 00:04
Window [00:01, 00:06) contains records from 00:01 to 00:05
Window [00:02, 00:07) contains records from 00:02 to 00:06
...

A record at 00:03 belongs to windows: [00:00-00:05], [00:01-00:06], [00:02-00:07], [00:03-00:08], [00:04-00:09].

func NewSliding

func NewSliding(size, slide time.Duration) *Sliding

NewSliding creates a sliding window assigner with the given window size and slide interval. Slide must be <= size.

func (*Sliding) AssignWindows

func (s *Sliding) AssignWindows(timestamp time.Time) []Window

AssignWindows returns all windows that the given timestamp falls into. For sliding windows, a record typically belongs to ceil(size/slide) windows.

func (*Sliding) IsSession

func (s *Sliding) IsSession() bool

IsSession returns false — sliding windows have fixed boundaries.

func (*Sliding) Name added in v0.3.0

func (s *Sliding) Name() string

Name returns the window type for the dashboard.

func (*Sliding) WindowSize

func (s *Sliding) WindowSize() time.Duration

WindowSize returns the window duration.

func (*Sliding) WithOffset

func (s *Sliding) WithOffset(offset time.Duration) *Sliding

WithOffset shifts the window alignment.

type Tumbling

type Tumbling struct {
	// contains filtered or unexported fields
}

Tumbling assigns records to fixed-size, non-overlapping windows. Each record belongs to exactly one window.

Example with size=5min, aligned to :00:

[00:00, 00:05), [00:05, 00:10), [00:10, 00:15), ...

A record with timestamp 00:07 goes into window [00:05, 00:10).

func NewTumbling

func NewTumbling(size time.Duration) *Tumbling

NewTumbling creates a tumbling window assigner with the given window size. Windows are aligned to Unix epoch (offset=0) by default.

func (*Tumbling) AssignWindows

func (t *Tumbling) AssignWindows(timestamp time.Time) []Window

AssignWindows returns exactly one window for the given timestamp.

func (*Tumbling) IsSession

func (t *Tumbling) IsSession() bool

IsSession returns false — tumbling windows have fixed boundaries.

func (*Tumbling) Name added in v0.3.0

func (t *Tumbling) Name() string

Name returns the window type for the dashboard.

func (*Tumbling) WindowSize

func (t *Tumbling) WindowSize() time.Duration

WindowSize returns the duration of each tumbling window.

func (*Tumbling) WithOffset

func (t *Tumbling) WithOffset(offset time.Duration) *Tumbling

WithOffset shifts the window alignment. For example, WithOffset(1*time.Minute) on a 5-minute tumbling window starts windows at :01, :06, :11, etc.

type Window

type Window struct {
	Start time.Time
	End   time.Time
}

Window represents a time range [Start, End) that records are grouped into.

func MergeSessions

func MergeSessions(a, b Window) []Window

MergeSessions merges overlapping or adjacent session windows. Two sessions overlap if sessionA.End > sessionB.Start (or vice versa). Returns the merged window if they overlap, or both windows if they don't.

type WindowAssigner

type WindowAssigner interface {
	// Name returns the window type name (e.g. "Tumbling", "Sliding", "Session").
	Name() string

	// AssignWindows returns one or more windows that the given timestamp falls into.
	// A timestamp can belong to multiple windows in the case of sliding windows.
	AssignWindows(timestamp time.Time) []Window

	// WindowSize returns the duration of a single window.
	WindowSize() time.Duration

	// IsSession returns true if this assigner creates session windows.
	// Session windows need merging when records arrive within the gap;
	// other window types have fixed boundaries and don't merge.
	IsSession() bool
}

WindowAssigner determines which window(s) a record belongs to based on its timestamp. Different window types implement this interface.

Jump to

Keyboard shortcuts

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