panic:同步:负的WaitGroup计数器,对同一端点有多个请求 [英] panic: sync: negative WaitGroup counter with multiple requests to the same Endpoint
问题描述
我正在做一个哈希密码生成器,我在服务器上有一个端点,该端点生成一个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)
}