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[T any] struct { TTL time.Duration // Time To Live for pool items. NewFunc func() (T, 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(T) // contains filtered or unexported fields }
Pool is an Object-Reusable Pool.
func New ¶
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/wesleywu/gcontainer/gpool"
)
func main() {
type DBConn struct {
Conn *sql.Conn
}
dbConnPool := gpool.New[*DBConn](time.Hour,
func() (*DBConn, error) {
dbConn := new(DBConn)
return dbConn, nil
},
func(i *DBConn) {
// sample : close db conn
// i.(DBConn).Conn.Close()
})
fmt.Println(dbConnPool.TTL)
}
Output: 1h0m0s
func (*Pool[T]) Clear ¶
func (p *Pool[T]) Clear()
Clear clears pool, which means it will remove all items from pool.
Example ¶
package main
import (
"database/sql"
"fmt"
"time"
"github.com/wesleywu/gcontainer/gpool"
)
func main() {
type DBConn struct {
Conn *sql.Conn
Limit int
}
dbConnPool := gpool.New[*DBConn](time.Hour,
func() (*DBConn, error) {
dbConn := new(DBConn)
dbConn.Limit = 10
return dbConn, nil
},
func(i *DBConn) {
i.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[T]) Close ¶
func (p *Pool[T]) 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/wesleywu/gcontainer/gpool"
)
func main() {
type DBConn struct {
Conn *sql.Conn
Limit int
}
var (
newFunc = func() (*DBConn, error) {
dbConn := new(DBConn)
dbConn.Limit = 10
return dbConn, nil
}
closeFunc = func(i *DBConn) {
fmt.Println("Close The Pool")
// sample : close db conn
// i.(DBConn).Conn.Close()
}
)
dbConnPool := gpool.New[*DBConn](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[T]) 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/wesleywu/gcontainer/gpool"
)
func main() {
type DBConn struct {
Conn *sql.Conn
Limit int
}
dbConnPool := gpool.New[*DBConn](time.Hour,
func() (*DBConn, error) {
dbConn := new(DBConn)
dbConn.Limit = 10
return dbConn, nil
},
func(i *DBConn) {
// sample : close db conn
// i.(DBConn).Conn.Close()
})
conn, err := dbConnPool.Get()
if err == nil {
fmt.Println(conn.Limit)
}
}
Output: 10
func (*Pool[T]) MustPut ¶
func (p *Pool[T]) MustPut(value T)
MustPut puts an item to pool, it panics if any error occurs.
func (*Pool[T]) Put ¶
Put puts an item to pool.
Example ¶
package main
import (
"database/sql"
"fmt"
"time"
"github.com/wesleywu/gcontainer/gpool"
)
func main() {
type DBConn struct {
Conn *sql.Conn
Limit int
}
dbConnPool := gpool.New[*DBConn](time.Hour,
func() (*DBConn, error) {
dbConn := new(DBConn)
dbConn.Limit = 10
return dbConn, nil
},
func(i *DBConn) {
// sample : close db conn
// i.(DBConn).Conn.Close()
})
// get db conn
conn, _ := dbConnPool.Get()
// modify this conn's limit
conn.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.Limit)
}
Output: 20
func (*Pool[T]) Size ¶
Size returns the count of available items of pool.
Example ¶
package main
import (
"database/sql"
"fmt"
"time"
"github.com/wesleywu/gcontainer/gpool"
)
func main() {
type DBConn struct {
Conn *sql.Conn
Limit int
}
dbConnPool := gpool.New[*DBConn](time.Hour,
func() (*DBConn, error) {
dbConn := new(DBConn)
dbConn.Limit = 10
return dbConn, nil
},
func(i *DBConn) {
// 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