go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbutil/pool_close_finalizer.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbutil 9 10 import ( 11 "go.charczuk.com/sdk/db" 12 "go.charczuk.com/sdk/errutil" 13 ) 14 15 // PoolCloseFinalizer is intended to be used in `defer` blocks with a named 16 // `error` return. It ensures a pool is closed after usage in contexts where 17 // a "limited use" pool is created. 18 // 19 // > func queries() (err error) { 20 // > var pool *db.Connection 21 // > defer func() { 22 // > err = db.PoolCloseFinalizer(pool, err) 23 // > }() 24 // > // ... 25 // > } 26 func PoolCloseFinalizer(pool *db.Connection, err error) error { 27 if pool == nil { 28 return err 29 } 30 closeErr := pool.Close() 31 if closeErr != nil { 32 return errutil.Append(err, closeErr) 33 } 34 return err 35 }