memsync

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2021 License: Apache-2.0, MIT Imports: 6 Imported by: 1

README

Memsync

Memsync provides a Memcached-based distributed mutual exclusion lock implementation for Go.

Installation

Install Memsync using:

$ go get github.com/moeryomenko/memsync

Usage

package main

import (
	"time"

	"github.com/bradfitz/gomemcache/memcache"
	"github.com/moeryomenko/memsync"
)

func main() {
	// Create a memcached client.
	client := memcache.New("localhost:112211")

	// Create an instance of memsync to be used to obtain a mutual exclusion lock.
	ms := memsync.New(client)

	// Obtaion a new mutex by using the same for all instances wanting the same lock.
	mx := rw.NewMutex(
		"examplelock",           // exlusive key for lock.
		WithExpiry(time.Second), // sets custom expiry of mutex.
		WithTries(8),            // sets numbers of times lock acquire is attempted.
	)

	// Obtain a lock for given mutex. After this is successful, no one else
	// can obtain the same lock (the same mutex key) until we unlock it.
	for err := mutex.Lock(); err != nil; {}

	// Do work that requires the lock.

	// Release the lock so other instances can obtain a lock.
	if err := mutex.Unlock(); err != nil {
		panic("unlock failed")
	}
}

License

Memsync is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and/or LICENSE-MIT for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFailed = errors.New("memsync: failed to acquire lock")

Functions

This section is empty.

Types

type DelayFunc

type DelayFunc func(tries int) time.Duration

DelayFunc is used to decide the amount of time to wait between retries.

type Memsync

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

Memsync provides a simple method for creating distributed mutexes using memcache.

func New

func New(cache *memcache.Client) *Memsync

New creates and returns a new Memsync instance.

func (*Memsync) NewMutex

func (m *Memsync) NewMutex(key string, options ...Option) *Mutex

NewMutex returns a new distributed mutex with given key.

type Mutex

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

A Mutex is a distributed mutual exclusion lock.

func (*Mutex) Key

func (m *Mutex) Key() string

Key returns mutex key.

func (*Mutex) Lock

func (m *Mutex) Lock() error

Lock tries lock mutex, in case it returns an error on failure.

func (*Mutex) Token

func (m *Mutex) Token() string

Token returns the current random token. The token will be empty until a lock is acquired.

func (*Mutex) Unlock

func (m *Mutex) Unlock() error

func (*Mutex) Until

func (m *Mutex) Until() time.Time

Until returns the time of validity of acquired lock. The token will be zero token until a lock is acquired.

type Option

type Option func(*Mutex)

Option confibures a mutex.

func WithDriftFactor

func WithDriftFactor(factor float64) Option

WithDriftFactor can be used to set the clock drift factor.

func WithExpiry

func WithExpiry(expiry time.Duration) Option

WithExpiry can be used to set the expiry of mutex to the given token.

func WithGenTokenFunc

func WithGenTokenFunc(genTokenFunc func() (string, error)) Option

WithGenTokenFunc can be used to set custom value generator.

func WithRetryDelay

func WithRetryDelay(delay time.Duration) Option

WithRetryDelay can be used to set the amount of time to wait between retries.

func WithRetryDelayFunc

func WithRetryDelayFunc(delayFn DelayFunc) Option

WithRetryDelayFunc can be used to override default delay behavior.

func WithTries

func WithTries(tries int) Option

WithTries can be used to set the number of times lock acquire is attempted.

Jump to

Keyboard shortcuts

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