doclock

package module
v0.0.0-...-e207442 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2021 License: MIT Imports: 8 Imported by: 0

README

DocLock - A locking Go library for cloud

A simple binary lock (mutex) library built on top of Docstore (Firestore, DynamoDB or MongoDB): https://gocloud.dev/howto/docstore/

Locks can be used for leader election or more custom use cases. Locks have a TTL and each resource has its own document in the collection. Locks acquire a specific Resource ID.

Usage

//...
import "github.com/bgadrian/doclock"
import "gocloud.dev/docstore"
//...

func(){
	resourceID := "my_server_leader"
	//the collection needs to have the Key ID set `id`, you can use the constant KEY
	var myDataStoreCollection *docstore.Collection
	
	//blocks until acquired
	leaderLock := doclock.New(myDataStoreCollection, resourceID).Build()

    leaderCh, _  := leaderLock.Lock(context.Background())
	
	//do your leader stuff while leaderCh is open
	
	//when shutting down call to release the lock
	leaderLock.Unlock()
}

Contribute

Requirements

Go 1.16+
go install github.com/golang/mock/mockgen

Documentation

Index

Constants

View Source
const KEY = "id"

KEY is the collection name of the Key property.

Variables

View Source
var (
	ErrLockHeld = errors.New("lock already held")
	ErrNotHeld  = errors.New("lock not held")
)

Functions

func WrapLockWithLeader

func WrapLockWithLeader(ctx context.Context, l *Lock, actor Leader)

WrapLockWithLeader is a helper that calls the Leader methods based on Locks status blocks as long as ctx is active and lock is acquired

Types

type Collection

type Collection interface {
	Put(ctx context.Context, doc docstore.Document) error
	Delete(ctx context.Context, doc docstore.Document) error
	Get(ctx context.Context, doc docstore.Document, fps ...docstore.FieldPath) error
}

Collection is a subset of methods of *docstore.Collection TODO make this more abstract, and implement a wrapper to use any other storage

type Doc

type Doc struct {
	ID              string    `docstore:"id"` //the resourceID
	ExpirationTime  time.Time `docstore:"ex_ts"`
	ExpirationActor uint64    `docstore:"ex_actor"`

	//for optimistic concurrency
	DocstoreRevision interface{}
}

Doc is the internal document. Exported only for marshalling.

type Leader

type Leader interface {
	IsLeader()
	IsFollower()
}

type Lock

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

func (*Lock) Lock

func (l *Lock) Lock(ctx context.Context) (<-chan struct{}, error)

Lock will begin to poll to acquire a lock. Blocks until acquired. Cancel the ctx to stop it. Returns a channel which will be closed when the lock is lost OR failure occurs.

func (*Lock) Unlock

func (l *Lock) Unlock() error

Unlock removes the resource lock. The operation is sync/blocking. The channel returned by Lock() will be closed soon after, async.

type LockBuilder

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

func New

func New(collection Collection, resourceID string) *LockBuilder

func (*LockBuilder) Build

func (b *LockBuilder) Build() *Lock

Build builds a lock instance

func (*LockBuilder) WithActorID

func (b *LockBuilder) WithActorID(d uint64) *LockBuilder

WithActorID sets a custom ActorID. It is not recommended!

func (*LockBuilder) WithLeaseTTL

func (b *LockBuilder) WithLeaseTTL(d time.Duration) *LockBuilder

WithLeaseTTL sets a custom TTL for the lock

func (*LockBuilder) WithLeaseTTLUpdateInterval

func (b *LockBuilder) WithLeaseTTLUpdateInterval(d time.Duration) *LockBuilder

WithLeaseTTLUpdateInterval sets a custom update rate of the extension of lease/TTL A document.Update with now+LeaseTTL will be called at each tick, while the lock is acquired.

func (*LockBuilder) WithLockPollDuration

func (b *LockBuilder) WithLockPollDuration(d time.Duration) *LockBuilder

WithLockPollDuration sets a custom maximum duration between 2 attempts to acquire the lock

Directories

Path Synopsis
Package tests is a generated GoMock package.
Package tests is a generated GoMock package.

Jump to

Keyboard shortcuts

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