Created
November 9, 2022 11:19
-
-
Save hotafrika/f91fe01585fe359d994062e884db0204 to your computer and use it in GitHub Desktop.
medium / singleflight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type App struct { | |
g singleflight.Group | |
} | |
func (a *App) GetNodeStat(nodeID string) (*NodeStat, error) { | |
result, err, _ := a.g.Do(nodeID, a.getNodeStatFunc(nodeID)) | |
if err != nil { | |
return nil, err | |
} | |
stat, ok := result.(*NodeStat) | |
if !ok { | |
return nil, errors.New("unable to cast interface to NodeStat") | |
} | |
return stat, nil | |
} | |
func (a *App) getNodeStatFunc(nodeID string) func() (interface{}, error) { | |
return func() (interface{}, error) { | |
return a.GetNodeStatSync(nodeID) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment