• Stars
    star
    1,246
  • Rank 36,168 (Top 0.8 %)
  • Language
    Go
  • License
    Other
  • Created almost 5 years ago
  • Updated 5 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,007
star
2

redeo

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

openrtb

OpenRTB protocol defintions for Go
Go
278
star
4

grpclb

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

grape-kaminari

kaminari paginator integration for grape API framework
Ruby
159
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
79
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
27
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

bitmap.lua

Lua bitmaps (aka bitstrings or bitsets) implemented in C
C
13
star
17

bfs

Multi-adapter bucket-based file system abstraction. #golang
Go
12
star
18

planb

Build distributed, low-latency services with a redis-compatible protocol and sentinel client support
Go
12
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

redpear

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

preferable

User preference management for ActiveRecord
Ruby
9
star
24

grape-pagy

Ruby
9
star
25

openmetrics

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

qualify

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

paperclip_remote

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

fiddle

Ruby
7
star
29

raft-badger

Raft backend implementation using Badger
Go
7
star
30

extsort

External merge sort algorithm, implemented in Go
Go
7
star
31

drone-s3-cache

Go
6
star
32

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
33

grape-app

Ruby
6
star
34

pivotable

Ruby
6
star
35

datadog-notifications

Ruby
6
star
36

go-sparkey

Go
6
star
37

bitset.lua

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

flood

Go
5
star
39

httpx

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

sntable

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

redeoraft

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

reason

Go
4
star
43

redis-balancer

Go
4
star
44

constrainable

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

accord

Go
3
star
46

serialization_scopes

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

histogram

Streamining histograms in Go
Go
3
star
48

sarama

Sarama cluster extensions
3
star
49

streamsort

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

sortable-by

Ruby
3
star
51

ccdb

Go
3
star
52

mlmetrics

Common metrics for evaluation of machine learning models
Go
3
star
53

pgpq

Priority queues with Postgres
Go
3
star
54

fleiss

Ruby
3
star
55

cdb64

Go
3
star
56

ginkgo

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

zetasketch

Go
2
star
58

cntdb

Go
2
star
59

dbx

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

nanoid

Go
2
star
61

geohex.lua

GeoHex V3 library for Lua
Lua
2
star
62

fluq

Coming soon
Ruby
2
star
63

redis-tools

Simple redis CLI helpers
Ruby
2
star
64

filterable-by

Ruby
2
star
65

grape-apidoc

Ruby
2
star
66

grpctools

Go
2
star
67

go-x

Useful extensions to the Go stdlib.
Go
2
star
68

shutdown

Go
2
star
69

firejwt

Validation for Firebase JWT
Go
2
star
70

pool

Go
2
star
71

strset

Go
2
star
72

multiredis

Go abstraction of various redis client types for simpler testing
Go
2
star
73

devise_imap_authenticatable

Ruby
2
star
74

feedx

Go
2
star
75

pbio

Ruby
1
star
76

gomega

Straight copy of the excellent Gomega library, stripped to the bare core to be free of third-party dependencies
Go
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

disquo

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

go-benchmark

Go (Awesome) Benchmarks
Go
1
star
86

intset

Go
1
star
87

paperclip_removable

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

timed_lru

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

grape-app-doc

Ruby
1
star
90

gemical

Gemical command-line client.
Ruby
1
star
91

mgmt

Ruby
1
star
92

paperclip_bsm_s3

Custom extensions to Paperclip
Ruby
1
star
93

redpear.lua

Simplistic object mapper for Redis/Lua
Lua
1
star
94

go-vlq

Variable-length quantity encoding
Go
1
star
95

redis-cluster

Go
1
star
96

capistrano-golang

Ruby
1
star
97

quasizero

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

tdigest

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

active-record-atomic

Ruby
1
star