etcdsync

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

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

Go to latest
Published: Aug 10, 2018 License: Apache-2.0 Imports: 8 Imported by: 13

README

etcdsync

etcdsync is a distributed lock library in Go using etcd. It easy to use like sync.Mutex.

In fact, there are many similar implementation which are all obsolete depending on library github.com/coreos/go-etcd/etcd which is official marked deprecated, and the usage is a little bit complicated. Otherwise this library is very very simple. The usage is simple, the code is simple.

Import

go get -u github.com/zieckey/etcdsync

Simplest usage

Steps:

  1. m, err := etcdsync.New()
  2. m.Lock()
  3. Do your business here
  4. m.Unlock()
package main

import (
	"log"
	"os"

	"github.com/zieckey/etcdsync"
)

func main() {
	m, err := etcdsync.New("/mylock", 10, []string{"http://127.0.0.1:2379"})
	if m == nil || err != nil {
		log.Printf("etcdsync.New failed")
		return
	}
	err = m.Lock()
	if err != nil {
		log.Printf("etcdsync.Lock failed")
	} else {
		log.Printf("etcdsync.Lock OK")
	}

	log.Printf("Get the lock. Do something here.")

	err = m.Unlock()
	if err != nil {
		log.Printf("etcdsync.Unlock failed")
	} else {
		log.Printf("etcdsync.Unlock OK")
	}
}

Test

etcd2 or etcd3 test OK.

You need a etcd instance running on http://localhost:2379, then:

go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

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

A Mutex is a mutual exclusion lock which is distributed across a cluster.

func New

func New(key string, ttl int, machines []string) (*Mutex, error)

New creates a Mutex with the given key which must be the same across the cluster nodes. machines are the ectd cluster addresses

func (*Mutex) Lock

func (m *Mutex) Lock() (err error)

Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.

func (*Mutex) RefreshLockTTL

func (m *Mutex) RefreshLockTTL(ttl time.Duration) (err error)

func (*Mutex) SetDebugLogger

func (m *Mutex) SetDebugLogger(w io.Writer)

func (*Mutex) Unlock

func (m *Mutex) Unlock() (err error)

Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock.

A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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