Created
November 9, 2022 11:22
-
-
Save hotafrika/b7dca578c2afba34496985b2524349bf 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, func() (interface{}, error) { | |
return a.GetNodeStatSync(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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment