panic:同步:负的WaitGroup计数器,对同一端点有多个请求 [英] panic: sync: negative WaitGroup counter with multiple requests to the same Endpoint

查看:177
本文介绍了panic:同步:负的WaitGroup计数器,对同一端点有多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个哈希密码生成器,我在服务器上有一个端点,该端点生成一个goroutine来创建哈希密码,然后在goroutine结束时将其作为响应发送.

Im doing a hashed password generator, i have an endpoint at the server that generates a goroutine to create the hashed password, and then send it as a response when the goroutine ends.

这是用来生成哈希密码的函数

this is the function that is called to generate the hashed password

func SetHash(c echo.Context) error {
    hashedhPassword := make(chan string)
    var wg sync.WaitGroup
    wg.Add(2)
    go utils.GenerateSaltedHashAsync("demoprueba", wg, hashedhPassword)
    return response.Success(c, map[string]string{
        "salt": <-hashedhPassword,
    })
}

这是哈希类

package utils

import (
    "encoding/base64"
    "fmt"
    "golang.org/x/crypto/argon2"
    "sync"
    "tigoApi/config"
)

const (
    Time         = 4
    Memory       = 64 * 1024
    KeyLen       = 80
    Threads      = 10
)

var environment = config.Instance()

func GenerateSaltedHashAsync(password string,wg sync.WaitGroup, hashedPassword chan string) {
    cryptoKey := argon2.IDKey([]byte(password), []byte(environment.SaltedPassword), Time, Memory, uint8(Threads), KeyLen)
    encodedPassword := base64.StdEncoding.EncodeToString(cryptoKey)
    consoleHash := fmt.Sprintf("%s$%d$%d$%d$%d$%s$%s", environment.PepperPassword, Time, Memory, Threads, KeyLen, environment.SaltedPassword, encodedPassword)
    defer wg.Done()
    hashedPassword <- consoleHash
    wg.Wait()
}

当我执行单个请求时,一切正常,但是当我一次发送多个请求(压力测试)时,应用会发送此错误.

everything works fine when i do a single request, however when i send multiple requests at once(stress test) the app send this error.

紧急:同步:负面的WaitGroup计数器

panic: sync: negative WaitGroup counter

goroutine 1566 [running]:sync.(* WaitGroup).Add(0xc0001320a0,0xffffffffffffffffff)/usr/local/go/src/sync/waitgroup.go:74 + 0x139 sync.(* WaitGroup).Done(0xc0001320a0)/usr/local/go/src/sync/waitgroup.go:99 + 0x34 tigoApi/utils.GenerateSaltedHashAsync(0x8e5324、0xa,0x0、0x2,0xc000226240)/home/crdzbird/goApps/src/tigoApi/utils/hashing.go:46 + 0x3cc,由tigoApi/controller.SetHash创建/home/crdzbird/goApps/src/tigoApi/controller/user_controller.go:23+ 0xcd

goroutine 1566 [running]: sync.(*WaitGroup).Add(0xc0001320a0, 0xffffffffffffffff) /usr/local/go/src/sync/waitgroup.go:74 +0x139 sync.(*WaitGroup).Done(0xc0001320a0) /usr/local/go/src/sync/waitgroup.go:99 +0x34 tigoApi/utils.GenerateSaltedHashAsync(0x8e5324, 0xa, 0x0, 0x2, 0xc000226240) /home/crdzbird/goApps/src/tigoApi/utils/hashing.go:46 +0x3cc created by tigoApi/controller.SetHash /home/crdzbird/goApps/src/tigoApi/controller/user_controller.go:23 +0xcd

以退出代码2完成的过程

Process finished with exit code 2

请任何人都可以告诉我代码有什么问题.

please anyone can tell me what is wrong with the code.

更新.

由于建议代码工作应该像这样...

Thanks to the suggestions the code working should be like this...

func SetHash(c echo.Context) error {
    hashedhPassword := make(chan string)
    go utils.GenerateSaltedHashAsync("lacb2208", hashedhPassword)
    return response.Success(c, map[string]string{
        "salt": <-hashedhPassword,
    })
}


func GenerateSaltedHashAsync(password string, hashedPassword chan string) {
    cryptoKey := argon2.IDKey([]byte(password), []byte(environment.SaltedPassword), Time, Memory, uint8(Threads), KeyLen)
    encodedPassword := base64.StdEncoding.EncodeToString(cryptoKey)
    consoleHash := fmt.Sprintf("%s$%d$%d$%d$%d$%s$%s", environment.PepperPassword, Time, Memory, Threads, KeyLen, environment.SaltedPassword, encodedPassword)
    hashedPassword <- consoleHash
    close(hashedPassword)
}

推荐答案

来自 sync

包含在此(同步)包中定义的类型的值不应被复制.

Values containing the types defined in this (sync) package should not be copied.

因此,如果需要引用,请使用指针.

So if something needs a reference use a pointer.

更改函数签名,以便 wg 是指针引用:

Change your function signature so wg is a pointer reference:

func GenerateSaltedHashAsync(password string,wg *sync.WaitGroup, hashedPassword chan string)

然后使用该引用进行呼叫:

and then call with that reference:

go utils.GenerateSaltedHashAsync("demoprueba", &wg, hashedhPassword)

这篇关于panic:同步:负的WaitGroup计数器,对同一端点有多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆