https://github.com/golang-infrastructure/go-singleflight
generic version of singleflight
https://github.com/golang-infrastructure/go-singleflight
generic golang singleflight
Last synced: 3 months ago
JSON representation
generic version of singleflight
- Host: GitHub
- URL: https://github.com/golang-infrastructure/go-singleflight
- Owner: golang-infrastructure
- License: mit
- Created: 2022-11-22T16:03:02.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-22T17:16:22.000Z (over 2 years ago)
- Last Synced: 2025-01-18T16:11:01.374Z (5 months ago)
- Topics: generic, golang, singleflight
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go singleflight 泛型版
# 一、这是啥
Go官方扩展库singleflight的泛型实现:
```text
https://cs.opensource.google/go/x/sync
```# 二、安装
```bash
go get -u github.com/golang-infrastructure/go-singleflight
```# 三、Example
```go
package mainimport (
"fmt"
"github.com/golang-infrastructure/go-singleflight"
"math/rand"
"sync"
"time"
)func main() {
var wg sync.WaitGroup
g := singleflight.Group[int]{}begin := time.Now()
for i := 0; i < 10; i++ {
id := i
wg.Add(1)
go func() {
defer wg.Done()
v, err, shared := g.Do("test", func() (value int, err error) {
fmt.Println(id, "开始执行了...")
time.Sleep(time.Second * 3)
return rand.Intn(100), nil
})
fmt.Println(id, v, err, shared)
}()
}
wg.Wait()
fmt.Println("cost: ", time.Now().Sub(begin).String())
fmt.Println("All done")
}
```# 四、TODO
- 写篇文章讨论一下singleflight,分析一下源码
- 跑下benchmark,看下泛型版对性能到底有多大的影响