当前位置:网站首页>Golang示例续期锁:Redis+Channel+sync.Mutex
Golang示例续期锁:Redis+Channel+sync.Mutex
2022-06-24 20:12:00 【游戏编程】
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) // 加锁 err := Lock("lock_key") if err != nil { return err } //解锁 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()) //...主业务代码 g.Go(func() error { time.Sleep(15 * time.Second) ch <- true return nil }) //锁续期 g.Go(func() error { ticker := time.NewTicker(time.Second * 1) for { select { // 任务还没执行完 每秒续期 case <-ticker.C: Expire("lock_key") fmt.Println(time.Now()) //收到执行完的 channel 就关闭time定时任务 case <-ch: ticker.Stop() return nil } } }) //等待信号量 if err = g.Wait(); err != nil { return err } close(ch) return nil}作者:Hello
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Android SQLite database
- Custom control - round dot progress bar (imitating one key acceleration in security guard)
- A plug-in framework for implementing registration free and login verification with hook technology
- The problem of multiple callback of video ads stimulated by applets (offcolse problem)
- Previous basic review
- 【Redis实现秒杀业务③】超卖问题之乐观锁具体实现
- 百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑
- @mysql
- Use of navigation and navigationui
- Working principle analysis of kubernetes architecture core components
猜你喜欢

Custom animation (simulated win10 loading animation)

A small program written this week
How can I persuade leaders to use DDD to construct the liver project?

Working principle analysis of kubernetes architecture core components
![[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem](/img/01/5ec4e5dae1748dce3d5dee33a24b0b.png)
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem
Hyperledger Fabric 2. X dynamic update smart contract

Source code analysis the problem that fragments cannot be displayed in the custom ViewGroup
WordPress add photo album function [advanced custom fields Pro custom fields plug-in series tutorial]

Design scheme of authority management of fusion model

iNFTnews | 国内NFT发展仅限于数字藏品吗?
随机推荐
In the process of enterprise development, I found that a colleague used the select * from where condition for update
ServerSocket and socket connection
Mobile security tool -dex2jar
JSON file of China's provincial boundaries
What is test development? Can you find a job at this stage?
Input series
2021-09-12
2021-02-15
Use of JMeter easynmon
adb shell sendevent
Android SQLite database
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
Custom control - round dot progress bar (imitating one key acceleration in security guard)
ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
Helm chart仓库操作
在企业级开发过程中我发现有位同事用select * from where 条件 for update
Apk slimming compression experience
More pictures | explain the Nacos parameters in detail!
Only positive integers can be entered in the text box
Apk decompiled method (not confused)