duanfuxing2417
2014-02-25 04:50
浏览 222

Golang增量数据到Redis

I have been playing around with golang and redis. I just stood up a simple http server and wanted to increment requests on redis. I am blowing up the connections (I think). I found that with redigo you can use connection pooling, but not sure how to implment that in go when I am serving the requests (where do you instantiate / call the pool from).

error: can't assign requested address.

Any suggestions would be appreciated....I am sure I am incorrectly making the connections, but just not sure how to change.

EDIT: Modified per pauljz's suggestions -- Works great now

    var pool redis.Pool

    func qryJson(rw http.ResponseWriter, req *http.Request){

        incrementRedis()
    }

    func incrementRedis () {

    t := time.Now().Format("2006-01-02 15:04:05")
    conn := pool.Get()
    defer conn.Close()
    if _, err := conn.Do("HINCRBY", "messages", t, 1); err != nil {
        log.Fatal(err)

    }


    }

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    pool = redis.Pool{
            MaxIdle: 50,
            MaxActive: 500, // max number of connections
            Dial: func() (redis.Conn, error) {
                    c, err := redis.Dial("tcp", ":6379")
                    if err != nil {
                            panic(err.Error())
                    }
                    return c, err
            },
    } 
    http.HandleFunc("/testqry", qryJson)
    log.Fatal(http.ListenAndServe(":8082", nil))
}

图片转代码服务由CSDN问答提供 功能建议

我一直在玩golang和redis。 我只是站在一个简单的http服务器上,想增加redis上的请求。 我正在炸毁连接(我认为)。 我发现使用redigo可以使用连接池,但是不确定在我处理请求时如何实现(您从哪里实例化/调用池)。

错误:无法分配请求的地址。

任何建议将不胜感激。...我确定我错误地建立了连接,但是不确定如何更改。

编辑:根据pauljz的建议进行了修改-现在可以正常使用

  var pool redis.Pool 
  
 func qryJson(rw http.ResponseWriter,req * http.Request){
 
crementRedis()
} 
 
 func增量Redis(){
 
t:= time.Now()。Format(  “ 2006-01-02 15:04:05”)
 conn:= pool.Get()
如果_,err == conn.Do(“ HINCRBY”,“ messages”,则推迟conn.Close()
  ,t,1);  err!= nil {
 log.Fatal(err)
 
} 
 
 
} 
 
func main(){
 runtime.GOMAXPROCS(runtime.NumCPU())
 pool = redis  .Pool {
 MaxIdle:50,
 MaxActive:500,//最大连接数
拨号:func()(redis.Conn,错误){
c,err:= redis.Dial(“ tcp”,  “:6379”)
 if err!= nil {
 panic(err.Error())
} 
 return c,err 
},
} 
 http.HandleFunc(“ / testqry”,  qryJson)
 log.Fatal(http.ListenAndServe(“:8082”,nil))
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyuan9512 2014-02-25 06:20
    已采纳

    The redigo docs have a good starter example for connection pooling: http://godoc.org/github.com/garyburd/redigo/redis#Pool

    In your case you would have a var pool redis.Pool in your package (i.e. not inside of a function).

    In main(), before your ListenAndServe call, you would call pool = redis.Pool{ ... } from the redigo example to initialize the pool.

    In incrementRedis() you would then do something like:

    func incrementRedis () {
        conn := pool.Get()
        defer conn.Close()
        if _, err := conn.Do("HINCRBY", "messages", t, 1); err != nil {
            log.Fatal(err)
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxixiu9134 2014-02-25 05:36

    In your code, you create a connection to redis for each http request. Use a global variable to store connected redis connection and reuse it.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题