Skip to content

Commit

Permalink
fix review part5
Browse files Browse the repository at this point in the history
  • Loading branch information
Пешков Дмитрий committed Sep 20, 2024
1 parent 93a3881 commit 86fc990
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 32 deletions.
9 changes: 5 additions & 4 deletions internal/client/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ 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)
if err != nil {
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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/add_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/add_withdrawals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/get_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/get_withdrawals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/services/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions internal/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
45 changes: 26 additions & 19 deletions internal/workers/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}
}

Expand All @@ -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
}

0 comments on commit 86fc990

Please sign in to comment.