locker

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

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

Go to latest
Published: Aug 22, 2018 License: MIT Imports: 4 Imported by: 0

README

Distributed Locker

Build Status Go Report Card GoDoc

Install

$ go get github.com/zoer/locker

Usage

package main

import (
	"context"
	"log"

	etcd "github.com/coreos/etcd/clientv3"
)

func main() {
	// connect to Etcd instance
	cl, err := clientv3.NewFromURL("127.0.0.1:2379")
	if err != nil {
		log.Fatalf("unable connect to Etcd")
	}

	lkr := NewEtcd(cl)

	// cancel the lock via context timeout after 30 seconds
	ctx, _ := context.WithTimeout(context.TODO(), 30*time.Second)

	l, err := lkr.Lock(ctx, WithKey("lock-key"))
	if err != nil {
		log.Fatalf("unable get a lock: %v", err)
	}
	defer l.Unlock()

	// ...
}

TODO

  • Etcd
  • Consul
  • Redis

Documentation

Overview

Example
// connect to Etcd instance
cl, err := clientv3.NewFromURL("127.0.0.1:2379")
if err != nil {
	log.Fatalf("unable connect to Etcd")
}

lkr := NewEtcd(cl)

// cancel the lock via context timeout after 30 seconds
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()

l, err := lkr.Lock(ctx, WithKey("lock-key"))
if err != nil {
	log.Fatalf("unable get a lock: %v", err)
}
defer l.Unlock()

// ...
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

type Lock interface {
	Unlock()
}

Lock represents interface to unlock existed lock

type Locker

type Locker interface {
	Lock(context.Context, ...Option) (Lock, error)
}

Locker represents interface to create a lock

func NewEtcd

func NewEtcd(cl *etcd.Client, oo ...Option) Locker

NewEtcd creates Etcd locker

type Option

type Option func(*options)

Option represents the lock option

func WithKey

func WithKey(key string) Option

WithKey sets the lock's key

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the TTL option.

func WithWaitTTL

func WithWaitTTL(ttl time.Duration) Option

WithWaitTTL sets the wait for receiving the lock TTL.

Jump to

Keyboard shortcuts

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