英文:
Golang Increment data to Redis
问题
我一直在使用golang和redis进行尝试。我刚刚搭建了一个简单的HTTP服务器,并希望在redis上增加请求次数。我认为我正在创建过多的连接。我发现使用redigo可以使用连接池,但不确定在处理请求时如何实现连接池(在哪里实例化/调用连接池)。
错误:无法分配请求的地址。
如果有建议,将不胜感激...我确定我在错误地创建连接,但不确定如何更改。
编辑:根据pauljz的建议进行修改--现在运行得很好
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, // 最大连接数
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))
}
以上是你提供的代码的翻译。
英文:
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))
}
答案1
得分: 4
redigo文档中有一个关于连接池的很好的入门示例:http://godoc.org/github.com/garyburd/redigo/redis#Pool
在你的情况下,你的包中会有一个var pool redis.Pool
(即不在函数内部)。
在main()
函数中,在你的ListenAndServe
调用之前,你会调用redigo示例中的pool = redis.Pool{ ... }
来初始化连接池。
在incrementRedis()
函数中,你可以这样做:
func incrementRedis() {
conn := pool.Get()
defer conn.Close()
if _, err := conn.Do("HINCRBY", "messages", t, 1); err != nil {
log.Fatal(err)
}
}
英文:
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)
}
}
答案2
得分: 3
在你的代码中,你为每个HTTP请求创建了一个与Redis的连接。使用一个全局变量来存储已连接的Redis连接,并重复使用它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论