当前位置:网站首页>Golang example renewal lock: redis+channel+sync Mutex
Golang example renewal lock: redis+channel+sync Mutex
2022-06-25 01:07:00 【Game programming】
package mainimport ( "context" "fmt" "github.com/go-redis/redis" "golang.org/x/sync/errgroup" "log" "sync" "time")func main() { NewRedis() fu1() time.Sleep(100 * time.Second)}var rdb *redis.Clientvar ctx = context.Background()var mutex sync.Mutexfunc NewRedis() { rdb = redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", Password: "", // no password set DB: 0, // use default DB })}func Lock(key string) error { mutex.Lock() defer mutex.Unlock() _, err := rdb.SetNX(key, 1, 1*time.Second).Result() if err != nil { log.Println(err.Error()) } return err}func UnLock(key string) error { _, err := rdb.Del(key).Result() if err != nil { log.Println(err.Error()) return err } return err}func Expire(key string) error { _, err := rdb.Expire(key, 1*time.Second).Result() if err != nil { log.Println(err.Error()) return err } return err}func fu1() error { ch := make(chan bool) // Lock err := Lock("lock_key") if err != nil { return err } // Unlock defer func() { err = UnLock("lock_key") if err != nil { fmt.Println(err.Error()) return } fmt.Println("release redis lock success") }() g, _ := errgroup.WithContext(context.Background()) //... Main business code g.Go(func() error { time.Sleep(15 * time.Second) ch <- true return nil }) // Lock renewal g.Go(func() error { ticker := time.NewTicker(time.Second * 1) for { select { // The task is not finished yet Renew per second case <-ticker.C: Expire("lock_key") fmt.Println(time.Now()) // Received the completed channel Just shut it down time Timing task case <-ch: ticker.Stop() return nil } } }) // Wait for the semaphore if err = g.Wait(); err != nil { return err } close(ch) return nil}
author :Hello
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Picture rotation move zoom gradient
- Contentresolver, get the SMS content
- 2022熔化焊接与热切割复训题库模拟考试平台操作
- 108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
- Scala trait exercise
- Qiniu cloud uploads video to get the first frame of video
- The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
- 丹麦技术大学首创将量子计算应用于能源系统潮流建模
- Distinguish between i++ and ++i seconds
- C#和C 的CAN通信实验
猜你喜欢
Scala IO reads by lexical units and numbers
重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
2022安全员-C证考试模拟100题及在线模拟考试
Add information on the left and add parts on the right of the status bar
QT electronic clock
Tiktok wallpaper applet v1.0.2 function, new arrival function
Xcode预览(Preview)显示List视图内容的一个Bug及解决
Syntax highlighting of rich text
2022熔化焊接与热切割复训题库模拟考试平台操作
【Redis实现秒杀业务③】超卖问题之乐观锁具体实现
随机推荐
ImageView展示网络图片
Scala trait construction mechanism
Qiniu cloud uploads video to get the first frame of video
Leetcode 1248. 统计「优美子数组」(害,突然发现只会暴力枚举了)
The basic principle and application of iterator and enhanced for
Previous basic review (link)
Previous basic review
Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system
图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
丹麦技术大学首创将量子计算应用于能源系统潮流建模
新手看过来,带你一次性了解“软考”
Use of file class filenamefilter & filefilter in io
QT(35)-操作EXCEL-QXlsx-QAxObject
2022熔化焊接与热切割复训题库模拟考试平台操作
2022r1 quick opening pressure vessel operation test questions and answers
Single blind box removal, social blind box and friend blind box program source code
Hot fix sophix multi-channel patch solution
【实用系列】家内wifi全覆盖
大厂高频软件测试面试题和答案都帮你准备好啦,备战金九银十
[microservices sentinel] cluster link | microservices cluster environment construction