sync

package
v0.0.0-...-2efaa0c Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2018 License: Apache-2.0 Imports: 3 Imported by: 53

README

Sync GoDoc

Sync is client library for locking and leadership election. It provides a way to coordinate across a number of nodes. It's a building block for synchronization in distributed systems.

Most distributed systems choose AP - availability and partition tolerance. At some point we need consistency as well so Sync provides a way to do this.

type Sync interface {
        // distributed lock interface
        Lock(...LockOption) (Lock, error)
        // leader election interface
        Leader(...LeaderOption) (Leader, error)
}

type Lock interface {
        Id() string
        Acquire() error
        Release() error
}

type Leader interface {
        // Returns the current leader
        Leader() (*registry.Node, error)
        // Elect self to become leader
        Elect() (Elected, error)
        // Returns the status of this node
        Status() (LeaderStatus, error)
}

type Elected interface {
        // Returns a channel which indicates
        // when the leadership is revoked
        Revoked() (chan bool, error)
        // Resign the leadership
        Resign() error
}

Supported Backends

Usage

package main

import (
        "fmt"

        "github.com/micro/go-os/sync/consul"
)

func main() {
        c := consul.NewSync()

        // create a lock
        lock, err := c.Lock("lockid")
        if err != nil {
                fmt.Println("Failed to create lock", err)
                return
        }

        fmt.Println("Created lock")

        // acquire the lock
        if err := lock.Acquire(); err != nil {
                fmt.Println("Failed to acquire lock", err)
                return
        }

        fmt.Println("Acquired lock")

        // release the lock
        if err := lock.Release(); err != nil {
                fmt.Println("Failed to release lock", err)
                return
        }

        fmt.Println("Released lock")
}

Documentation

Overview

Package sync is an interface for synchronization.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultNamespace = "/micro/sync"
)

Functions

func NewContext

func NewContext(ctx context.Context, c Sync) context.Context

Types

type Elected

type Elected interface {
	// Returns a channel which indicates
	// when the leadership is revoked
	Revoked() (chan struct{}, error)
	// Resign the leadership
	Resign() error
}

type Leader

type Leader interface {
	// The unique ID to synchronize with
	Id() string
	// Returns the current leader
	Leader() (*registry.Node, error)
	// Elect self to become leader
	Elect() (Elected, error)
	// Returns the status of this node
	Status() (LeaderStatus, error)
}

type LeaderOption

type LeaderOption func(o *LeaderOptions)

type LeaderOptions

type LeaderOptions struct{}

type LeaderStatus

type LeaderStatus int32
const (
	FollowerStatus  LeaderStatus = 0
	CandidateStatus LeaderStatus = 1
	ElectedStatus   LeaderStatus = 2
)

type Lock

type Lock interface {
	// The unique ID to lock on
	Id() string
	// Acquire the lock
	Acquire() error
	// Release the lock
	Release() error
}

type LockOption

type LockOption func(o *LockOptions)

func LockTTL

func LockTTL(t time.Duration) LockOption

func LockWait

func LockWait(t time.Duration) LockOption

type LockOptions

type LockOptions struct {
	TTL  time.Duration
	Wait time.Duration
}

type Option

type Option func(o *Options)

func Namespace

func Namespace(n string) Option

func Nodes

func Nodes(nodes ...string) Option

func Service

func Service(s *registry.Service) Option

type Options

type Options struct {
	Namespace string
	Service   *registry.Service
	Nodes     []string
}

type Sync

type Sync interface {
	// distributed lock interface
	Lock(id string, opts ...LockOption) (Lock, error)
	// leader election interface
	Leader(id string, opts ...LeaderOption) (Leader, error)
	// Name of sync
	String() string
}

func FromContext

func FromContext(ctx context.Context) (Sync, bool)

Jump to

Keyboard shortcuts

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