Skip to content

connection pool size  #2439

Closed
Closed
@mshariat7

Description

@mshariat7

Hi
I am using redis.Client in my project with this configs.
poolsize: 10
minIdleConn: 5
maxRetries: 5

the used command is LPUSH for a list of 2000 elements.

var dialCount uint64
client := redis.NewClient(&redis.Options{
	Addr:       "localhost:6379",
	DB:         0,
	MaxRetries: 5,
	Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
		fmt.Printf("%d - dialer called for new connection\n", atomic.AddUint64(&dialCount, 1))
		return net.Dial(network, addr)
	},
	PoolSize:     50,
	MinIdleConns: 10,
})

ctx := context.Background()
var items []int
for i := 1; i < 2000; i++ {
	items = append(items, i)
}
pipe := client.Pipeline()
defer func(pipe redis.Pipeliner) {
	err := pipe.Close()
	if err != nil {
		fmt.Println("error in closing pipe")
	}
}(pipe)

key := "test_key"
for _, item := range items {
	cmd := pipe.LPush(ctx, key, item)
	if cmd.Err() != nil {
		fmt.Printf("error in list push is: %v\n", cmd.Err())
		return
	}
}
_, err := pipe.Expire(ctx, key, time.Duration(72)*time.Hour).Result()
if err != nil {
	fmt.Printf("error in expiring pipeline is %v\n", err)
	return
}
_, err = pipe.Exec(ctx)
if err != nil {
	fmt.Printf("error in executing pipe is %v\n", err)
	return
}

signal := make(chan bool)
wg := sync.WaitGroup{}

for i := 0; i < 20000; i++ {
	wg.Add(1)
	go func(ctx context.Context, wg *sync.WaitGroup, n int) {
		defer wg.Done()
		<-signal
		cmd := client.RPop(ctx, key)

		if cmd.Err() != nil {
			if cmd.Err().Error() != "redis: connection pool timeout" && cmd.Err() != redis.Nil {
				fmt.Printf("%d - error: %v\n", n, cmd.Err())
			}
		} else {
			fmt.Printf("%d - value: %v\n", n, cmd.Val())
		}
	}(ctx, &wg, i)
}
close(signal)
wg.Wait()

Expected Behavior

the dial function should be called for 50 times, which is the size of poolSize.

Current Behavior

I see more than 50 connections opens. why client opens connections more than poolSize?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions