etcdmutex

package module
v0.0.0-...-10cfadf Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

etcdmutex

etcdmutex 是 Go 中使用 etcd 的分布式锁库。它像sync.Mutex一样易于使用

其实有很多类似的实现都已经过时了,取决于具体"go.etcd.io/etcd/clientv3"官方标注的库,用法很简单,代码也很简单。

#Import

go get -u github.com/CaoZhechuan/etcdmutex

#Simplest usage

争抢锁默认抢三次,每一秒尝试获取一次,若三次未获取到锁,则放弃争抢锁,因此需要对error判断,避免对未获取锁进行解锁操作。

Steps:

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

import (
	"log"
	"os"

	"github.com/CaoZhechuan/etcdmutex"
)

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")
	}
}


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
}

func New

func New(key string, ttl int64, endpoints []string, etcdCert string, etcdCertKey string, etcdCa string) (*Mutex, error)

new一个etcd客户端,

func (*Mutex) Lock

func (m *Mutex) Lock() error

func (*Mutex) SetDebugLogger

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

func (*Mutex) Unlock

func (m *Mutex) Unlock() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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