redis-adapter
redis-adapter copied to clipboard
redis pool often has error
my project use this adapter and initlize redis pool when project start. after 30 minutes when website send api request, the redis pool always report 'use of closed network connection' or 'write break pipe'.
this is my code
i search this problem in internet. it likes redisgo problem, so i goto redisgo github addr and search in issues. i find this reply
then i see the adapter origin code. this adapter func NewAdapterWithPool() don't call defer conn.Close()
so i guess maybe is it make problem. plz vaild it and fix it. or if other problem plz tell me. thk
@kissIce @marcozac hi, can you make a PR to fix it?
https://github.com/casbin/redis-adapter/commit/0cd40e5655c43a9787353fca24087fde2b571b58
@kissIce @hsluoyz sorry for the delay!
The pool should be created before to pass it to NewAdapterWithPool
, using (defer?) pool.Close()
to close all resources. It frequently happens in the main function of the program.
For example:
func main() {
pool := &redis.Pool{
// ...
}
defer pool.Close()
a, err := redisadapter.NewAdapterWithPool(pool)
if err != nil {
// ...
}
cas := common.CasbinSt{}
cas.RedisAdapter, _ = a
// ...
}
The pooled conn il closed by the finalizer (*Adapter
does not expose a Close
method) instead.
Using defer conn.Close()
in NewAdapterWithPool
would close the connection when the function returns.
However, I'll take a look and some tests in the weekend.
I have a server in production using this adapter but I didn't find this issue. I'll try to replicate and fix it.
@kissIce if you want to share more details it would be very helpful!
Just a last (maybe intrusive) question: are you sure you need a pool? Based on the screenshot you shared, the same result would be accomplished with a simple connection leaving to the finalizer the responsibility to close it.
As far as i know, when you use a Redis connection pool, you should use the pool.Get() method every time when you need to retrieve an available connection, rather than just getting it once during initialization.
And i make a PR(#43), you can take a look.