DLocker

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

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

Go to latest
Published: Aug 27, 2016 License: MIT Imports: 6 Imported by: 4

README

go-zk-lock

Build Status

a distributed locker based on zookeeper and implemented in golang.

Installation

go get github.com/samuel/go-zookeeper/zk
go get github.com/nladuo/go-zk-lock

Usage

Configure the Zookeeper

You can check out the zookeeper configuration here.

Set Your Configuration
var (
        hosts         []string      = []string{"127.0.0.1:2181"} // the zookeeper hosts
        basePath      string        = "/locker"                  //the application znode path
        lockerTimeout time.Duration = 1 * time.Minute            // the maximum time for a locker waiting
        zkTimeOut     time.Duration = 20 * time.Second           // the zk connection timeout
)
Establish Zookeeper Connection
err := DLocker.EstablishZkConn(hosts, zkTimeOut)
defer DLocker.CloseZkConn()
if err != nil {
        panic(err)
}
Create Distributed Locker
locker := DLocker.NewLocker(basePath, lockerTimeout)
Lock And Unlock
locker.Lock() // like mutex.Lock()
//do something of which time not excceed lockerTimeout
if !locker.Unlock() { // like mutex.Unlock(), return false when zookeeper connection error or locker timeout
        log.Println("Sorry, unlock failed")
}

Documentation

Overview

the zk initialization

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseZkConn

func CloseZkConn()

func CreatePath

func CreatePath(path string)

func EstablishZkConn

func EstablishZkConn(_hosts []string, zkTimeOut time.Duration) error

Types

type Dlocker

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

func NewLocker

func NewLocker(path string, timeout time.Duration) *Dlocker

func (*Dlocker) Lock

func (this *Dlocker) Lock()

just list mutex.Lock()

func (*Dlocker) Unlock

func (this *Dlocker) Unlock() bool

just list mutex.Unlock(), return false when zookeeper connection error or locker timeout

Directories

Path Synopsis
some algorism
some algorism

Jump to

Keyboard shortcuts

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