[gogf/gf]官方gredis例子压测后出现不能访问

2024-07-09 324 views
3

环境: go1.14 gf v1.16.4

使用官方redis示例做压测,发现压测后不能正常连接redis,使用原生的redisgo或者go-redis在gf下压测没用问题,以下是压测的步骤和结果:

` [root@centos7 bin]# curl http://10.11.81.174:9999

v

[root@centos7 bin]# ./wrk -c5000 -d10 -t4 -T3 --latency http://10.11.81.174:9999

Running 10s test @ http://10.11.81.174:9999

4 threads and 5000 connections

Thread Stats Avg Stdev Max +/- Stdev

Latency    29.48ms   69.41ms   1.65s    97.25%

Req/Sec    30.30k     8.99k   49.88k    66.92%

Latency Distribution

 50%   19.17ms

 75%   27.67ms

 90%   44.97ms

 99%  201.93ms

1204337 requests in 10.06s, 248.09MB read

Socket errors: connect 0, read 104, write 4121, timeout 666

Requests/sec: 119763.03

Transfer/sec: 24.67MB

[root@centos7 bin]# curl http://10.11.81.174:9999

write tcp 127.0.0.1:47785->127.0.0.1:6379: use of closed network connection

[root@centos7 bin]# ` 压完访问会提示write tcp 127.0.0.1:47785->127.0.0.1:6379: use of closed network connection

附件是我使用的代码 main.zip

回答

0

试了一下你的代码,好像没有用到连接池 试试加这一行:conn = redisInstance.Conn()

s.BindHandler("/", func(r *ghttp.Request) {
        // 加这一行
        conn = redisInstance.Conn()

        v, err := conn.Do("GET", "k")
        fmt.Println(v)
        if err != nil {
            r.Response.Write(err)
        }
        r.Response.Write(v)
    })
    s.Run()
1

ConnectTimeout:10s GET请求时,一直用的一个链接,导致后面的请求超时被断掉了。 请求参考最新的官方代码

package main

import (
    "fmt"
    "github.com/gogf/gf/database/gredis"
    "github.com/gogf/gf/util/gconv"
)

var (
    config = gredis.Config{
        Host : "127.0.0.1",
        Port : 6379,
        Db   : 1,
    }
)

func main() {
    group := "test"
    gredis.SetConfig(&config, group)

    redis := gredis.Instance(group)
    defer redis.Close()

    _, err := redis.Do("SET", "k", "v")
    if err != nil {
        panic(err)
    }

    r, err := redis.Do("GET", "k")
    if err != nil {
        panic(err)
    }
    fmt.Println(gconv.String(r))
}
0

试了你这种,一压就卡住,你可以用wrk压一下试试

[root@centos7 bin]#./wrk -c5000 -d10 -t4 -T3 --latencyv http://10.11.81.174:9999
Running 10s test @ http://10.11.81.174:9999
  4 threads and 5000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    17.10ms    7.48ms  31.69ms   57.29%
    Req/Sec     0.96k     0.00     0.96k   100.00%
  Latency Distribution
     50%   15.51ms
     75%   24.66ms
     90%   27.00ms
     99%   31.69ms
  96 requests in 10.09s, 13.22KB read
  Socket errors: connect 0, read 26, write 1924, timeout 0
Requests/sec:      9.52
Transfer/sec:      1.31KB
3

所以我要放在http服务调用,应该怎么写?我也是参照了这个例子放在一个http服务里面调用就不行了

1

没有conn的 在Do会默认调用close方法进行关闭

1

目前没有定位到什么原因

7

建议使用新版的V2.1.2版本