solution lock for golang, locallock and remote lock base on redis.

Overview

Glock

all lock module and ratelimit using redis for controll.

install

go get github.com/teng231/glock

usage

many structs and methods, not all but i think it's help you for lock.

counter lock

Counter lock: It's distributed counter, counter can run up or run down. If run up, you can hanlder check and it's not atomic before all node. Counter down or coundown it's good. Helpful for count turn.

func Run(){
	cd, err := CreateCountLock("localhost:6379", "", "test3_", time.Minute)
	if err != nil {
		log.Print(err)
		t.Fail()
	}
	cd.Start("test1", 100, time.Minute)
	cur, _ := cd.DecrBy("test1", 1)
	log.Print(cur)
	if cur != 99 {
		log.Print(cur)
		t.Fail()
	}
}

distributed lock

As You know about this. It's can be lock for all node. Request run one by one, depend on a key. If 2 request going at the same time. Fist request come run and when its done. Second request continue process.

func Run(){
	dl, err := CreateDistributedLock("localhost:6379", "", "test2_", 4*time.Second)
	if err != nil {
		panic(err)
	}
    lctx, err := dl.Lock("test-redsync")
    if err := dl.Unlock(lctx); err != nil {
				panic(err)
	}
}

If many request coming. Distributed lock it not good because many ping request to check redis. Redis can be down then lock system down.

kmutex

It's local lock like distributed lock but using mutex and waitgroup. You can using it combine with distributed lock.

func Run(){
	km := CreateKmutexInstance()
	km.Lock("key")
	defer km.Unlock("key")
}

limiter

handler limit like counter lock. but have duration

func Run(){
    r, err := CreateLimiter("localhost:6379", "", 2*time.Second)
	if err != nil {
		panic(err)
	}
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
	time.Sleep(1 * time.Second)
	if err := r.Allow("key1", Second, 5); err != nil {
		log.Print(err)
	}
}

optimistic lock

like distributed lock but request 2 come at the same time. It's will return false. Redis will lower traffics

func Run(){
    ol, err := CreateOptimisticLock("localhost:6379", "", "test_", time.Second)
	if err != nil {
		panic(err)
	}
	if err := ol.Lock("key1"); err != nil {
		log.Print(err)
	}
}
Owner
Te Nguyen
A little bits for thinking yourself
Te Nguyen
A Golang and Python solution for Queue-it's Proof-of-Work challenge.

Queue-it Proof-of-Work A Golang and Python solution for Queue-it's Proof-of-Work challenge (https://queue-it.com/blog/proof-of-work-block-bad-bots/).

eta 24 Jun 30, 2022
Flock is a project which provides a Go solution for system level file locks for all platforms Golang supports.

Flock is a project which provides a Go solution for system level file locks for all platforms Golang supports.

Ken Sipe 0 Feb 8, 2022
Magma: Gives network operators an open, flexible and extendable mobile core network solution

Connecting the Next Billion People Magma is an open-source software platform tha

nikhilc 0 Dec 24, 2021
Hotswap provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure.

Hotswap provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure. Hotswap is built upon the plugin mechanism.

Edwin 95 Aug 8, 2022
Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis.

go-ratelimiter Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis. Example usage client := redis.NewClient

null 0 Oct 19, 2021
Sliding window counters Redis rate limiting implementation for Golang

Sliding window counters Redis rate limiting implementation for Golang (Based on the Figma API rate limit algorithm)

Barnaby Keene 5 Apr 4, 2022
Small utility to cleanup entries with unexpected/erroneous ttl in redis

Small utility to cleanup entries with unexpected/erroneous ttl in redis

Selva 0 Apr 28, 2022
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

Spiral Scout 1.1k Aug 5, 2022
A Go (golang) library for parsing and verifying versions and version constraints.

go-version is a library for parsing versions and version constraints, and verifying versions against a set of constraints. go-version can sort a collection of versions properly, handles prerelease/beta versions, can increment versions, etc.

HashiCorp 1.2k Aug 8, 2022
Golang: unify nil and empty slices and maps

unifynil, unify nil and empty slices and maps in Golang Empty slices and maps can be nil or not nil in Go. It may become a nightmare in tests and JSON

Boris Nagaev 0 Jan 16, 2022
memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals

mem-resolver memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals How to use it Create your custom

Antonio Ojea 4 Jun 23, 2022
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Walter Schulze 998 Aug 5, 2022
Copier for golang, copy value from struct to struct and more

Copier I am a copier, I copy everything from one to another Features Copy from field to field with same name Copy from method to field with same name

Jinzhu 3.4k Aug 6, 2022
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Maxime Soulé 313 Aug 8, 2022
A well tested and comprehensive Golang statistics library package with no dependencies.

Stats - Golang Statistics Package A well tested and comprehensive Golang statistics library / package / module with no dependencies. If you have any s

Montana Flynn 2.5k Aug 8, 2022
Golang io.Reader and io.Writer but with limits

LimitIO io.Reader and io.Writer with limit.

LI Zhennan 68 May 11, 2022
Cogger is a standalone binary and a golang library that reads an internally tiled geotiff

Cogger is a standalone binary and a golang library that reads an internally tiled geotiff (optionally with overviews and masks) and rewrites it

Airbus DS GEO S.A. 71 Jun 23, 2022
⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior

SSC Engine An HTML render engine concept that brings frontend-like components experience to the server side with native html/template on steroids. Sup

Yurii Zinets 535 Aug 6, 2022
GoDynamic can load and run Golang dynamic library compiled by -buildmode=shared -linkshared

GoDynamic can load and run Golang dynamic library compiled by -buildmode=shared -linkshared How does it work? GoDynamic works like a dynamic

pkujhd 8 Aug 8, 2022