Skip to content

Instantly share code, notes, and snippets.

@hotafrika
Created November 9, 2022 11:21
Show Gist options
  • Save hotafrika/260147b8fe1a06be473fa3d075da78eb to your computer and use it in GitHub Desktop.
Save hotafrika/260147b8fe1a06be473fa3d075da78eb to your computer and use it in GitHub Desktop.
medium / singleflight
type App struct {
g singleflight.Group
}
func (a *App) GetNodeStat(nodeID string) (*NodeStat, error) {
f := func() (interface{}, error){
return a.GetNodeStatSync(nodeID)
}
result, err, _ := a.g.Do(nodeID, f)
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