util: use ClusterConnection.Copy() for re-using connections

Connections are reference counted, so just assigning the connection to
an other object for re-use is not correct. This can cause connections to
be garbage collected while something else is still using it.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2021-03-15 10:50:09 +01:00
committed by mergify[bot]
parent 6e941539b5
commit 5c26fbb0d7
4 changed files with 35 additions and 3 deletions

View File

@ -168,6 +168,22 @@ func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
return conn, nil
}
// Copy adds an extra reference count to the used ConnEntry and returns the
// *rados.Conn if it was found.
func (cp *ConnPool) Copy(conn *rados.Conn) *rados.Conn {
cp.lock.Lock()
defer cp.lock.Unlock()
for _, ce := range cp.conns {
if ce.conn == conn {
ce.get()
return ce.conn
}
}
return nil
}
// Put reduces the reference count of the rados.Conn object that was returned with
// ConnPool.Get().
func (cp *ConnPool) Put(conn *rados.Conn) {