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 ¶
- type Backend
- func (b *Backend) Acquire(ctx context.Context, request coordination.AcquireRequest) (coordination.Lease, error)
- func (b *Backend) Join(ctx context.Context, member coordination.Member, ttl time.Duration) (coordination.Membership, error)
- func (b *Backend) List(ctx context.Context, group string) ([]coordination.MemberInfo, error)
- func (b *Backend) Wait(ctx context.Context, request coordination.BarrierRequest) (coordination.BarrierResult, error)
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 ¶
func (b *Backend) Acquire(ctx context.Context, request coordination.AcquireRequest) (coordination.Lease, error)
Acquire implements coordination.Backend. A waiting acquisition wakes on release or at the holder's deadline and honours ctx.
func (*Backend) Join ¶
func (b *Backend) Join(ctx context.Context, member coordination.Member, ttl time.Duration) (coordination.Membership, error)
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 ¶
func (b *Backend) Wait(ctx context.Context, request coordination.BarrierRequest) (coordination.BarrierResult, error)
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]