mcmutex

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

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

Go to latest
Published: Jun 14, 2016 License: MIT Imports: 3 Imported by: 0

README

About

This is a simple mutex library using memcached for the Go programming language (http://golang.org/).

Memcached client is golibmc (https://github.com/douban/libmc/blob/master/src/golibmc.go)

GoDoc is Here (https://godoc.org/github.com/mosasiru/mcmutex)

Installing

Using go get

$ go get github.com/mosasiru/mcmutex

After this command mutex is ready to use. Its source will be in:

$GOPATH/src/github.com/mosasiru/mcmutex

Example

import (
    "github.com/douban/libmc/golibmc"
    "github.com/mosasiru/mcmutex"
)

func main() {
    mc := golibmc.SimpleNew([]string{"127.0.0.1:11211"})
    mutex := mcmutex.NewMCMutex(mc)
    defer mutex.Unlock("key")
    if err := mutex.Lock("key"); err != nil {
        println("lock failed")
        return
    }
    ...
}

Configure

Retry

retry count before acquisition lock (default: 0)

Interval

retry interval (default: 10ms)

Expiration

lock will be expired after Expiration time (default: 30s)

Appendix

This library features perl's KeyedMutex::Memcached (http://search.cpan.org/~zigorou/KeyedMutex-Memcached-0.01/lib/KeyedMutex/Memcached.pm)

Documentation

Overview

Package mcmutex provides a mutex using memcached(golibmc)

Index

Constants

This section is empty.

Variables

View Source
var ErrLockFailed = errors.New("failed to acquire lock")

ErrLockFailed means failure to acquire lock after all retrys.

Functions

This section is empty.

Types

type MCMutex

type MCMutex struct {

	// retry interval
	Interval time.Duration

	// retry count before acquisition lock (default: 0)
	Retry int

	// lock will be expired after Expiration time (default: 30s)
	Expiration int64
	// contains filtered or unexported fields
}

MCMutex can create mutex using golibmc

func NewMCMutex

func NewMCMutex(mc *golibmc.Client) *MCMutex

NewMCMutex create *MCMutex using default configure.

func (*MCMutex) Lock

func (m *MCMutex) Lock(key string) error

Lock the key, or sleep and retry to lock according to configuration. it returns err when fail to acquire lock or got memcached error.

func (*MCMutex) Unlock

func (m *MCMutex) Unlock(key string) error

Unlock the key. it returns err when lock is already free or got memcached error.

Jump to

Keyboard shortcuts

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