Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called. However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored.
()
| 17 | // Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called. |
| 18 | // However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored. |
| 19 | func (c *Conn) Release() { |
| 20 | if c.res == nil { |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | conn := c.Conn() |
| 25 | res := c.res |
| 26 | c.res = nil |
| 27 | |
| 28 | if c.p.releaseTracer != nil { |
| 29 | c.p.releaseTracer.TraceRelease(c.p, TraceReleaseData{Conn: conn}) |
| 30 | } |
| 31 | |
| 32 | if conn.IsClosed() || conn.PgConn().IsBusy() || conn.PgConn().TxStatus() != 'I' { |
| 33 | res.Destroy() |
| 34 | // Signal to the health check to run since we just destroyed a connections |
| 35 | // and we might be below minConns now |
| 36 | c.p.triggerHealthCheck() |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | // If the pool is consistently being used, we might never get to check the |
| 41 | // lifetime of a connection since we only check idle connections in checkConnsHealth |
| 42 | // so we also check the lifetime here and force a health check |
| 43 | if c.p.isExpired(res) { |
| 44 | c.p.lifetimeDestroyCount.Add(1) |
| 45 | res.Destroy() |
| 46 | // Signal to the health check to run since we just destroyed a connections |
| 47 | // and we might be below minConns now |
| 48 | c.p.triggerHealthCheck() |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | if c.p.afterRelease == nil { |
| 53 | res.Release() |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | go func() { |
| 58 | if c.p.afterRelease(conn) { |
| 59 | res.Release() |
| 60 | } else { |
| 61 | res.Destroy() |
| 62 | // Signal to the health check to run since we just destroyed a connections |
| 63 | // and we might be below minConns now |
| 64 | c.p.triggerHealthCheck() |
| 65 | } |
| 66 | }() |
| 67 | } |
| 68 | |
| 69 | // Hijack assumes ownership of the connection from the pool. Caller is responsible for closing the connection. Hijack |
| 70 | // will panic if called on an already released or hijacked connection. |