concurrent

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: BSD-3-Clause Imports: 8 Imported by: 4

Documentation

Overview

Package concurrent provides high-level synchronization tooling and thread-safe data structures.

Index

Constants

View Source
const DefaultConcurrency = 16

DefaultConcurrency is the default level of concurrency applied in the scoreboard constructor.

View Source
const DefaultCounterCheckInterval = 10 * time.Millisecond

DefaultCounterCheckInterval is the default check interval used by Await/AwaitCtx/Drain/Fill.

View Source
const DefaultReferenceCheckInterval = 10 * time.Millisecond

DefaultReferenceCheckInterval is the default check interval used by Await/AwaitCtx.

View Source
const DefaultScoreboardCheckInterval = 10 * time.Millisecond

DefaultScoreboardCheckInterval is the default check interval used by Await/AwaitCtx/Drain/Fill.

View Source
const Indefinitely = math.MaxInt64 * time.Nanosecond

Indefinitely is a constant that represents the longest allowable duration (approx. 290 years). It is used when an arbitrarily long timeout is needed.

Variables

This section is empty.

Functions

func Forever

func Forever(parent context.Context) (context.Context, context.CancelFunc)

Forever returns a context that never times out.

func Nop added in v0.3.0

func Nop()

Nop is a no-op function, useful for coverage-testing empty branches (e.g. in switch statements) that don't otherwise show up as uncovered.

func Timeout

func Timeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

Timeout returns a context that will expire within the given timeout.

Types

type AtomicCounter

type AtomicCounter interface {
	fmt.Stringer
	Get() int64
	GetInt() int
	Add(amount int64) int64
	Inc() int64
	Dec() int64
	Set(amount int64)
	CompareAndSwap(expected int64, replacement int64) bool
	Fill(atLeast int64, timeout time.Duration, interval ...time.Duration) int64
	Drain(atMost int64, timeout time.Duration, interval ...time.Duration) int64
	Await(cond I64Condition, timeout time.Duration, interval ...time.Duration) int64
	AwaitCtx(ctx context.Context, cond I64Condition, interval ...time.Duration) int64
}

AtomicCounter encapsulates an int64 value that may updated atomically.

func NewAtomicCounter

func NewAtomicCounter(initial ...int64) AtomicCounter

NewAtomicCounter creates a new counter, optionally assigning its value to the given initial value (0 by default)

type AtomicReference

type AtomicReference interface {
	fmt.Stringer
	Set(value interface{})
	Get() interface{}
	Await(cond RefCondition, timeout time.Duration, interval ...time.Duration) interface{}
	AwaitCtx(ctx context.Context, cond RefCondition, interval ...time.Duration) interface{}
}

AtomicReference encapsulates a pointer that may updated atomically. Unlike its sync/atomic.Value counterpart, this implementation permits nil pointers.

func NewAtomicReference

func NewAtomicReference(initial ...interface{}) AtomicReference

NewAtomicReference creates a new reference, optionally assigning its contents to the given initial referent (nil by default)

type Deadline

type Deadline interface {
	TryRun(f func()) bool
	Elapsed() time.Duration
	Expired() bool
	Move(new time.Time)
	Last() time.Time
	Remaining() time.Duration
}

Deadline tracks the time a task was last run and conditionally runs a task if the deadline has lapsed.

Deadline is thread-safe.

func NewDeadline

func NewDeadline(interval time.Duration) Deadline

NewDeadline creates a new Deadline with the specified interval.

type I64Condition

type I64Condition func(value int64) bool

I64Condition is a predicate that checks whether the current (supplied) value meets some condition, returning true if the condition is met.

func I64Equal

func I64Equal(target int64) I64Condition

I64Equal tests that the value equals a target value.

func I64GreaterThan

func I64GreaterThan(target int64) I64Condition

I64GreaterThan tests that the value is greater than the given target value.

func I64GreaterThanOrEqual

func I64GreaterThanOrEqual(target int64) I64Condition

I64GreaterThanOrEqual tests that the value is greater than or equal to the given target value.

func I64LessThan

func I64LessThan(target int64) I64Condition

I64LessThan tests that the value is less than the given target value.

func I64LessThanOrEqual

func I64LessThanOrEqual(target int64) I64Condition

I64LessThanOrEqual tests that the value is less than or equal to the given target value.

func I64Not

func I64Not(cond I64Condition) I64Condition

I64Not produces a logical inverse of the given condition.

type RefCondition

type RefCondition func(referent interface{}) bool

RefCondition is a predicate that checks whether the current (supplied) referent meets some condition, returning true if the condition is met.

func RefEqual

func RefEqual(target interface{}) RefCondition

RefEqual tests that the encapsulated referent equals a target referent.

func RefNil

func RefNil() RefCondition

RefNil checks that the current referent is nil.

func RefNot

func RefNot(cond RefCondition) RefCondition

RefNot produces a logical inverse of the given condition.

type Scoreboard

type Scoreboard interface {
	fmt.Stringer
	Add(key string, amount int64) int64
	Inc(key string) int64
	Dec(key string) int64
	Get(key string) int64
	GetInt(key string) int
	Set(key string, value int64)
	Clear()
	View() map[string]int64
	Fill(key string, atLeast int64, timeout time.Duration, interval ...time.Duration) int64
	Drain(key string, atMost int64, timeout time.Duration, interval ...time.Duration) int64
	Await(key string, cond I64Condition, timeout time.Duration, interval ...time.Duration) int64
	AwaitCtx(ctx context.Context, key string, cond I64Condition, interval ...time.Duration) int64
}

Scoreboard is a compactly represented map of atomic counters, where a counter takes up a map slot only if it is not equal to zero.

func NewScoreboard

func NewScoreboard(concurrency ...int) Scoreboard

NewScoreboard creates a new scoreboard instance with an optionally specified concurrency level, controlling the number of internal shards. If unspecified, concurrency is set to DefaultConcurrency.

Each shard is individually locked. A greater number of shards allows for a greater degree of uncontended access, provided the keys are well-distributed. Shards are created up-front, meaning that scoreboards with more concurrency take up more space.

Jump to

Keyboard shortcuts

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