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
func main() { | |
router := chi.NewRouter() | |
router.Use(httprate.LimitByIP(100, time.Minute)) | |
router.Get("/info", infoHandler) | |
router.Group(func(router chi.Router) { | |
router.Use(httprate.Limit(20, time.Minute, httprate.WithKeyFuncs( | |
httprate.KeyByIP, | |
httprate.KeyByEndpoint, |
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
func main() { | |
router := chi.NewRouter() | |
router.Use(httprate.LimitByIP(100, time.Minute)) | |
router.Get("/info", infoHandler) | |
router.Group(func(router chi.Router) { | |
router.Use(httprate.Limit(20, time.Minute, httprate.WithKeyFuncs( | |
httprate.KeyByIP, | |
httprate.KeyByEndpoint, |
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
func main() { | |
router := chi.NewRouter() | |
router.Get("/info", infoHandler) | |
router.Group(func(router chi.Router) { | |
router.Use(authMiddleware) | |
router.Get("/profile", profileHandler) | |
router.Get("/stat", statHandler) | |
}) |
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 |
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) { | |
f := func() (interface{}, error){ | |
return a.GetNodeStatSync(nodeID) | |
} | |
result, err, _ := a.g.Do(nodeID, f) | |
if err != nil { |
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) |
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 NodeStat struct { | |
// NodeData []byte // omit it for this example | |
NodeID string | |
Timestamp time.Time | |
} | |
type App struct {} | |
func (a *App) GetNodeStatSync(nodeID string) (*NodeStat, error) { | |
collectedAt := time.Now() |