why is RLock mutex used in Close() on PoolConn
AnirudhVyas opened this issue · 3 comments
func (p *PoolConn) Close() error {
p.mu.RLock()
defer p.mu.RUnlock()
if p.unusable {
if p.Conn != nil {
return p.Conn.Close()
}
return nil
}
return (p.Conn)
}
Why RLock - could you clarify? Sorry if its newbie question - still trying to go into goland ...
This lock make sure that conn.MarkUnusable() will not run concurrently with conn.Close()
MarkUnusable will block all Close operations until it finishes.
For example you can do something like:
go conn.Close()
conn.MarkUnusable()
It this example MarkUnused()
writes p.unusable
and Close()
reads it.
Race condition occurs.
I hope this will help.
@AnirudhVyas Using RLock may cause duplicated calling of Close and a error will be returned immediately, which does no harm to your proram because you intent to Close the conn and you achived, you can do whatever you want with the error, it doesn't matter. On the other hand, you gain a higher concurrency than using Lock, this is important. Just my personal view.
I'm archiving and closing this project. It's not going to be maintained anymore. Thanks all for their valuable feedback and contributions. If you have questions, feel free to reach me from https://twitter.com/fatih