網站首頁 編程語言 正文
這個東西很重要,可以經常用在項目當中,所以我們單獨拿出來進行講解。
在使用它之前我們需要導包:
?go get golang.org/x/sync/singleflight
golang/sync/singleflight.Group
是 Go 語言擴展包中提供了另一種同步原語,它能夠在一個服務中抑制對下游的多次重復請求。一個比較常見的使用場景是:我們在使用 Redis 對數據庫中的數據進行緩存,發生緩存擊穿時,大量的流量都會打到數據庫上進而影響服務的尾延時。
但是 golang/sync/singleflight.Group
能有效地解決這個問題,它能夠限制對同一個鍵值對的多次重復請求,減少對下游的瞬時流量。
使用方法
singleflight
類的使用方法就新建一個singleflight.Group
,使用其方法Do
或者DoChan
來包裝方法,被包裝的方法在對于同一個key,只會有一個協程執行,其他協程等待那個協程執行結束后,拿到同樣的結果。
Group
結構體
代表一類工作,同一個group
中,同樣的key
同時只能被執行一次
Do
方法
func (g *Group) Do(key string, fn func() (interface{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->}, error)) (v interface{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->}, err error, shared bool)
key
:同一個key
,同時只有一個協程執行
fn
:被包裝的函數
v
:返回值,即執行結果。其他等待的協程都會拿到
shared
:表示是否由其他協程得到了這個結果v
DoChan
方法
func (g *Group) DoChan(key string, fn func() (interface{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->}, error)) <-chan Result
和Do
差不多其實,因此我們就只講解Do
的實際應用場景了。
具體應用場景
var singleSetCache singleflight.Group func GetAndSetCache(r *http.Request, cacheKey string) (string, error) { log.Printf("request %s start to get and set cache...", r.URL) value, err, _ := singleSetCache.Do(cacheKey, func() (interface{}, error) { log.Printf("request %s is getting cache...", r.URL) time.Sleep(3 * time.Second) log.Printf("request %s get cache success!", r.URL) return cacheKey, nil }) return value.(string), err } func main() { r := gin.Default() r.GET("/sekill/:id", func(context *gin.Context) { ID := context.Param("id") cache, err := GetAndSetCache(context.Request, ID) if err != nil { log.Println(err) } log.Printf("request %s get value: %v", context.Request.URL, cache) }) r.Run() }
來看一下執行結果:
2022/12/29 16:21:18 request /sekill/5 start to get and set cache...
2022/12/29 16:21:18 request /sekill/5 is getting cache...
2022/12/29 16:21:18 request /sekill/9 start to get and set cache...
2022/12/29 16:21:18 request /sekill/9 is getting cache...
2022/12/29 16:21:18 request /sekill/9 start to get and set cache...
2022/12/29 16:21:18 request /sekill/5 start to get and set cache...
2022/12/29 16:21:18 request /sekill/5 start to get and set cache...
2022/12/29 16:21:18 request /sekill/9 start to get and set cache...
2022/12/29 16:21:18 request /sekill/9 start to get and set cache...
2022/12/29 16:21:18 request /sekill/5 start to get and set cache...
2022/12/29 16:21:19 request /sekill/9 start to get and set cache...
2022/12/29 16:21:19 request /sekill/5 start to get and set cache...
2022/12/29 16:21:21 request /sekill/9 get cache success!
2022/12/29 16:21:21 request /sekill/5 get cache success!
2022/12/29 16:21:21 request /sekill/5 get value: 5
2022/12/29 16:21:21 request /sekill/5 get value: 5
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?3.0106529s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/5"
2022/12/29 16:21:21 request /sekill/9 get value: 9
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.8090881s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/5"
2022/12/29 16:21:21 request /sekill/9 get value: 9
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.2166003s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/9"
2022/12/29 16:21:21 request /sekill/9 get value: 9
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.6064069s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/9"
2022/12/29 16:21:21 request /sekill/9 get value: 9
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.4178652s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/9"
2022/12/29 16:21:21 request /sekill/9 get value: 9
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.8101267s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/9"
2022/12/29 16:21:21 request /sekill/5 get value: 5
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?3.0116892s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/9"
2022/12/29 16:21:21 request /sekill/5 get value: 5
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.6074537s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/5"
2022/12/29 16:21:21 request /sekill/5 get value: 5
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ?2.4076473s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/5"
[GIN] 2022/12/29 - 16:21:21 | 200 | ? ? 2.218686s | ? ? ? 127.0.0.1 | GET ? ? ?"/sekill/5"
可以看到確實只有一個協程執行了被包裝的函數,并且其他協程都拿到了結果。
接下來我們來看一下它的原理吧!
原理
首先來看一下Group
結構體:
type Group struct { mu sync.Mutex // 鎖保證并發安全 m map[string]*call //保存key對應的函數執行過程和結果的變量。 }
然后我們來看一下call
結構體:
type call struct { wg sync.WaitGroup //用WaitGroup實現只有一個協程執行函數 val interface{} //函數執行結果 err error forgotten bool dups int //含義是duplications,即同時執行同一個key的協程數量 chans []chan<- Result }
然后我們來看一下Do
方法:
func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) { // 寫Group的m字段時,加鎖保證寫安全 g.mu.Lock() if g.m == nil { g.m = make(map[string]*call) } if c, ok := g.m[key]; ok { // 如果key已經存在,說明已經由協程在執行,則dups++并等待其執行結果,執行結果保存在對應的call的val字段里 c.dups++ g.mu.Unlock() c.wg.Wait() if e, ok := c.err.(*panicError); ok { panic(e) } else if c.err == errGoexit { runtime.Goexit() } return c.val, c.err, true } // 如果key不存在,則新建一個call,并使用WaitGroup來阻塞其他協程,同時在m字段里寫入key和對應的call c := new(call) c.wg.Add(1) g.m[key] = c g.mu.Unlock() g.doCall(c, key, fn) // 進來的第一個協程就來執行這個函數 return c.val, c.err, c.dups > 0 }
然后我們來分析一下doCall
函數:
func (g *Group) doCall(c *call, key string, fn func() (interface{}, error)) { c.val, c.err = fn() c.wg.Done() g.mu.Lock() delete(g.m, key) for _, ch := range c.chans { ch <- Result{c.val, c.err, c.dups > 0} } g.mu.Unlock() }
- 運行傳入的函數
fn
,該函數的返回值會賦值給c.val
和c.err
; - 調用
sync.WaitGroup.Done
方法通知所有等待結果的Goroutine
— 當前函數已經執行完成,可以從call
結構體中取出返回值并返回了; - 獲取持有的互斥鎖并通過管道將信息同步給使用
golang/sync/singleflight.Group.DoChan
方法的Goroutine
;
問題分析
分析了源碼之后,我們得出了一個結論,這個東西是用阻塞來實現的,這就引發了一個問題:如果我們處理的那個請求剛好遇到問題了,那么后面的所有請求都會被阻塞,也就是,我們應該加上適合的超時控制,如果在一定時間內,沒有獲得結果,那么就當作超時處理。
于是這個適合我們應該使用DoChan()
。兩者實現上完全一樣,不同的是, DoChan()
通過 channel
返回結果。因此可以使用 select
語句實現超時控制。
var singleSetCache singleflight.Group func GetAndSetCache(r *http.Request, cacheKey string) (string, error) { log.Printf("request %s start to get and set cache...", r.URL) retChan := singleSetCache.DoChan(cacheKey, func() (interface{}, error) { log.Printf("request %s is getting cache...", r.URL) time.Sleep(3 * time.Second) log.Printf("request %s get cache success!", r.URL) return cacheKey, nil }) var ret singleflight.Result timeout := time.After(2 * time.Second) select { case <-timeout: log.Println("time out!") return "", errors.New("time out") case ret = <-retChan: // 從chan中獲取結果 return ret.Val.(string), ret.Err } } func main() { r := gin.Default() r.GET("/sekill/:id", func(context *gin.Context) { ID := context.Param("id") cache, err := GetAndSetCache(context.Request, ID) if err != nil { log.Println(err) } log.Printf("request %s get value: %v", context.Request.URL, cache) }) r.Run() }
補充
這里其實還有一個Forget
方法,它可以在映射表中刪除某個鍵,接下來對鍵的調用就不會等待前面的函數返回了。
總結
當然,如果單次的失敗無法容忍,在高并發的場景下更好的處理方案是:
放棄使用同步請求,犧牲數據更新的實時性
“緩存” 存儲準實時的數據 + “異步更新” 數據到緩存
原文鏈接:https://blog.csdn.net/qq_61039408/article/details/128484679
相關推薦
- 2022-07-28 pyodps中的apply用法及groupby取分組排序第一條數據_python
- 2022-08-25 Python中的基本數據類型介紹_python
- 2022-07-17 JDK、JRE、JVM是什么?有什么區別?
- 2022-08-19 Python?Asyncio調度原理詳情_python
- 2022-01-22 Springboot + Redis 哨兵模式
- 2022-12-13 Python?urllib?入門使用詳細教程_python
- 2022-08-19 android九宮格鎖屏控件使用詳解_Android
- 2022-04-12 Oracle?Session每日統計功能實現_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支