Skip to content

Commit

Permalink
fix: make sure context are not canceled before request finish and set…
Browse files Browse the repository at this point in the history
… a default timeout
  • Loading branch information
trim21 committed Oct 20, 2024
1 parent c635c98 commit 78693d3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/go-playground/validator/v10 v10.22.1
github.com/go-resty/resty/v2 v2.15.3
github.com/go-sql-driver/mysql v1.8.1
github.com/google/uuid v1.6.0
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jarcoal/httpmock v1.3.1
github.com/jmoiron/sqlx v1.4.0
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand Down
2 changes: 0 additions & 2 deletions internal/auth/mysql_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func (m mysqlRepo) GetPermission(ctx context.Context, groupID uint8) (Permission
m.log.Error("can't find permission for group", zap.Uint8("user_group_id", groupID))
return Permission{}, nil
}

m.log.Error("unexpected error", zap.Error(err))
return Permission{}, errgo.Wrap(err, "dal")
}

Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ package main
import (
"fmt"

"github.com/google/uuid"

"github.com/bangumi/server/cmd"
"github.com/bangumi/server/internal/pkg/logger"
)

func main() {
uuid.EnableRandPool()
if err := cmd.Root.Execute(); err != nil {
logger.Fatal("failed to start app:\n" + fmt.Sprintf("\n%+v", err))
}
Expand Down
16 changes: 16 additions & 0 deletions web/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func globalNotFoundHandler(c echo.Context) error {
})
}

//nolint:funlen
func getDefaultErrorHandler() echo.HTTPErrorHandler {
var log = logger.Named("http.err").
WithOptions(zap.AddStacktrace(zapcore.PanicLevel), zap.WithCaller(false))
Expand All @@ -49,6 +50,7 @@ func getDefaultErrorHandler() echo.HTTPErrorHandler {
_ = c.JSON(e.Code, res.Error{
Title: http.StatusText(e.Code),
Description: e.Msg,
RequestID: c.Request().Header.Get(cf.HeaderRequestID),
Details: util.Detail(c),
})
return
Expand All @@ -69,13 +71,27 @@ func getDefaultErrorHandler() echo.HTTPErrorHandler {
_ = c.JSON(http.StatusInternalServerError, res.Error{
Title: http.StatusText(e.Code),
Description: e.Error(),
RequestID: c.Request().Header.Get(cf.HeaderRequestID),
Details: util.DetailWithErr(c, err),
})
return
}
}

if errors.Is(err, context.Canceled) {
log.Error("unexpected echo error",
zap.Int("code", http.StatusInternalServerError),
zap.Any("message", "request timeout"),
zap.String("path", c.Request().URL.Path),
zap.String("query", c.Request().URL.RawQuery),
zap.String("cf-ray", c.Request().Header.Get(cf.HeaderRequestID)),
)

_ = c.JSON(http.StatusInternalServerError, res.Error{
Title: "request timeout",
Description: "request timeout",
RequestID: c.Request().Header.Get(cf.HeaderRequestID),
})
return
}

Expand Down
21 changes: 15 additions & 6 deletions web/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -99,12 +100,20 @@ func New() *echo.Echo {

app.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.SetRequest(c.Request().
WithContext(context.WithValue(c.Request().Context(), logger.RequestKey, &logger.RequestTrace{
IP: c.RealIP(),
ReqID: c.Request().Header.Get(cf.HeaderRequestID),
Path: c.Request().RequestURI,
})))
ctx, cancel := context.WithTimeout(context.WithoutCancel(c.Request().Context()), time.Minute)
defer cancel()

reqID := c.Request().Header.Get(cf.HeaderRequestID)

if reqID == "" {
reqID = uuid.Must(uuid.NewV7()).String()
}

c.SetRequest(c.Request().WithContext(context.WithValue(ctx, logger.RequestKey, &logger.RequestTrace{
IP: c.RealIP(),
ReqID: reqID,
Path: c.Request().RequestURI,
})))

return next(c)
}
Expand Down
3 changes: 3 additions & 0 deletions web/res/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/labstack/echo/v4"

"github.com/bangumi/server/web/req/cf"
"github.com/bangumi/server/web/util"
)

Expand All @@ -31,6 +32,7 @@ var ErrNotFound = NewError(http.StatusNotFound, "resource can't be found in the
type Error struct {
Title string `json:"title"`
Details any `json:"details,omitempty"`
RequestID string `json:"request_id,omitempty"`
Description string `json:"description"`
}

Expand Down Expand Up @@ -77,6 +79,7 @@ func InternalError(c echo.Context, err error, message string) error {
return c.JSON(http.StatusInternalServerError, Error{
Title: "Internal Server Error",
Description: message,
RequestID: c.Request().Header.Get(cf.HeaderRequestID),
Details: util.DetailWithErr(c, err),
})
}
Expand Down

0 comments on commit 78693d3

Please sign in to comment.