go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

Strange behavior when new connect with `MinIdleConns`

Open zzjin opened this issue 3 years ago • 0 comments

As document said:

// Minimum number of idle connections which is useful when establishing
// new connection is slow.
MinIdleConns int

Say set MinIdleConns something bigger like 4 * NumCPU, go redis will instant connect redis server 4xCPU connections and mark them all idle. code here

When in common use, (start one web server) all things goes right. But when using for tests(or other parallel binary tasks) which default behavior is run each package in parallel, redis server will recive many many (4xCPUxpackage test file number) new connections.

One possible solution is set low idle connections in test files, but still wonder in real case we also no not need such new connections at first start. If we have slow redis connections this works worse.

Expected Behavior

Just connect small piece of new connections or provide another options to set such as StartConns int.

Current Behavior

Connect server as mush as idle connections options set..

Possible Solution

New 0 conn (or StartConns option) when new conn called, and inscrease double conns len when needed, and at end reach MinIdleConns or PoolSize

Possible Implementation

minPoolIncrease := p.poolSize*2
minIdleIncrease := p.idleConnsLen * 2
for p.poolSize < p.opt.PoolSize && p.idleConnsLen < p.opt.MinIdleConns && p.poolSize < minPoolIncrease && p.idleConnsLen < minIdleIncrease {
    ...
}

zzjin avatar Mar 04 '21 15:03 zzjin