Categorygithub.com/cloudfstrife/gpool
modulepackage
0.0.6
Repository: https://github.com/cloudfstrife/gpool.git
Documentation: pkg.go.dev

# README

gpool

library for create pool easy , write in google go language

USAGE

create pool item struct and implement Item interface

Item interface function list

Initial(map[string]string) error
Destory(map[string]string) error
Check(map[string]string) error

example

//Connection pool item struct
type Connection struct {
	TCPConn net.Conn
}

//Initial Initial operation
func (c *Connection) Initial(params map[string]string) error {
	con, err := net.Dial("tcp", params["host"]+":"+params["port"])
	if err != nil {
		return err
	}
	c.TCPConn = con
	return nil
}

//Destory Destory Connection
func (c *Connection) Destory(params map[string]string) error {
	return c.TCPConn.Close()
}

//Check check item avaiable
func (c *Connection) Check(params map[string]string) error {
	fmt.Println("Check item Avaiable")
	return nil
}

create item factory

//NewConnection New item 
func NewConnection() gpool.Item {
	return &Connection{}
}

create Singleton pool

var (
	pool *gpool.Pool
	once sync.Once
)

func init() {
	once.Do(func() {
		pool = gpool.DefaultPool(NewConnection)
		pool.Config.LoadToml("general.toml")
		pool.Initial()
	})
}

implement get Item and give back item

//GetConnection Get item Connection
func GetConnection() (net.Conn, error) {
	item, err := pool.GetOne()
	if err != nil {
		return nil, err
	}
	con, ok := item.(*Connection)
	if ok {
		return con.TCPConn, nil
	}
	return nil, errors.New("Class cast ERROR")
}

//CloseConnection back item Connection
func CloseConnection(conn net.Conn) {
	pool.BackOne(&Connection{
		TCPConn: conn,
	})
}

implement close pool

func ClosePool() {
	wg := &sync.WaitGroup{}
	wg.Add(1)
	pool.Shutdown(wg)
	wg.Wait()
}

use pool

omit

Config

NameDescriptionTypeDefault
InitialPoolSizeinitial pool size.int5
MinPoolSizemin item in pool.int2
MaxPoolSizemax item in pool.int15
AcquireRetryAttemptsretry times when get item Failed.int5
AcquireIncrementcreate item count when pool is empty.int5
TestDurationinterval time between check item avaiable.Unit:Millisecondint1000
TestOnGetItemtest avaiable when get item.boolfalse
Paramsitem initial paramsmap[string]string

Complete Example

here is a Complete Example : gpool_example

# Functions

DefaultConfig create default config.
DefaultPool create a pool with default config.

# Variables

ErrCanNotGetItem return when get item failed.
ErrHasBeenShotdown return when do something after pool has been shutdown.
ErrItemInitialFailed return when Initial item failed.
ErrPoolExtendFailed return when Initial pool failed.
ErrTypeConvert return when Convert item to require type.

# Structs

Config pool config.
Pool pool class.

# Interfaces

Item pool item.

# Type aliases

Creator function create item.