草庐IT

Golang WaitGroup.Done() 被跳过

coder 2023-07-03 原文

我有一个依赖于并发检查某些错误的函数,我正在尝试使用 WaitGroup 等待所有返回可能错误的进程完成,然后再检查所有错误。

它似乎跳过了一些 wg.Done() cals。这是调试的 youtube 视频(抱歉,它循环“for”循环 3 次): Golang Delve Debug for WaitGroups

知道为什么它会跳过一些 waitgroup.Done() 调用吗?

代码如下:

package controllers

import (
    "errors"
    "mobilebid/billable"
    db "mobilebid/database"
    "mobilebid/stripe"
    "net/http"
    "os"
    "strconv"
    "sync"
    "time"

    log "github.com/Sirupsen/logrus"
    "github.com/gorilla/mux"
)

var (
    errBillableID     = errors.New("It looks like there was an error while getting your billable ID. Do you have a credit card set up?")
    errWinningItems   = errors.New("It looks like there was an error while gathering your winning items. Please contact an event rep.")
    errAcctInfo       = errors.New("We had some trouble getting the account information for the event. Please contact an event rep.")
    errLoggingTrans   = errors.New("It looks like we had some sort of issue while logging your transaction. Please contact an event rep.")
    errParsingURL     = errors.New("We had some issue looking at the URL.")
    errStripeIssue    = errors.New("It looks like there was some kind of issue while talking with Stripe. If you were in the middle of a transaction, this doesn't mean the transaction was cancelled. Take a look at your transactions and/or contact an event rep.")
    errItemsPurchased = errors.New("One or more of the items you're trying to purchase have already been purchased. If this doesn't sound right, please contact an event rep.")
)

func createLogCtx(bidderID, eventID int) *log.Entry {
    return log.WithFields(log.Fields{
        "bidderID": bidderID,
        "eventID":  eventID,
    })
}

var wg sync.WaitGroup

const gorutineCt = 6

//PurchaseItems purchases items from the event for the bidder and sends the funds to the customer
//  In order for PurchaseItems to work:
//      1. Bidder must have a customer account set up in Stripe
//      2. Event owner needs to have their Stripe registered with the apps Stripe account
//      3. Item must not have been purchased before (ever)
func PurchaseItems(dB db.AppDB) http.HandlerFunc {
    return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {

        ps := mux.Vars(req)

        eventID, err := strconv.Atoi(ps["eventID"])
        if err != nil {
            log.Error(err.Error())
            res.Write(ResErr(errParsingURL.Error()))
            return
        }

        bidderID, err := strconv.Atoi(ps["bidderID"])
        if err != nil {
            log.Error(err.Error())
            res.Write(ResErr(errParsingURL.Error()))
            return
        }

        itemsChan := make(chan []db.ItemWon)
        billableBidderIDChan := make(chan string)
        creditableAcctChan := make(chan string)
        errsChan := make(chan error, gorutineCt)
        wg.Add(gorutineCt)

        logCtx := createLogCtx(bidderID, eventID)

        acct := stripe.New(os.Getenv("SECRET_KEY"), os.Getenv("PUBLISHABLE_KEY"))

        go func() {
            id, e := dB.GetBidderBillableID(bidderID)
            if e != nil {
                logCtx.Error(e.Error())
                errsChan <- errBillableID
                billableBidderIDChan <- id
            } else {
                errsChan <- nil
                billableBidderIDChan <- id
            }
            wg.Done()
        }()

        go func() {
            i, e := dB.GetWinningItemsForBidder(bidderID, eventID)
            if e != nil {
                logCtx.Error(e.Error())
                errsChan <- errWinningItems
                itemsChan <- i
            } else {
                errsChan <- nil
                itemsChan <- i
            }
            wg.Done()
        }()

        go func() {
            a, e := dB.GetCreditableAccountFromEvent(eventID)
            if e != nil {
                logCtx.Error(e.Error())
                errsChan <- errAcctInfo
                creditableAcctChan <- a
            } else {
                errsChan <- nil
                creditableAcctChan <- a
            }
            wg.Done()
        }()

        go func() {
            items := <-itemsChan
            for _, val := range items {
                e := dB.CheckIfItemPurchased(val.ItemID)
                if e != nil {
                    logCtx.WithFields(log.Fields{
                        "itemID":     val.ItemID,
                        "_timestamp": time.Now(),
                    }).Error(e.Error())
                    errsChan <- errItemsPurchased
                    itemsChan <- items
                    wg.Done()
                    return
                }
            }
            errsChan <- nil
            itemsChan <- items
            wg.Done() //SKIPPED
        }()

        go func() {
            billableBidderID := <-billableBidderIDChan
            e := acct.BuyerIsBillable(billableBidderID)
            if e != nil {
                logCtx.Error(e.Error())
                errsChan <- errStripeIssue
                billableBidderIDChan <- billableBidderID
            } else {
                errsChan <- nil
                billableBidderIDChan <- billableBidderID
            }
            wg.Done()
        }()

        go func() {
            creditableAcct := <-creditableAcctChan
            e := acct.CanReceiveFunds(creditableAcct)
            if e != nil {
                logCtx.Error(e.Error())
                errsChan <- errStripeIssue
                creditableAcctChan <- creditableAcct
            } else {
                errsChan <- nil
                creditableAcctChan <- creditableAcct
            }
            wg.Done()
        }()

        wg.Wait()
        close(errsChan)

        if err = checkConcurrentErrs(errsChan); err != nil {
            logCtx.Error(err.Error())
            res.Write(ResErr(err.Error()))
            return
        }

        items := <-itemsChan
        amount := addItems(items)
        appFee := calculateFee(amount, .03) //TODO: Store this somewhere where it can be edited without having to restart the app.

        invoice := billable.BillObject{
            Desc:     "Test Charge", //TODO: Generate this description from the event, items and bidder somehow.
            Amount:   amount,
            Currency: "usd",
            Dest:     <-creditableAcctChan,
            Fee:      appFee,
            Meta:     createItemsList(items),
            Customer: <-billableBidderIDChan,
        }

        trans, err := acct.ChargeBidder(invoice)
        if err != nil {
            logCtx.Error(err.Error())
            res.Write(ResErr(errStripeIssue.Error()))
            return
        }

        logCtx.WithFields(log.Fields{
            "stripeTransID": trans.TransID,
            "itemcCount":    len(items),
        }).Info("Transferred funds from bidder to client")

        dbTrans := db.Transaction{
            TransID:  trans.TransID,
            UserID:   5,
            BidderID: bidderID,
            EventID:  eventID,
            Amount:   int64(amount),
            AppFee:   int64(appFee),
            Desc:     "Some test order",
            Status:   "completed",
        }

        orderID, err := dB.InsertTransaction(dbTrans)
        if err != nil {
            logCtx.WithFields(log.Fields{
                "stripeTransID": dbTrans.TransID,
                "_timestamp":    time.Now(),
            }).Error(err.Error())
            res.Write(ResErr(errLoggingTrans.Error()))
            return
        }

        for it, val := range items {
            i := db.TransactionLine{
                OrderID: orderID,
                ItemID:  val.ItemID,
                Amount:  uint64(val.Bid * 100), //Must do this since the bid is in dollars but the amount is pennies
                Line:    it,
            }

            err := dB.InsertTransactionLine(i)
            if err != nil {
                logCtx.WithFields(log.Fields{
                    "stripeTransID": dbTrans.TransID,
                    "lineNumber":    i,
                    "_timestamp":    time.Now(),
                }).Error(err.Error())
                res.Write(ResErr(errLoggingTrans.Error()))
                return
            }
        }

        logCtx.WithField("orderID", orderID).Info("Order created")

        //TODO: Send receipt to buyer.
        res.Write(ResOK(trans.TransID))

    })
}

最佳答案

为了后代(和谷歌搜索):

在每个 go func() 行之前放置 wg.Add(1),而不是使用 wg.Add(gorutineCt)

defer wg.Done() 放在每个 goroutine enclosure 的开头,而不是在每个退出情况下调用 wg.Done()。这确保 wg.Done() 无论如何都能运行。

使用更接近的例程而不是尝试充分缓冲 channel :

// start other goroutines

go func () {
    wg.Wait()
    close(errschan)
}

for _, err := range errsChan { // automatically terminates once chan is closed
    if err != nil {
         // handle err
    }
}

关于Golang WaitGroup.Done() 被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948574/

有关Golang WaitGroup.Done() 被跳过的更多相关文章

  1. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  2. Ruby - 如何在读取文件时跳过/忽略特定行? - 2

    在读取/解析文件(使用Ruby)时忽略某些行的最佳方法是什么?我正在尝试仅解析Cucumber.feature文件中的场景,并希望跳过不以Scenario/Given/When/Then/And/But开头的行。下面的代码有效,但它很荒谬,所以我正在寻找一个聪明的解决方案:)File.open(file).each_linedo|line|line.chomp!nextifline.empty?nextifline.include?"#"nextifline.include?"Feature"nextifline.include?"Inorder"nextifline.include?

  3. ruby - 如何跳过 CSV 文件的第一行并将第二行作为标题 - 2

    有没有办法跳过CSV文件的第一行,让第二行作为标题?我有一个CSV文件,第一行是日期,第二行是标题,所以我需要能够在遍历它时跳过第一行。我尝试使用slice但它会将CSV转换为数组,我真的很想将其读取为CSV,以便我可以利用header。 最佳答案 根据您的数据,您可以使用另一种方法和skip_lines-option此示例跳过所有以#开头的行require'csv'CSV.parse(DATA.read,:col_sep=>';',:headers=>true,:skip_lines=>/^#/#Markcomments!)do|

  4. ruby - 在 Ruby 中跳过额外的关键字参数 - 2

    我定义了一个方法:defmethod(one:1,two:2)[one,two]end当我这样调用它时:methodone:'one',three:'three'我得到:ArgumentError:unknownkeyword:three我不想从散列中一个一个地提取所需的键或排除额外的键。除了像这样定义方法之外,有没有办法规避这种行为:defmethod(one:1,two:2,**other)[one,two,other]end 最佳答案 如果不想写**other中的other,可以省略。defmethod(one:1,two:2

  5. ruby-on-rails - 我如何跳过前三行而不是 FasterCSV 中的第一行 - 2

    我正在使用FasterCSV我正在循环使用这样的foreachFasterCSV.foreach("#{Rails.public_path}/uploads/transfer.csv",:encoding=>'u',:headers=>:first_row)do|row|但问题是我的csv将前3行作为标题...有什么方法可以使fasterCSV跳过前三行而不是仅跳过第一行?? 最佳答案 不确定FasterCSV,但在Ruby1.9标准CSV库(由FasterCSV制作)中,我可以执行以下操作:c=CSV.open'/path/to/

  6. ruby-on-rails - my_object.save(false) 并没有真正跳过我的 Active Record 验证 - 2

    所以我一直在努力解决我一直遇到的这个错误,我终于找到了导致它的原因。我一直觉得,当我调用@my_model.save(false)我会跳过我的ActiveRecord验证。事实证明这是部分正确的。我的对象正在保存到数据库中DESPITE我的ActiveRecord验证。我的问题存在是因为我的一个验证在验证过程中修改了一个子模型(这是一个24小时位置的调度应用程序,因此当午餐被保存时,我对照他们保存的那天和第二天检查它们以及确保用户不是指“凌晨2点”表示要上夜类。我的问题是:有没有办法真正跳过我的验证并直接移动到数据库?这是正常的ActiveRecord行为还是我应该更深入地研究我的验证

  7. ruby - 如何禁止在 RSpec 中显示挂起(跳过)的规范? - 2

    我有几个跳过的规范。Pending:(Failureslistedhereareexpectedanddonotaffectyoursuite'sstatus)1)...#Notyetimplemented#./spec/requests/request_spec.rb:22如何抑制未决规范的输出? 最佳答案 您可以添加以下配置选项以从运行中过滤掉所有待处理的规范:RSpec.configuredo|config|config.filter_run_excludingskip:trueend此外,here是一个更详细的抑制输出的建议

  8. Win10 / 11新电脑最简单跳过联网激活和使用本地账户登录方法 - 2

    跳过联网激活:OOBE界面直接按Ctrl+Shift+F3进入审核模式。这样就可以直接进入系统进行一些硬件测试等,而不用联网激活导致新机无法退货。需要注意的是,在审核模式下进行的一些操作都会保留,并不会在退出后自动还原!安装的软件在正常开机进系统后还会看见!如果电脑确实没连互联网又不想强行跳过OOBE(网上很多教程会叫你直接结束OOBE进程,但这是不推荐的,因为一些厂商自带优化程序和系统初始化设置在后面都会应用,对于笔记本跳过的话你会发现驱动和内置应用都没有装上。其实这部分脚本就在系统盘的Recovery隐藏文件夹下),可以参考以下方式:https://www.landiannews.com/

  9. ruby-on-rails - Rails 3 - 如何跳过验证规则? - 2

    我有这个验证规则的注册表单:validates:email,:presence=>{:message=>'cannotbeblank.'},:allow_blank=>true,:format=>{:with=>/\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/,:message=>'addressisnotvalid.Please,fixit.'},:uniqueness=>true此规则检查,如果用户填写注册表单电子邮件地址(+其正确格式)。现在我正尝试添加使用Twitter登录的机会。Twitter不提供用户的电子邮件地址。在这种情

  10. ruby-on-rails - 当加载根命名空间中的另一个同名类时,Rails 类加载会跳过命名空间类 - 2

    我有两个命名空间,每个都有自己的Controller和演示器类:成员::DocumentsController成员::DocumentPresenterguest::DocumentsControllerGuest::DocumentPresenter两个演示者都继承自::DocumentPresenter。Controller在没有指定命名空间的情况下访问各自的演示者,例如:classGuest::DocumentsController这通常会在同一个命名空间中调用演示者。但是有时在开发环境中我看到正在使用base::DocumentPresenter。我怀疑原因是base::Doc

随机推荐