[go redigo]maxactive/wait可能无法工作,仍在创建数千个连接。
go版本:1.14 github.com/gomodule/redigo v2.0.0+不兼容
第一步:编写如下代码,wait为true,maxactive为99。步骤2:在k8s中部署容器。进行负载测试/步骤3:使用 netstat -nat | grep 6379 | wc -l
,您可以找到每个pod的连接都超过maxactive。
# netstat -nat | grep 6379 | wc -l
xxbp-blacklist-6c9fcb57f4-66jpw
2897
xxbp-blacklist-6c9fcb57f4-6pdrp
12853
xxbp-blacklist-6c9fcb57f4-9kpjs
252
xxbp-blacklist-6c9fcb57f4-hkwj7
165
第四步,在aws-elasticache-redis服务器监控工具中,可以发现连接总数达到10000个。
代码--redisutil.go--
package redisutil
import (
"AuthService/config"
"context"
"fmt"
"github.com/gomodule/redigo/redis"
"log"
"time"
)
var pool *redis.Pool
func InitializeRedisPool() error {
conf := config.GetConfig()
conf.AutomaticEnv()
pool = &redis.Pool{
MaxIdle: 10,
MaxActive: 99,
Wait: true,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp",
conf.GetString("redis_host"),
redis.DialPassword(conf.GetString("redis_token")),
redis.DialTLSSkipVerify(conf.GetBool("redis_tlsskipverify")),
redis.DialUseTLS(conf.GetBool("redis_usetls")))
if err != nil {
log.Fatalln("Redis error during connect! " + conf.GetString("redis_host") +
" redis_token" + conf.GetString("redis_token") +
" redis_tlsskipverify:" + conf.GetString("redis_tlsskipverify") +
" redis_usetls:" + conf.GetString("redis_usetls"))
log.Fatalln(err)
return nil, err
} else {
return c, nil
}
},
}
//Check whether conn can be set up correctly
conn, err := pool.GetContext(context.Background())
if err != nil {
return err
}
defer conn.Close()
return nil
}
func VerifyBlacklist(token string) bool {
conn := pool.Get()
defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", "blacklist:"+token))
if err != nil {
// handle error return from c.Do or type conversion error.
log.Println("Redis error: error return from c.Do or type conversion error.")
log.Println(err)
return false //Use this temporally, friend for testing
}
return exists
}
--代码main.go---
err := redisutil.InitializeRedisPool()
if err != nil {
log.Print("Redis error! InitializeRedisPool Fail!")
}
--api将在负载测试期间调用数千个redisutil.verifyblacklist---
暂无答案!
目前还没有任何答案,快来回答吧!