From 86fc9904f2eb8b7ce50dde0688627ff1e0861844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D1=88=D0=BA=D0=BE=D0=B2=20=D0=94=D0=BC=D0=B8?= =?UTF-8?q?=D1=82=D1=80=D0=B8=D0=B9?= Date: Fri, 20 Sep 2024 20:38:51 +0300 Subject: [PATCH] fix review part5 --- internal/client/order.go | 9 +++--- internal/handlers/add_orders.go | 3 +- internal/handlers/add_withdrawals.go | 3 +- internal/handlers/get_orders.go | 3 +- internal/handlers/get_withdrawals.go | 3 +- internal/handlers/login.go | 3 +- internal/handlers/register.go | 3 +- internal/services/order.go | 3 +- internal/services/user.go | 6 ++-- internal/workers/order.go | 45 ++++++++++++++++------------ 10 files changed, 49 insertions(+), 32 deletions(-) diff --git a/internal/client/order.go b/internal/client/order.go index bd9fa97..d8e97a4 100644 --- a/internal/client/order.go +++ b/internal/client/order.go @@ -25,6 +25,9 @@ func (c OrderClient) CheckOrder(OrderNum string) (services.OrderFromAccrual, int if err != nil { return data, 0, err } + defer func() { + _ = resp.Body.Close() + }() if resp.StatusCode == http.StatusTooManyRequests { retryAfter := resp.Header.Get("Retry-After") seconds, err := strconv.Atoi(retryAfter) @@ -32,14 +35,12 @@ func (c OrderClient) CheckOrder(OrderNum string) (services.OrderFromAccrual, int seconds = 0 } return data, seconds, errors.New("Error " + resp.Status) - } else if resp.StatusCode != http.StatusOK { + } + if resp.StatusCode != http.StatusOK { return data, 0, errors.New("Error " + resp.Status) } bodyBytes, _ := io.ReadAll(resp.Body) - defer func() { - _ = resp.Body.Close() - }() if err = json.Unmarshal(bodyBytes, &data); err != nil { return data, 0, err } diff --git a/internal/handlers/add_orders.go b/internal/handlers/add_orders.go index 331e4ba..94cf19e 100644 --- a/internal/handlers/add_orders.go +++ b/internal/handlers/add_orders.go @@ -25,7 +25,8 @@ func AddOrdersHandler(o *services.OrderService) http.HandlerFunc { } else if errors.Is(err, services.ErrOrderUserExists) { res.WriteHeader(http.StatusOK) return - } else if err != nil { + } + if err != nil { res.WriteHeader(http.StatusInternalServerError) return } diff --git a/internal/handlers/add_withdrawals.go b/internal/handlers/add_withdrawals.go index c0add23..7e2483d 100644 --- a/internal/handlers/add_withdrawals.go +++ b/internal/handlers/add_withdrawals.go @@ -22,7 +22,8 @@ func AddWithdrawalHandler(w *services.WithdrawalService) http.HandlerFunc { } else if errors.Is(err, services.ErrBalance) { http.Error(res, err.Error(), http.StatusPaymentRequired) return - } else if err != nil { + } + if err != nil { res.WriteHeader(http.StatusInternalServerError) return } diff --git a/internal/handlers/get_orders.go b/internal/handlers/get_orders.go index 646d921..201c5b4 100644 --- a/internal/handlers/get_orders.go +++ b/internal/handlers/get_orders.go @@ -13,7 +13,8 @@ func GetOrdersHandler(o *services.OrderService) http.HandlerFunc { if errors.Is(err, services.ErrOrdersNotFound) { res.WriteHeader(http.StatusNoContent) return - } else if err != nil { + } + if err != nil { http.Error(res, err.Error(), http.StatusInternalServerError) return } diff --git a/internal/handlers/get_withdrawals.go b/internal/handlers/get_withdrawals.go index 16a6c00..a293904 100644 --- a/internal/handlers/get_withdrawals.go +++ b/internal/handlers/get_withdrawals.go @@ -13,7 +13,8 @@ func GetWithdrawalsHandler(w *services.WithdrawalService) http.HandlerFunc { if errors.Is(err, services.ErrWithdrawNotFound) { res.WriteHeader(http.StatusNoContent) return - } else if err != nil { + } + if err != nil { http.Error(res, err.Error(), http.StatusInternalServerError) return } diff --git a/internal/handlers/login.go b/internal/handlers/login.go index dc945c9..36d86c2 100644 --- a/internal/handlers/login.go +++ b/internal/handlers/login.go @@ -20,7 +20,8 @@ func LoginHandler(u *services.UserService, a *authenticate.Authenticate) http.Ha if errors.Is(err, services.ErrLogin) { http.Error(res, err.Error(), http.StatusUnauthorized) return - } else if err != nil { + } + if err != nil { res.WriteHeader(http.StatusInternalServerError) return } diff --git a/internal/handlers/register.go b/internal/handlers/register.go index 122a059..4e719e7 100644 --- a/internal/handlers/register.go +++ b/internal/handlers/register.go @@ -23,7 +23,8 @@ func RegisterHandler(u *services.UserService, a *authenticate.Authenticate) http } else if errors.Is(err, services.ErrLoginExists) { res.WriteHeader(http.StatusConflict) return - } else if err != nil { + } + if err != nil { res.WriteHeader(http.StatusInternalServerError) return } diff --git a/internal/services/order.go b/internal/services/order.go index 29f6801..1585f2b 100644 --- a/internal/services/order.go +++ b/internal/services/order.go @@ -53,7 +53,8 @@ func (o *OrderService) Add(ctx context.Context, orderNum string) error { ON CONFLICT(number) DO UPDATE SET number = EXCLUDED.number RETURNING id, user_id`, orderID, ctx.Value(authenticate.ContextUserID), orderNum).Scan(&baseOrderID, &baseUserID) if err != nil { return err - } else if ctx.Value(authenticate.ContextUserID) != uuid.MustParse(baseUserID) { + } + if ctx.Value(authenticate.ContextUserID) != uuid.MustParse(baseUserID) { return ErrOrderAnotherUserExists } else if orderID != baseOrderID { return ErrOrderUserExists diff --git a/internal/services/user.go b/internal/services/user.go index 4a9db20..1fb3360 100644 --- a/internal/services/user.go +++ b/internal/services/user.go @@ -51,7 +51,8 @@ func (u *UserService) Add(ctx context.Context, input InputDataUser) (uuid.UUID, userID, input.Login, string(bytes)).Scan(&baseUserID) if err != nil { return uuid.Nil, err - } else if userID != baseUserID { + } + if userID != baseUserID { return uuid.Nil, ErrLoginExists } return uuid.MustParse(baseUserID), nil @@ -66,7 +67,8 @@ func (u *UserService) Login(ctx context.Context, input InputDataUser) (uuid.UUID err := row.Scan(&userID, &password) if errors.Is(err, sql.ErrNoRows) { return uuid.Nil, ErrLogin - } else if err != nil { + } + if err != nil { return uuid.Nil, err } diff --git a/internal/workers/order.go b/internal/workers/order.go index 694dd94..d3a305c 100644 --- a/internal/workers/order.go +++ b/internal/workers/order.go @@ -2,6 +2,7 @@ package workers import ( "context" + "fmt" "github.com/spqrcor/gofermart/internal/client" "github.com/spqrcor/gofermart/internal/services" "go.uber.org/zap" @@ -24,7 +25,7 @@ func NewOrderWorker(ctx context.Context, orderService *services.OrderService, or func (w *OrderWorker) Run() { go w.fillQueue() for i := 1; i <= w.workerCount; i++ { - go w.worker() + w.worker() } } @@ -35,29 +36,35 @@ func (w *OrderWorker) fillQueue() { } } -func (w *OrderWorker) worker() { - for { - select { - case <-w.ctx.Done(): - return - case orderNum, ok := <-w.orderQueue: - if !ok { - w.logger.Info("order queue is closed") +func (w *OrderWorker) worker() chan error { + errors := make(chan error) + go func() { + defer close(errors) + for { + select { + case <-w.ctx.Done(): return - } + case orderNum, ok := <-w.orderQueue: + if !ok { + w.logger.Info("order queue is closed") + errors <- fmt.Errorf("order queue is closed") + return + } - result, sleepSeconds, err := w.orderClient.CheckOrder(orderNum) - if err != nil { - w.logger.Info(err.Error()) - } else { - if err := w.orderService.ChangeStatus(w.ctx, result); err != nil { + result, sleepSeconds, err := w.orderClient.CheckOrder(orderNum) + if err != nil { w.logger.Info(err.Error()) + } else { + if err := w.orderService.ChangeStatus(w.ctx, result); err != nil { + w.logger.Info(err.Error()) + } } - } - if sleepSeconds > 0 { - time.Sleep(time.Duration(sleepSeconds) * time.Second) + if sleepSeconds > 0 { + time.Sleep(time.Duration(sleepSeconds) * time.Second) + } } } - } + }() + return errors }