gpool

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 9 Imported by: 1

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

Name Description Type Default
InitialPoolSize initial pool size. int 5
MinPoolSize min item in pool. int 2
MaxPoolSize max item in pool. int 15
AcquireRetryAttempts retry times when get item Failed. int 5
AcquireIncrement create item count when pool is empty. int 5
TestDuration interval time between check item avaiable.Unit:Millisecond int 1000
TestOnGetItem test avaiable when get item. bool false
Params item initial params map[string]string

Complete Example

here is a Complete Example : gpool_example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCanNotGetItem return when get item failed
	ErrCanNotGetItem = errors.New("Unable GET Item")
	// ErrTypeConvert return when Convert item to require type
	ErrTypeConvert = errors.New("Class Cast Failed")
	// ErrItemInitialFailed return when Initial item failed
	ErrItemInitialFailed = errors.New("Iem Initial Failed")
	// ErrPoolExtendFailed return when Initial pool  failed
	ErrPoolExtendFailed = errors.New("Pool Extend Failed")
	// ErrHasBeenShotdown return when do something after pool has been shutdown
	ErrHasBeenShotdown = errors.New("Pool has been shutdown")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	//InitialPoolSize initial pool size. Default: 5
	InitialPoolSize int
	//MinPoolSize min item in pool. Default: 2
	MinPoolSize int
	//MaxPoolSize  max item in pool. Default: 15
	MaxPoolSize int
	//AcquireRetryAttempts retry times when get item Failed. Default: 5
	AcquireRetryAttempts int
	//AcquireIncrement create item count when pool is empty. Default: 5
	AcquireIncrement int
	//TestDuration interval time between check item avaiable.Unit:Millisecond Default: 1000
	TestDuration int
	//TestOnGetItem test avaiable when get item. Default: false
	TestOnGetItem bool
	//Params item initial params
	Params map[string]string
}

Config pool config

func DefaultConfig

func DefaultConfig() Config

DefaultConfig create default config

func (*Config) LoadToml

func (config *Config) LoadToml(tomlFilePath string) error

LoadToml load config from toml file

func (*Config) String

func (config *Config) String() string

String String

type Creator added in v0.0.3

type Creator func() Item

Creator function create item

type Item

type Item interface {
	Initial(map[string]string) error
	Destory(map[string]string) error
	Check(map[string]string) error
}

Item pool item

type Pool

type Pool struct {
	Config Config
	// contains filtered or unexported fields
}

Pool pool class

func DefaultPool

func DefaultPool(creater Creator) *Pool

DefaultPool create a pool with default config

func (*Pool) BackOne

func (pool *Pool) BackOne(item Item)

BackOne give back a pool item

func (*Pool) GetOne

func (pool *Pool) GetOne() (Item, error)

GetOne get a pool item

func (*Pool) Initial

func (pool *Pool) Initial()

Initial initial pool

func (*Pool) Shutdown

func (pool *Pool) Shutdown(wg *sync.WaitGroup)

Shutdown shutdown pool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL