Skip to content

adrian-lin-1-0-0/redlock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RedLock

distributed-locks

透過 github.com/redis/go-redis/v9 簡單實作 RedLock

如果你需要使用到RedLock,請使用 redsync

因為更新Database的資料前,可能遇到stop the world gc,所以還需要搭配 樂觀鎖或其他方式來避免stop the world gc之後鎖過期被取走的問題:

	rl := redlock.New(
		&redlock.Options{
			Addr: "localhost:6379",
		},
		&redlock.Options{
			Addr: "localhost:6380",
		},
	)

	m := rl.NewMutex("test1", 10*time.Second)
	err := m.Lock()
	if err != nil {
		log.Default().Println(err)
		m.Unlock()
		return
	}

	mysql := NewMysql()
	data := mysql.GetTestById(1)
	version := data.Version
	newVersion := version + 1
	mysql.UpdateTest.
		Where("id", 1).
		Where("version", version).
		Set("version", newVersion).
		Set("value", "new value").
		Exec()

	m.Unlock()

Redlock分析

Martin Kleppmann的分析

與此分析相反的觀點

About

redlock 讀書筆記與demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages