Hi, guys
I saw the issue <https://github.com/golang/go/issues/13347> posted by Ian
Lance Tayler on finalizers. I am wandering whether the statement "input
args are kept live until the end of the function, including the receiver
for methods" commented by Keith Randy is true in Go 1.6
Here is my code using runtime.SetFinalizer() in a concurrent environment:
__________________________________________________
type redisCheckerT struct {
connPool *lru.LRUCache
}
func (r *redisCheckerT) ping(host_port string) error {
connif, ok := r.connPool.Get(host_port)
if !ok {
c, err := redis.DialTimeout(
"tcp", host_port, time.Second, 3 * time.Second, 3 * time.Second)
if err != nil {
return err
}
r.connPool.Put(host_port, c)
runtime.SetFinalizer(c, redis.Conn.Close)
connif = c
}
conn := connif.(redis.Conn)
_, err := conn.Do("ping")
// Ensure conn is not finalized before conn.Do() returns
//runtime.KeepAlive(conn)
if err != nil {
r.connPool.Remove(host_port)
}
return err
}
func (r *redisCheckerT) CheckAlive(host_port string) error {
var err error
for i := 0; i < 5; i++ { // try a few times
err = r.ping(host_port)
if err != nil {
continue
}
return nil
}
log.InfoErrorf(err, "Ping server '%s' failed", host_port)
return err
}
___________________________________________________
Will conn be finalized before conn.Do() returns? Thanks very much
Notice that runtime.KeepAlive is not available in Go 1.6, so I commented it
out.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.