duai4512 2013-11-14 07:29
浏览 340
已采纳

go / golang + redis打开文件过多错误

I'm using redis in Golang with the Redigo connector (https://github.com/garyburd/redigo) suggested by the Redis website.

I have:

  • After every Dial() I defer a Close()
  • Set fs.file-max = 100000
  • Set vm.overcommit_memory = 1
  • Disabled saving
  • Set maxclients = 100000

I run a high traffic website, and everything runs great for about 10 minutes, from which I get

error: dial tcp 127.0.0.1:6379: too many open files

Then I can't access redis at all from my application.

I see nothing in the redis logs to suggest any errors or problems. What do I do to fix this?

  • 写回答

2条回答 默认 最新

  • ds9567 2013-11-15 01:10
    关注

    I don't know which driver you use but with redigo you can define a number of open connections in a pool, all you have to do then is in every query to redis, first get a client from the pool, then close it, so it goes back to the pool and gets re-used, just like this:

    redisPool = &redis.Pool{
            MaxIdle: 3,
            MaxActive: 10, // max number of connections
            Dial: func() (redis.Conn, error) {
                    c, err := redis.Dial("tcp", ":6379")
                    if err != nil {
                            panic(err.Error())
                    }
                    return c, err
            },
    }
    r := redisPool.Get() // get a client from the pool
    _, err = r.Do("GET one") // use the client
    if err != nil {
            panic(err.Error())
    }
    r.Close() // close the client so the connection gets reused
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?