lock

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2019 License: Apache-2.0 Imports: 5 Imported by: 1

README

memcached lock

Build Status Coverage Status Go Report Card

A simple lock/release library written in golang to be used with a single memcached instance.

This package is compatible with bradfitz/gomemcache but really any client will do, just implement the ./adapters.Adapter interface.

is it reliable?

memcached is not a native lock server, you should use this with criteria and certainly avoid when failures in mutual exclusion will result in permanent data corruption.

That said, I've sucessfully tested this against race conditions with 1k goroutines attempting to lock the same resource. The same tests are stored in ./locker_test.go feel free to run them yourself.

usage

package main

import (
	locker "github.com/alessandro-c/gomemcached-lock"
	adapter "github.com/alessandro-c/gomemcached-lock/adapters/gomemcache"
	"github.com/bradfitz/gomemcache/memcache"
	"time"
)

func main() {

	client := memcache.New("memcachedhost:11211")

	adapter := adapter.New(client)

	lock := locker.New(adapter, "resource:to:lock", "")

	err := lock.Lock(time.Minute * 5)

	if err == nil {
		// lock acquired, do something ...
		lock.Release()
	} else if err == locker.ErrNotAcquired {
		// lost race ...
	} else {
		// something went wrong ...
	}
}

Documentation

Overview

Package lock provides a simple lock/release library to be used on a single memcached instance.

This package is compatible with https://github.com/bradfitz/gomemcache but really any client will do, just implement the ./adapters.Adapter interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAcquired is returned when a locker tries to lock an already locked resource
	ErrNotAcquired = errors.New("locker: not acquired")

	// ErrNotFound is returned when a lock does not exist in memcached
	ErrNotFound = errors.New("locker: not found")

	// ErrForbidden is returned when an owner attempts to release a non-owned locked
	ErrForbidden = errors.New("locker: forbidden")
)

Functions

This section is empty.

Types

type Locker

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

Locker is the main entrypoint for locking operations

func New

func New(c adapters.Adapter, name, owner string) *Locker

New creates a new Locker instance

func (*Locker) GetCurrentOwner

func (l *Locker) GetCurrentOwner() (string, error)

GetCurrentOwner returns the current lock owner return ErrNotFound if lock does not exist

func (*Locker) Lock

func (l *Locker) Lock(ttl time.Duration) (err error)

Lock attempts to lock for the given TTL. returns ErrNotAcquired if the resource was already locked.

func (*Locker) Release

func (l *Locker) Release() (err error)

Release attempts to release a lock return ErrNotFound if the lock does not exist return ErrForbidden if the locker does not own the lock

Directories

Path Synopsis
gomemcache
Package gomemcache implements the adapters.Adapter interface for https://github.com/bradfitz/gomemcache/blob/master/memcache/memcache.go
Package gomemcache implements the adapters.Adapter interface for https://github.com/bradfitz/gomemcache/blob/master/memcache/memcache.go

Jump to

Keyboard shortcuts

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