/singleflight

Replace the community implementation of SingleFlight, and the caller no longer needs to perform type assertion.

Primary LanguageGoApache License 2.0Apache-2.0

singleflight How to use the singleflight common component

  1. 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.
  2. The component supports native Prometheus metric reporting by default, and business parties do not need to implement it repeatedly.
type xStruct struct{}

// Declare GenericsReader in the structure, and specify the type to be converted after singleflight
type testStruct struct {
    Group[*xStruct]
}

func New() *testStruct{
	// If you want to use the metric, you need to initialize the Group and pass in the group name
    sf := NewGroup[*xStruct]("group_name")
	return &testStruct{
		Group:sf,
	}
}

func (t *testStruct) read()  {
	t.Do(key, func() (*xStruct, error) {
		// Business request logic
	})
}