Replace the community implementation of SingleFlight, and the caller no longer needs to perform type assertion.
GoApache-2.0
singleflight How to use the singleflight common component
Replace the community implementation golang.org/x/sync/singleflight, you can initiate a request through this component singleflight, and the caller no longer needs to perform type assertion.
The component supports native Prometheus metric reporting by default, and business parties do not need to implement it repeatedly.
typexStructstruct{}
// Declare GenericsReader in the structure, and specify the type to be converted after singleflighttypetestStructstruct {
Group[*xStruct]
}
funcNew() *testStruct{
// If you want to use the metric, you need to initialize the Group and pass in the group namesf:=NewGroup[*xStruct]("group_name")
return&testStruct{
Group:sf,
}
}
func (t*testStruct) read() {
t.Do(key, func() (*xStruct, error) {
// Business request logic
})
}