Skip to content

Commit

Permalink
feat(backend:signalhandler): Add a way to ignore path in active reque…
Browse files Browse the repository at this point in the history
…st counter http middleware
  • Loading branch information
oxyno-zeta committed Oct 21, 2024
1 parent 2cafb75 commit 3b838de
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/pkg/golang-graphql-example/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (svr *Server) generateRouter() (http.Handler, error) {
// Answer
utils.AnswerWithError(c, err)
}))
router.Use(svr.signalHandlerSvc.ActiveRequestCounterMiddleware())
router.Use(svr.signalHandlerSvc.ActiveRequestCounterMiddleware([]string{}))
router.Use(correlationid.HTTPMiddleware(svr.logger))
router.Use(svr.tracingSvc.HTTPMiddlewareList(correlationid.GetFromContext)...)
router.Use(log.Middleware(svr.logger, correlationid.GetFromGin, tracing.GetTraceIDFromContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Service interface {
// That will be launched only when all incoming requests are finished.
OnExit(hook func())
// Middleware to count active requests.
ActiveRequestCounterMiddleware() gin.HandlerFunc
ActiveRequestCounterMiddleware(ignoredPathList []string) gin.HandlerFunc
// Is stopping system will return true if the application is stopping.
IsStoppingSystem() bool
// IncreaseActiveRequestCounter will increase active request counter by one.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion backend/pkg/golang-graphql-example/signalhandler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package signalhandler

import (
"github.com/gin-gonic/gin"
"github.com/samber/lo"
)

func (s *service) initializeServerMode() {
Expand All @@ -15,14 +16,21 @@ func (s *service) initializeServerMode() {
}()
}

func (s *service) ActiveRequestCounterMiddleware() gin.HandlerFunc {
func (s *service) ActiveRequestCounterMiddleware(ignoredPathList []string) gin.HandlerFunc {
// Check if server mode isn't enabled
if !s.serverMode {
return func(c *gin.Context) { c.Next() }
}

// Middleware
return func(c *gin.Context) {
if lo.Contains(ignoredPathList, c.FullPath()) {
// Next now
c.Next()
// Stop
return
}

// Send +1 to active request counter channel
s.activeRequestCounterChan <- 1

Expand Down

0 comments on commit 3b838de

Please sign in to comment.