Skip to content

Commit

Permalink
Try to fix cancellation error (#202)
Browse files Browse the repository at this point in the history
* Try to fix cancellation error

* Fix: timeout middleware

* 502 on cancel request
  • Loading branch information
aopoltorzhicky authored May 28, 2024
1 parent 6711cc5 commit 0d9996e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/api/handler/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (handler *BlockHandler) GetEvents(c echo.Context) error {
if handler.block.IsNoRows(err) {
return returnArray(c, []any{})
}
return internalServerError(c, err)
return handleError(c, err, handler.block)
}

fltrs := storage.EventFilter{
Expand Down
13 changes: 10 additions & 3 deletions cmd/api/handler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
errInvalidHashLength = errors.New("invalid hash: should be 32 bytes length")
errInvalidAddress = errors.New("invalid address")
errCancelRequest = "pq: canceling statement due to user request"
)

type NoRows interface {
Expand All @@ -33,9 +34,7 @@ func badRequestError(c echo.Context, err error) error {

func internalServerError(c echo.Context, err error) error {
if hub := sentryecho.GetHubFromContext(c); hub != nil {
if !errors.Is(err, context.Canceled) {
hub.CaptureMessage(err.Error())
}
hub.CaptureMessage(err.Error())
}
return c.JSON(http.StatusInternalServerError, Error{
Message: err.Error(),
Expand All @@ -46,6 +45,14 @@ func handleError(c echo.Context, err error, noRows NoRows) error {
if err == nil {
return nil
}
if err.Error() == errCancelRequest {
return nil
}
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return c.JSON(http.StatusBadGateway, Error{
Message: err.Error(),
})
}
if noRows.IsNoRows(err) {
return c.NoContent(http.StatusNoContent)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (handler *NamespaceHandler) Blob(c echo.Context) error {

response, err := responses.NewBlob(blob)
if err != nil {
return internalServerError(c, err)
return handleError(c, err, handler.blobLogs)
}

return c.JSON(http.StatusOK, response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (handler SearchHandler) Search(c echo.Context) error {
}
if err != nil {
if !handler.address.IsNoRows(err) {
return internalServerError(c, err)
return handleError(c, err, handler.address)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (sh *StateHandler) Head(c echo.Context) error {

votingPower, err := sh.validator.TotalVotingPower(c.Request().Context())
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.state)
}
state.TotalVotingPower = votingPower

Expand Down
20 changes: 10 additions & 10 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (sh StatsHandler) Summary(c echo.Context) error {
if errors.Is(err, storage.ErrValidation) {
return badRequestError(c, err)
}
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

return c.JSON(http.StatusOK, summary)
Expand Down Expand Up @@ -144,7 +144,7 @@ func (sh StatsHandler) Histogram(c echo.Context) error {
})
}
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

response := make([]responses.HistogramItem, len(histogram))
Expand All @@ -168,7 +168,7 @@ func (sh StatsHandler) Histogram(c echo.Context) error {
func (sh StatsHandler) TPS(c echo.Context) error {
tps, err := sh.repo.TPS(c.Request().Context())
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}
return c.JSON(http.StatusOK, responses.NewTPS(tps))
}
Expand All @@ -186,7 +186,7 @@ func (sh StatsHandler) TPS(c echo.Context) error {
func (sh StatsHandler) TxCountHourly24h(c echo.Context) error {
histogram, err := sh.repo.TxCountForLast24h(c.Request().Context())
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}
response := make([]responses.TxCountHistogramItem, len(histogram))
for i := range histogram {
Expand Down Expand Up @@ -222,7 +222,7 @@ func (sh StatsHandler) NamespaceUsage(c echo.Context) error {

namespaces, err := sh.nsRepo.ListWithSort(c.Request().Context(), "size", sdk.SortOrderDesc, *req.Top, 0)
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

var top100Size int64
Expand All @@ -234,7 +234,7 @@ func (sh StatsHandler) NamespaceUsage(c echo.Context) error {

state, err := sh.state.List(c.Request().Context(), 1, 0, sdk.SortOrderAsc)
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}
if len(state) == 0 {
return returnArray(c, response)
Expand Down Expand Up @@ -284,7 +284,7 @@ func (sh StatsHandler) Series(c echo.Context) error {
storage.NewSeriesRequest(req.From, req.To),
)
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

response := make([]responses.SeriesItem, len(histogram))
Expand Down Expand Up @@ -345,7 +345,7 @@ func (sh StatsHandler) NamespaceSeries(c echo.Context) error {
storage.NewSeriesRequest(req.From, req.To),
)
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

response := make([]responses.SeriesItem, len(histogram))
Expand Down Expand Up @@ -419,7 +419,7 @@ func (sh StatsHandler) PriceSeries(c echo.Context) error {
func (sh StatsHandler) PriceCurrent(c echo.Context) error {
price, err := sh.price.Last(c.Request().Context())
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

return c.JSON(http.StatusOK, responses.NewPrice(price))
Expand Down Expand Up @@ -463,7 +463,7 @@ func (sh StatsHandler) StakingSeries(c echo.Context) error {
storage.NewSeriesRequest(req.From, req.To),
)
if err != nil {
return internalServerError(c, err)
return handleError(c, err, sh.nsRepo)
}

response := make([]responses.SeriesItem, len(histogram))
Expand Down
21 changes: 12 additions & 9 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func cacheSkipper(c echo.Context) bool {
func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo {
e := echo.New()
e.Validator = handler.NewCelestiaApiValidator()

e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
Expand Down Expand Up @@ -183,6 +184,17 @@ func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo {
return nil
},
}))

timeout := 30 * time.Second
if cfg.RequestTimeout > 0 {
timeout = time.Duration(cfg.RequestTimeout) * time.Second
}
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Skipper: websocketSkipper,
Timeout: timeout,
ErrorMessage: `{"message":"timeout"}`,
}))

e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: gzipSkipper,
}))
Expand All @@ -201,15 +213,6 @@ func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo {
e.Use(middleware.Secure())
e.Pre(middleware.RemoveTrailingSlash())

timeout := 30 * time.Second
if cfg.RequestTimeout > 0 {
timeout = time.Duration(cfg.RequestTimeout) * time.Second
}
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Skipper: websocketSkipper,
Timeout: timeout,
}))

if cfg.Prometheus {
e.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{
Namespace: "celestia_api",
Expand Down

0 comments on commit 0d9996e

Please sign in to comment.