memory

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memory provides a synchronized, in-process reference implementation of the coordination contracts.

Backend implements coordination.Backend, coordination.Registry and coordination.Barrier. It is deterministic under an injected coordination.Clock (or inside a testing/synctest bubble with the real clock), which makes it suitable for tests and for coordinating goroutines of one process. It does not coordinate separate processes and keeps no state across restarts.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

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

Backend is the in-memory reference backend. Its zero value is not usable; create one with NewBackend. A Backend is safe for concurrent use.

Fencing tokens start at 1 per key and survive release and expiry for the lifetime of the Backend.

func NewBackend

func NewBackend(clock coordination.Clock) *Backend

NewBackend returns an empty Backend that reads time from clock. A nil clock selects the real clock.

Example
package main

import (
	"context"
	"fmt"
	"time"

	coordination "github.com/justusschock/distributed-coordination"
	"github.com/justusschock/distributed-coordination/memory"
)

func main() {
	backend := memory.NewBackend(nil) // nil selects the real clock

	lease, err := backend.Acquire(context.Background(), coordination.AcquireRequest{
		Key: "jobs", Owner: "worker-a", TTL: time.Minute,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(lease.Info().Key, lease.Info().Owner, lease.Info().Token)
	_ = lease.Release(context.Background())
}
Output:
jobs worker-a 1

func (*Backend) Acquire

Acquire implements coordination.Backend. A waiting acquisition wakes on release or at the holder's deadline and honours ctx.

func (*Backend) Join

Join implements coordination.Registry. Member metadata is copied on the way in and on every read.

Example
package main

import (
	"context"
	"fmt"
	"time"

	coordination "github.com/justusschock/distributed-coordination"
	"github.com/justusschock/distributed-coordination/memory"
)

func main() {
	ctx := context.Background()
	backend := memory.NewBackend(nil)

	for _, id := range []string{"node-b", "node-a"} {
		membership, err := backend.Join(ctx, coordination.Member{
			Group:    "workers",
			ID:       id,
			Metadata: map[string]string{"zone": "eu-central"},
		}, 30*time.Second)
		if err != nil {
			fmt.Println(err)
			return
		}
		defer func() { _ = membership.Leave(context.Background()) }()
	}

	members, err := backend.List(ctx, "workers")
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, member := range members {
		fmt.Println(member.Member.ID, member.Member.Metadata["zone"])
	}
}
Output:
node-a eu-central
node-b eu-central

func (*Backend) List

func (b *Backend) List(ctx context.Context, group string) ([]coordination.MemberInfo, error)

List implements coordination.Registry. It returns copies; mutating them does not affect the Backend.

func (*Backend) Wait

Wait implements coordination.Barrier. The first Wait for a name fixes its party count for the lifetime of the Backend. Expired arrivals are pruned by deadline before a generation completes, even when their timer has not fired yet.

Example
package main

import (
	"context"
	"fmt"
	"sync"
	"time"

	coordination "github.com/justusschock/distributed-coordination"
	"github.com/justusschock/distributed-coordination/memory"
)

func main() {
	backend := memory.NewBackend(nil)

	var wg sync.WaitGroup
	results := make([]coordination.BarrierResult, 2)
	for i, participant := range []string{"node-b", "node-a"} {
		wg.Go(func() {
			result, err := backend.Wait(context.Background(), coordination.BarrierRequest{
				Name:        "deployment-ready",
				Participant: participant,
				Parties:     2,
				TTL:         time.Minute,
			})
			if err != nil {
				fmt.Println(err)
			}
			results[i] = result
		})
	}
	wg.Wait()
	fmt.Printf("generation %d: %v\n", results[0].Generation, results[0].Participants)
}
Output:
generation 1: [node-a node-b]

Jump to

Keyboard shortcuts

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