Skip to content

Commit

Permalink
Merge pull request #70 from allinbits/fix/router-race-condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi authored Mar 4, 2022
2 parents 9aa2502 + 461aef0 commit 0929586
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
10 changes: 9 additions & 1 deletion api/router/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ func GetDeps(c *gin.Context) *Deps {
panic(fmt.Sprintf("deps not of the expected type, found %T", deps))
}

// override logger with the one from the gin.context
logger, err := logging.GetLoggerFromContext(c)
if err == nil {
deps.Logger = logger
} else {
deps.Logger.Warnw("couldn't get logger from context, using fallback", "error", err)
}

return deps
}

// WriteError lgos and return client-facing errors
// WriteError logs and return client-facing errors
func (d *Deps) WriteError(c *gin.Context, err Error, logMessage string, keyAndValues ...interface{}) {

// setting error id
Expand Down
14 changes: 2 additions & 12 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func New(
if debug {
engine.Use(logging.LogRequest(l.Desugar()))
}
engine.Use(r.setLoggerFromContext)
engine.Use(r.catchPanicsFunc)
engine.Use(r.decorateCtxWithDeps)
engine.Use(r.handleErrors)
Expand All @@ -91,16 +90,6 @@ func (r *Router) Serve(address string) error {
return r.g.Run(address)
}

func (r *Router) setLoggerFromContext(c *gin.Context) {
l, err := logging.GetLoggerFromContext(c)
if err != nil && r.l == nil {
panic("cant get logger from context")
}
if l != nil {
r.l = l
}
}

func (r *Router) catchPanicsFunc(c *gin.Context) {
defer func() {
if rval := recover(); rval != nil {
Expand All @@ -110,7 +99,8 @@ func (r *Router) catchPanicsFunc(c *gin.Context) {
errors.New("internal server error"),
http.StatusInternalServerError)

r.l.Errorw(
logger := logging.AddCorrelationIDToLogger(c, r.l)
logger.Errorw(
"panic handler triggered while handling call",
"endpoint", c.Request.RequestURI,
"error", fmt.Sprint(rval),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/alicebob/miniredis/v2 v2.18.0
github.com/allinbits/demeris-backend-models v1.0.0
github.com/allinbits/emeris-cns-server v0.0.0-20211201093144-fa626186dded
github.com/allinbits/emeris-utils v1.1.1
github.com/allinbits/emeris-utils v1.1.2
github.com/allinbits/sdk-service-meta v0.0.0-20220225122854-eb00092170cc
github.com/allinbits/starport-operator v0.0.1-alpha.45
github.com/cockroachdb/cockroach-go/v2 v2.2.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ github.com/allinbits/demeris-backend-models v1.0.0 h1:USjPbAeHpaqO1Gg1GrseQif33S
github.com/allinbits/demeris-backend-models v1.0.0/go.mod h1:P/PDgyB6Y/GcBcODjzHDfAT+WtDl86Qqq/LIbwHJAPI=
github.com/allinbits/emeris-cns-server v0.0.0-20211201093144-fa626186dded h1:AD2UB10Bx7QgStLnJ6Jp1XQD2Y2u9Js0TMCJIhd61n8=
github.com/allinbits/emeris-cns-server v0.0.0-20211201093144-fa626186dded/go.mod h1:FuP1e/sGgjXI/pJ0ipHmEWLHHQWMPwnT8rDFz2WKKUE=
github.com/allinbits/emeris-utils v1.1.1 h1:x9oEPATvNvRkMVVoewLXlo+5rULpiEygvjH7MBwCv2c=
github.com/allinbits/emeris-utils v1.1.1/go.mod h1:tMkUvBItgKYJZJUx8zpE9QWm+lqUb4MvT3fKohYOoHI=
github.com/allinbits/emeris-utils v1.1.2 h1:18W+W3cF6rs34BqsYxfV/XHBBSaTo7Sh4zcGaDXpQXU=
github.com/allinbits/emeris-utils v1.1.2/go.mod h1:tMkUvBItgKYJZJUx8zpE9QWm+lqUb4MvT3fKohYOoHI=
github.com/allinbits/sdk-service-meta v0.0.0-20220225122854-eb00092170cc h1:RYkjiawhVW5kyGedO87fhoJ/B+mPKz3UbMWyCtfoJHU=
github.com/allinbits/sdk-service-meta v0.0.0-20220225122854-eb00092170cc/go.mod h1:1qjnoOm6sxbMQp2uIMi2EaVIjAazPyom5HNUlGPZdAw=
github.com/allinbits/starport-operator v0.0.1-alpha.26/go.mod h1:rhYpjy5c3QXjSS0SGxeDU/vVuxwLFeni1MJbpBTKXqo=
Expand Down

0 comments on commit 0929586

Please sign in to comment.