• Stars
    star
    1,456
  • Rank 32,304 (Top 0.7 %)
  • Language
    Go
  • License
    Other
  • Created over 5 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simplified distributed locking implementation using Redis

redislock

Test GoDoc License

Simplified distributed locking implementation using Redis. For more information, please see examples.

Examples

import (
  "context"
  "fmt"
  "log"
  "time"

  "github.com/bsm/redislock"
  "github.com/redis/go-redis/v9"
)

func main() {
	// Connect to redis.
	client := redis.NewClient(&redis.Options{
		Network:	"tcp",
		Addr:		"127.0.0.1:6379",
	})
	defer client.Close()

	// Create a new lock client.
	locker := redislock.New(client)

	ctx := context.Background()

	// Try to obtain lock.
	lock, err := locker.Obtain(ctx, "my-key", 100*time.Millisecond, nil)
	if err == redislock.ErrNotObtained {
		fmt.Println("Could not obtain lock!")
	} else if err != nil {
		log.Fatalln(err)
	}

	// Don't forget to defer Release.
	defer lock.Release(ctx)
	fmt.Println("I have a lock!")

	// Sleep and check the remaining TTL.
	time.Sleep(50 * time.Millisecond)
	if ttl, err := lock.TTL(ctx); err != nil {
		log.Fatalln(err)
	} else if ttl > 0 {
		fmt.Println("Yay, I still have my lock!")
	}

	// Extend my lock.
	if err := lock.Refresh(ctx, 100*time.Millisecond, nil); err != nil {
		log.Fatalln(err)
	}

	// Sleep a little longer, then check.
	time.Sleep(100 * time.Millisecond)
	if ttl, err := lock.TTL(ctx); err != nil {
		log.Fatalln(err)
	} else if ttl == 0 {
		fmt.Println("Now, my lock has expired!")
	}

}

Documentation

Full documentation is available on GoDoc

More Repositories

1

sarama-cluster

Cluster extensions for Sarama, the Go client library for Apache Kafka 0.9 [DEPRECATED]
Go
1,008
star
2

redeo

High-performance framework for building redis-protocol compatible TCP servers/services
Go
439
star
3

openrtb

OpenRTB protocol defintions for Go
Go
288
star
4

grpclb

External Load Balancing Service solution for gRPC written in Go
Go
263
star
5

grape-kaminari

kaminari paginator integration for grape API framework
Ruby
160
star
6

redis-lock

[DEPRECATED] Please see https://github.com/bsm/redislock instead
Go
142
star
7

ratelimit

Simple, thread-safe Go rate-limiter
Go
81
star
8

attribute-defaults

Simple ActiveRecord plugin that allows to specify default values for attributes
Ruby
52
star
9

poseidon_cluster

Poseidon cluster extensions
Ruby
42
star
10

fakengx

Library for testing Lua scripts embedded into Nginx
Lua
40
star
11

sidekiq-datadog

Ruby
31
star
12

lua-resty-http

Lua HTTP client driver for ngx_lua
Lua
31
star
13

activesupport-cache-database

ActiveSupport::Cache::Store implementation backed by a database via ActiveRecord
Ruby
28
star
14

ssdb-rb

Ruby client library for SSDB
Ruby
17
star
15

go-guid

MongoDB style globally unique identifiers in Go
Go
16
star
16

bfs

Multi-adapter bucket-based file system abstraction. #golang
Go
14
star
17

bitmap.lua

Lua bitmaps (aka bitstrings or bitsets) implemented in C
C
14
star
18

planb

Build distributed, low-latency services with a redis-compatible protocol and sentinel client support
Go
13
star
19

go-geohex

GeoHex implementation in Go
Go
11
star
20

bps

Pub/sub & message processing abstraction
Go
11
star
21

appdash-rb

Ruby client for Appdash
Ruby
10
star
22

grape-pagy

Ruby
10
star
23

redpear

A simple, elegant & efficient ORM for Redis, optimised for speed!
Ruby
9
star
24

preferable

User preference management for ActiveRecord
Ruby
9
star
25

extsort

External merge sort algorithm, implemented in Go
Go
9
star
26

raft-badger

Raft backend implementation using Badger
Go
9
star
27

paperclip_remote

Plugin for Paperclip: Allows fetching attachments from remote locations
Ruby
8
star
28

openmetrics

A standalone, dependency-free implementation of OpenMetrics v1.0
Go
8
star
29

histogram

Streamining histograms in Go
Go
8
star
30

qualify

Match a fact against large number of pre-defined rules in Go
Go
8
star
31

fiddle

Ruby
7
star
32

datadog-notifications

Ruby
7
star
33

drone-s3-cache

Go
6
star
34

macdaddy

MAC Daddy is a Go library for generating encrypted messages and verifying their authenticity using the Poly1305 message authentication code with a ChaCha20 cipher
Go
6
star
35

grape-app

Ruby
6
star
36

pivotable

Ruby
6
star
37

go-sparkey

Go
6
star
38

bitset.lua

Bitsets (aka bitstrings or bitmaps) implemented in pure Lua
Lua
5
star
39

flood

Go
5
star
40

pgpq

Priority queues with Postgres
Go
5
star
41

httpx

Useful and opinionated helpers for building secure HTTP services
Go
4
star
42

sntable

Fast, custom SSTable implementation with numeric keys.
Go
4
star
43

redeoraft

Raft transport implementation for Redeo servers #raft #golang #redeo #redis
Go
4
star
44

reason

Go
4
star
45

redis-balancer

Go
4
star
46

mlmetrics

Common metrics for evaluation of machine learning models
Go
4
star
47

constrainable

Filtering for ActiveRecord. Sanitizes readable query parameters - great for building APIs & HTML filters.
Ruby
4
star
48

accord

Go
3
star
49

serialization_scopes

Define output scopes for XML/JSON serialization of your ActiveRecord models
Ruby
3
star
50

sarama

Sarama cluster extensions
3
star
51

streamsort

DEPRECATED: please use https://github.com/bsm/extsort instead
Go
3
star
52

sortable-by

Ruby
3
star
53

ccdb

Go
3
star
54

fleiss

Ruby
3
star
55

multiredis

Go abstraction of various redis client types for simpler testing
Go
3
star
56

cdb64

Go
3
star
57

ginkgo

Straight copy of the excellent Ginkgo library, stripped to the bare core to be free of third-party dependencies.
Go
3
star
58

zetasketch

Go
2
star
59

cntdb

Go
2
star
60

gomega

Straight copy of the excellent Gomega library, stripped to the bare core to be free of third-party dependencies
Go
2
star
61

dbx

Useful extensions to stdlib's database/sql
Go
2
star
62

nanoid

Go
2
star
63

geohex.lua

GeoHex V3 library for Lua
Lua
2
star
64

fluq

Coming soon
Ruby
2
star
65

redis-tools

Simple redis CLI helpers
Ruby
2
star
66

filterable-by

Ruby
2
star
67

grape-apidoc

Ruby
2
star
68

grpctools

Go
2
star
69

go-x

Useful extensions to the Go stdlib.
Go
2
star
70

shutdown

Go
2
star
71

firejwt

Validation for Firebase JWT
Go
2
star
72

pool

Go
2
star
73

strset

Go
2
star
74

devise_imap_authenticatable

Ruby
2
star
75

feedx

Go
2
star
76

pbio

Ruby
1
star
77

bst

Fast and generic Set and Map implementations using binary-search-trees.
Go
1
star
78

bsm.github.io

HTML
1
star
79

geo_hex

Ruby
1
star
80

pipa

Go
1
star
81

redis_recipes

Collection of Redis LUA recipes. Require Redis 2.6.0 or higher.
Ruby
1
star
82

geohashi

Go(lang) clone of geohash-int
Go
1
star
83

forensiq

Small tool for collecting forensic stats and storing them in Redis.
Go
1
star
84

redis-cluster

Go
1
star
85

disquo

Minimalist, threaded high-performance Ruby workers on top of Disque
Ruby
1
star
86

mgmt

Ruby
1
star
87

go-benchmark

Go (Awesome) Benchmarks
Go
1
star
88

intset

Go
1
star
89

paperclip_removable

Paperclip plugin: Allows removal of previously uploaded files
Ruby
1
star
90

grape-app-doc

Ruby
1
star
91

redpear.lua

Simplistic object mapper for Redis/Lua
Lua
1
star
92

gemical

Gemical command-line client.
Ruby
1
star
93

go-vlq

Variable-length quantity encoding
Go
1
star
94

paperclip_bsm_s3

Custom extensions to Paperclip
Ruby
1
star
95

capistrano-golang

Ruby
1
star
96

quasizero

TCP server (and client) implementation, optimised for low latency and pipelined throughput
Go
1
star
97

active-record-atomic

Ruby
1
star
98

timed_lru

Simple, thread-safe LRU with (optional) TTLs and constant time operations
Ruby
1
star
99

tdigest

Implementation of Ted Dunning's t-digest in Go
Go
1
star