flag

package
v0.0.0-...-32c7374 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flag

type Flag interface {
	// IsSet returns true if the flag is set.
	// The method uses an atomic boolean internally and is non-blocking.
	IsSet() bool

	// Wait waits for the flag to be set.
	Wait() <-chan struct{}
}

Flag is a read-only boolean flag that can be waited on until set.

Example:

serving := async.UnsetFlag()

func handle(ctx Context, req *request) {
	if !serving.Get() { // just to show Get in example
		select {
		case <-ctx.Wait():
			return ctx.Status()
		case <-serving.Wait():
		}
	}

	// ... handle request
}

func ReverseFlag

func ReverseFlag(f Flag) Flag

ReverseFlag returns a new flag which reverses the original one.

type MutFlag

type MutFlag interface {
	Flag

	// Set sets the flag and notifies the waiters.
	Set()

	// Unset unsets the flag and replaces its wait channel with an open one.
	Unset()
}

MutFlag is a routine-safe boolean flag that can be set, reset, and waited on until set.

Example:

serving := async.UnsetFlag()

func serve() {
	s.serving.Set()
	defer s.serving.Unset()

	// ... start server ...
}

func handle(ctx Context, req *request) {
	select {
	case <-ctx.Wait():
		return ctx.Status()
	case <-serving.Wait():
	}

	// ... handle request
}

func SetFlag

func SetFlag() MutFlag

SetFlag returns a new set flag.

func UnsetFlag

func UnsetFlag() MutFlag

UnsetFlag returns a new unset flag.

Jump to

Keyboard shortcuts

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