Documentation ¶
Overview ¶
Package gpool provides object-reusable concurrent-safe pool.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool struct { TTL time.Duration // Time To Live for pool items. NewFunc func() (interface{}, error) // Callback function to create pool item. // ExpireFunc is the for expired items destruction. // This function needs to be defined when the pool items // need to perform additional destruction operations. // Eg: net.Conn, os.File, etc. ExpireFunc func(interface{}) // contains filtered or unexported fields }
Pool is an Object-Reusable Pool.
func New ¶
func New(ttl time.Duration, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool
New creates and returns a new object pool. To ensure execution efficiency, the expiration time cannot be modified once it is set.
Note the expiration logic: ttl = 0 : not expired; ttl < 0 : immediate expired after use; ttl > 0 : timeout expired;
Example ¶
package main import ( "database/sql" "fmt" "time" "github.com/gogf/gf/v2/container/gpool" ) func main() { type DBConn struct { Conn *sql.Conn } dbConnPool := gpool.New(time.Hour, func() (interface{}, error) { dbConn := new(DBConn) return dbConn, nil }, func(i interface{}) { // sample : close db conn // i.(DBConn).Conn.Close() }) fmt.Println(dbConnPool.TTL) }
Output: 1h0m0s
func (*Pool) Clear ¶
func (p *Pool) Clear()
Clear clears pool, which means it will remove all items from pool.
Example ¶
package main import ( "database/sql" "fmt" "time" "github.com/gogf/gf/v2/container/gpool" ) func main() { type DBConn struct { Conn *sql.Conn Limit int } dbConnPool := gpool.New(time.Hour, func() (interface{}, error) { dbConn := new(DBConn) dbConn.Limit = 10 return dbConn, nil }, func(i interface{}) { i.(*DBConn).Limit = 0 // sample : close db conn // i.(DBConn).Conn.Close() }) conn, _ := dbConnPool.Get() dbConnPool.MustPut(conn) dbConnPool.MustPut(conn) fmt.Println(dbConnPool.Size()) dbConnPool.Clear() fmt.Println(dbConnPool.Size()) }
Output: 2 0
func (*Pool) Close ¶
func (p *Pool) Close()
Close closes the pool. If `p` has ExpireFunc, then it automatically closes all items using this function before it's closed. Commonly you do not need to call this function manually.
Example ¶
package main import ( "database/sql" "fmt" "time" "github.com/gogf/gf/v2/container/gpool" ) func main() { type DBConn struct { Conn *sql.Conn Limit int } var ( newFunc = func() (interface{}, error) { dbConn := new(DBConn) dbConn.Limit = 10 return dbConn, nil } closeFunc = func(i interface{}) { fmt.Println("Close The Pool") // sample : close db conn // i.(DBConn).Conn.Close() } ) dbConnPool := gpool.New(time.Hour, newFunc, closeFunc) conn, _ := dbConnPool.Get() dbConnPool.MustPut(conn) dbConnPool.Close() // wait for pool close time.Sleep(time.Second * 1) // May Output: // Close The Pool }
Output:
func (*Pool) Get ¶
Get picks and returns an item from pool. If the pool is empty and NewFunc is defined, it creates and returns one from NewFunc.
Example ¶
package main import ( "database/sql" "fmt" "time" "github.com/gogf/gf/v2/container/gpool" ) func main() { type DBConn struct { Conn *sql.Conn Limit int } dbConnPool := gpool.New(time.Hour, func() (interface{}, error) { dbConn := new(DBConn) dbConn.Limit = 10 return dbConn, nil }, func(i interface{}) { // sample : close db conn // i.(DBConn).Conn.Close() }) conn, err := dbConnPool.Get() if err == nil { fmt.Println(conn.(*DBConn).Limit) } }
Output: 10
func (*Pool) MustPut ¶
func (p *Pool) MustPut(value interface{})
MustPut puts an item to pool, it panics if any error occurs.
func (*Pool) Put ¶
Put puts an item to pool.
Example ¶
package main import ( "database/sql" "fmt" "time" "github.com/gogf/gf/v2/container/gpool" ) func main() { type DBConn struct { Conn *sql.Conn Limit int } dbConnPool := gpool.New(time.Hour, func() (interface{}, error) { dbConn := new(DBConn) dbConn.Limit = 10 return dbConn, nil }, func(i interface{}) { // sample : close db conn // i.(DBConn).Conn.Close() }) // get db conn conn, _ := dbConnPool.Get() // modify this conn's limit conn.(*DBConn).Limit = 20 // example : do same db operation // conn.(*DBConn).Conn.QueryContext(context.Background(), "select * from user") // put back conn dbConnPool.MustPut(conn) fmt.Println(conn.(*DBConn).Limit) }
Output: 20
func (*Pool) Size ¶
Size returns the count of available items of pool.
Example ¶
package main import ( "database/sql" "fmt" "time" "github.com/gogf/gf/v2/container/gpool" ) func main() { type DBConn struct { Conn *sql.Conn Limit int } dbConnPool := gpool.New(time.Hour, func() (interface{}, error) { dbConn := new(DBConn) dbConn.Limit = 10 return dbConn, nil }, func(i interface{}) { // sample : close db conn // i.(DBConn).Conn.Close() }) conn, _ := dbConnPool.Get() fmt.Println(dbConnPool.Size()) dbConnPool.MustPut(conn) dbConnPool.MustPut(conn) fmt.Println(dbConnPool.Size()) }
Output: 0 2