From a18da64cf921019e4ce6bb773d07975a4ae9ea53 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Tue, 23 Jan 2024 14:45:36 +0330 Subject: [PATCH] Using goroutines to create multiple kind-box --- repository/mysql/kind_box/kind_box.go | 67 ++++++++++++++++++++------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index bb1de97..4ccfb87 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,6 +2,7 @@ package mysqlkindbox import ( "context" + "log" "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -9,36 +10,68 @@ import ( ) func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) { - const op = "mysqlkindbox.AddBatchKindBox" + const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" + errCh := make(chan error, len(kindBoxes)) + resultCh := make(chan entity.KindBox, len(kindBoxes)) tx, tErr := d.conn.Conn().Begin() if tErr != nil { return nil, richerror.New(op).WithErr(tErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } + for _, kindBox := range kindBoxes { - res, err := tx. - ExecContext(ctx, - "insert into kind_boxes (kind_box_req_id , benefactor_id , type ,serial_number , status) values (? , ? , ? , ? ,?);", - kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.Type.String(), kindBox.SerialNumber, kindBox.Status.String(), + go func(kb entity.KindBox) { + res, err := tx.ExecContext(ctx, + "insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);", + kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(), ) - if err != nil { - if rErr := tx.Rollback(); rErr != nil { - return nil, richerror.New(op).WithErr(rErr). + if err != nil { + errCh <- richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + + return } - return nil, richerror.New(op).WithErr(err). + //nolint + // err is always nil + id, _ := res.LastInsertId() + kb.ID = uint(id) + + resultCh <- kb + errCh <- nil + }(kindBox) + } + + var result []entity.KindBox + + for i := 0; i < len(kindBoxes); i++ { + select { + case err := <-errCh: + if err != nil { + if err := tx.Rollback(); err != nil { + log.Printf("Rollback error: %v", err) + } + + return nil, err + } + case res := <-resultCh: + result = append(result, res) + case <-ctx.Done(): + if err := tx.Rollback(); err != nil { + log.Printf("Rollback error: %v", err) + } + + return nil, richerror.New(op).WithErr(ctx.Err()). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - - //nolint - // err is always nil - id, _ := res.LastInsertId() - kindBox.ID = uint(id) } - if cErr := tx.Commit(); cErr != nil { - return nil, richerror.New(op).WithErr(cErr). + + if err := tx.Commit(); err != nil { + log.Printf("Commit error: %v", err) + + return nil, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - return kindBoxes, nil + + return result, nil }