Help us shape the future of Go! Take the 2026 Go Developer Survey

pool

package module
v0.0.0-...-bb1af9d Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2016 License: Apache-2.0 Imports: 2 Imported by: 1

README

golang-pool

pool.go

pool.go is a pool implementation for golang. You may need it if you use golang with version <= 1.2

Its features include:

  • Network failure tolerance.
  • Establishing and closing connections on demand depending on the network state.

Installation

Just run go get github.com/jinntrance/pool

Examples

Initial a pool

var pool = Pool{
        //how to create a client and then put it into the pool
        New: func() (interface{}, error) {
                cli, err := CreateAClient(GetAServer())
                return cli, err
        },
        //do something when closing the client
        Close: func(x interface{}) {
                x.(*Client).Close()
        },
        PoolSize: 100, //MaxNum of clients to retain
}

Where Client is the type for your sepecific client. Then use the pool just created

cli, err := pool.Get()
if nil == err {
        client := cli.(*Client) //get a client and convert it to a specific type
        re, lastError := client.doSomething()
        err = lastError //record the last error
} 
pool.Put(cli, err) // put back the client with the error. If error occurs and 'err' is not nil, 'cli' would be closed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {

	//return a new object and error
	New func() (interface{}, error)
	//close the element, usually a client
	Close       func(interface{})
	PoolSize    int
	MinPoolSize int

	RecycleTimeout int
	// contains filtered or unexported fields
}

func (*Pool) Cleanup

func (p *Pool) Cleanup()

func (*Pool) Get

func (p *Pool) Get() (interface{}, error)

func (*Pool) Init

func (p *Pool) Init() *Pool

func (*Pool) Put

func (p *Pool) Put(x interface{}, lastError error)

Put element 'x' back to the pool.

Jump to

Keyboard shortcuts

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