限制读写线程最小值为4,最大值为32

This commit is contained in:
刘祥超
2024-04-29 22:51:51 +08:00
parent 973324ae8f
commit fa02713ab5

View File

@@ -7,8 +7,9 @@ import (
"time" "time"
) )
var WriterLimiter = NewLimiter(runtime.NumCPU()) var maxThreads = runtime.NumCPU()
var ReaderLimiter = NewLimiter(runtime.NumCPU()) var WriterLimiter = NewLimiter(maxThreads)
var ReaderLimiter = NewLimiter(maxThreads)
type Limiter struct { type Limiter struct {
threads chan struct{} threads chan struct{}
@@ -16,6 +17,13 @@ type Limiter struct {
} }
func NewLimiter(threads int) *Limiter { func NewLimiter(threads int) *Limiter {
if threads < 4 {
threads = 4
}
if threads > 32 {
threads = 32
}
var threadsChan = make(chan struct{}, threads) var threadsChan = make(chan struct{}, threads)
for i := 0; i < threads; i++ { for i := 0; i < threads; i++ {
threadsChan <- struct{}{} threadsChan <- struct{}{}