pool

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

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

Go to latest
Published: Apr 11, 2020 License: MIT Imports: 5 Imported by: 2

README

该仓库改自 https://github.com/fatih/pool 使用方式: 在自己的函数中创建一个factory函数,将需要生成的连接放入该函数中,然后调用NewPool()函数获得连接池,下面以postgres数据库的连接为例

var (
    factory = func () (time.Duration, io.Closer, error){
        c,err := sql.Open("postgres", fmt.Sprintf("user=%s host=%s dbname=%s sslmode=%s password=%s", user, host, dbname, sslmode, password))
        return time.Second * 10, c, err
    }
)
func main(){
    P, err := NewPool(1, 1, factory)
    if err != nil {
        log.Printf("create Pool failed,err is: %s\n", err.Error())
    }

    //从连接池中获取连接
    v, err := P.Get()
    if err != nil {
        log.Printf("get connection from pool failed, err is: %s\n", err.Error())
    }
    
    //使用断言来使用连接池内资源, 实际使用中请根据实际情况进行断言
    v.(*pool.Conn).Client.(*sql.DB).DoSomething()
    ...
    
    //将使用完的连接放回连接池
    v.Close()

    //如果希望真实的关闭该连接,需要使用MarkUnusable()方法
    //v.(*pool.Conn).MarkUnusable()
}

Documentation

Overview

Package pool implements a pool of net.Conn interfaces to manage and reuse them.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is the error resulting if the pool is closed via pool.Close().
	ErrClosed = errors.New("pool is closed")
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Client  io.Closer
	Checker func(io.Closer) bool
	// contains filtered or unexported fields
}

Conn is a wrapper around io.Closer

func (*Conn) CheckOK

func (c *Conn) CheckOK() bool

CheckOK check conn health

func (*Conn) Close

func (c *Conn) Close() error

Close puts the given connects back to the pool instead of closing it.

func (*Conn) MarkUnusable

func (c *Conn) MarkUnusable()

MarkUnusable () marks the connection not usable any more, to let the pool close it instead of returning it to pool.

type Factory

type Factory func() (time.Duration, io.Closer, func(io.Closer) bool, error)

Factory is a function to create new connections.

type IPool

type IPool interface {
	// Get returns a new connection from the pool. Closing the connections puts
	// it back to the Pool. Closing it when the pool is destroyed or full will
	// be counted as an error.
	Get() (*Conn, error)

	// Close closes the pool and all its connections. After Close() the pool is
	// no longer usable.
	Close() error

	// Len returns the current number of connections of the pool.
	Len() int
}

Ipool interface describes a pool implementation. A pool should have maximum capacity. An ideal pool is threadsafe and easy to use.

func NewPool

func NewPool(initialCap, maxCap int, factory Factory) (IPool, error)

NewPool returns a new pool based on buffered channels with an initial capacity and maximum capacity. Factory is used when initial capacity is greater than zero to fill the pool. A zero initialCap doesn't fill the Pool until a new Get() is called. During a Get(), If there is no new connection available in the pool, a new connection will be created via the Factory() method.

type Pool

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

Pool implements the Pool interface based on buffered channels.

func (*Pool) Close

func (p *Pool) Close() error

Close close all resource in the pool

func (*Pool) Get

func (p *Pool) Get() (*Conn, error)

Get implements the Pool interfaces Get() method. If there is no new connection available in the pool, a new connection will be created via the Factory() method.

func (*Pool) Len

func (p *Pool) Len() int

Len return pool's length

Jump to

Keyboard shortcuts

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