Using goroutines to create multiple kind-box

This commit is contained in:
Abolfazl Nourzad 2024-01-23 14:45:36 +03:30
parent befdad0c50
commit a18da64cf9
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
1 changed files with 50 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package mysqlkindbox
import ( import (
"context" "context"
"log"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" 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) { 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() tx, tErr := d.conn.Conn().Begin()
if tErr != nil { if tErr != nil {
return nil, richerror.New(op).WithErr(tErr). return nil, richerror.New(op).WithErr(tErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
} }
for _, kindBox := range kindBoxes { for _, kindBox := range kindBoxes {
res, err := tx. go func(kb entity.KindBox) {
ExecContext(ctx, res, err := tx.ExecContext(ctx,
"insert into kind_boxes (kind_box_req_id , benefactor_id , type ,serial_number , status) values (? , ? , ? , ? ,?);", "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(), kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(),
) )
if err != nil { if err != nil {
if rErr := tx.Rollback(); rErr != nil { errCh <- richerror.New(op).WithErr(err).
return nil, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil, richerror.New(op).WithErr(err). return
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
} }
//nolint //nolint
// err is always nil // err is always nil
id, _ := res.LastInsertId() id, _ := res.LastInsertId()
kindBox.ID = uint(id) kb.ID = uint(id)
resultCh <- kb
errCh <- nil
}(kindBox)
} }
if cErr := tx.Commit(); cErr != nil {
return nil, richerror.New(op).WithErr(cErr). 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) WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
} }
return kindBoxes, nil }
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 result, nil
} }