Skip to content

Commit

Permalink
fix: io read timeout on create item creds endpoint (#2599)
Browse files Browse the repository at this point in the history
Use io.ReadAll instead of requestutils.ReadJSON and increase the read timeout for grants server.
  • Loading branch information
clD11 authored Jul 17, 2024
1 parent 9db3baa commit 1eda1a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion services/grant/cmd/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func GrantServer(
srv := http.Server{
Addr: ":3333",
Handler: chi.ServerBaseContext(ctx, r),
ReadTimeout: 3 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 20 * time.Second,
}
err = srv.ListenAndServe()
Expand Down
16 changes: 10 additions & 6 deletions services/skus/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Router(

// For now, this endpoint is placed directly under /credentials.
// It would make sense to put it under /items/item_id, had the caller known the item id.
// However, the caller of this endpoit does not posses that knowledge, and it would have to call the order endpoint to get it.
// However, the caller of this endpoint does not possess that knowledge, and it would have to call the order endpoint to get it.
// This extra round-trip currently does not make sense.
// So until Bundles came along we can benefit from the fact that there is one item per order.
// By the time Bundles arrive, the caller would either have to fetch order anyway, or this can be communicated in another way.
Expand Down Expand Up @@ -623,15 +623,19 @@ type createItemCredsRequest struct {
// createItemCreds handles requests for creating credentials for an item.
func createItemCreds(svc *Service) handlers.AppHandler {
return func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
ctx := r.Context()
lg := logging.Logger(ctx, "skus.createItemCreds")
b, err := io.ReadAll(io.LimitReader(r.Body, reqBodyLimit10MB))
if err != nil {
return handlers.WrapError(err, "error reading body", http.StatusBadRequest)
}

req := &createItemCredsRequest{}
if err := requestutils.ReadJSON(ctx, r.Body, req); err != nil {
lg.Error().Err(err).Msg("failed to read body payload")
return handlers.WrapError(err, "Error in request body", http.StatusBadRequest)
if err := json.Unmarshal(b, req); err != nil {
return handlers.WrapError(err, "error decoding body", http.StatusBadRequest)
}

ctx := r.Context()
lg := logging.Logger(ctx, "skus.createItemCreds")

if _, err := govalidator.ValidateStruct(req); err != nil {
lg.Error().Err(err).Msg("failed to validate struct")
return handlers.WrapValidationError(err)
Expand Down

0 comments on commit 1eda1a0

Please sign in to comment.