New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set workerPool.MaxIdleWorkerDuration? #1178
Comments
You're right, it's not possible. What would be your use case? Would you like to set it lower or higher and for what reason? |
The purpose of workerPool is to reduce the overhead caused by the frequent creation and destruction of goroutine. If worker goroutine expiration time can be set, users can determine how long a goroutine expires based on their own usage scenarios. For example, if there is a rush of traffic every ten seconds or a minute, the workerPool will lose its meaning because the goroutines created in the previous wave are destroyed before the next wave arrives (although fasthttp still outperforms std http lib in this case). In this case, I would probably prefer to set the expiration time of the worker goroutine slightly longer to reduce the cost of creating and destroying the goroutine. |
Have you tried making a change to fasthttp to increase the timeout? Since goroutine creating is very cheap in Go I wonder if it would have any measurably effect. If you can show there is a measurable effect I can expose the API so you can change it. |
I'm sorry I couldn't figure out how to test the difference between unrestricted creation and destruction of goroutine and changing the expiration time of workerPool, I was inspired by panjf2000/ants and thought it might be good to allow the user to set the expiration time of worker goroutine.😢 Sorry again. |
No need to apologize. I have pushed a test branch which adds You can use the branch by running go mod edit -replace github.com/valyala/fasthttp=github.com/valyala/fasthttp@a396af6384f8f09c9b052097528040189cdf326b
go mod tidy (and After that you should be able to set |
Hi, thank you for your branch code, I didn't use fasthttp directly in my project, I'm using gofiber/fiber which is based on fasthttp, and after I replace the dependency of fasthttp, I have run 5 times of stress test on my API endpoint, there are the codes of stress test: func main() {
reg := regexp.MustCompile(`Requests/sec: + \d+.\d+`)
const times = 12
// const times = 50
var res [times]float64
for i := 0; i < times; i++ {
cmd := exec.Command("zsh", "-c", "./codes/wrk.sh")
out, err := cmd.Output()
if err != nil {
log.Fatal(string(out), err)
}
fmt.Println(string(out))
num := string(reg.Find(out))
req := strings.ReplaceAll(num[13:], " ", "")
res[i], _ = strconv.ParseFloat(req, 64)
if i < times-1 {
time.Sleep(time.Second * 10) // Sleep to let the workerPool clean the goroutines
}
}
sort.Float64s(res[:])
var totalSpeed float64
for i := 1; i < times-1; i++ {
totalSpeed += res[i]
}
fmt.Printf("Max TPS: %.2f\n", res[times-1])
fmt.Printf("Min TPS: %.2f\n", res[0])
fmt.Printf("Avg TPS: %.2f\n", totalSpeed/(times-2))
} It will use
|
The value in the table is the Avg TPS(Transaction/Request per second) on my HTTP endpoint, sorry. |
There is too much variance in those results. What I'm wondering now is if we should completely get rid of the worker pool instead. When I have time I'm going to run some benchmarks on that instead. |
Due to the system limit, I could only run the main func once at the same time, the 10 results are run in the following order: |
Removing the worker pool completely is slower. So I guess we should add |
I'd be happy to do that |
Fixed by #1183 |
I have found usage of workerPool.MaxIdleWorkerDuration in GoLand, but only find it was used in workerPool.getMaxIdleWorkerDuration(), and there is no other way to set workerPool.MaxIdleWorkerDuration, so workerPool.getMaxIdleWorkerDuration() always returns 10 * time.Second. How could I set the workerPool.MaxIdleWorkerDuration to a bigger value so that the worker goroutine can live longer?
The text was updated successfully, but these errors were encountered: