pool

package
v0.0.0-...-d3f65ed Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrPoolNewFuncIsNull = errors.New("container/pool: 初始化函数为空")
	// ErrPoolExhausted 连接以耗尽
	ErrPoolExhausted = errors.New("container/pool: 连接已耗尽")
	// ErrPoolClosed 连接池已关闭.
	ErrPoolClosed = errors.New("container/pool: 连接池已关闭")
)

Functions

func SetActive

func SetActive(active uint64) options.Option

SetActive 设置 Pool 连接数, 如果 == 0 则无限制

func SetIdle

func SetIdle(idle uint64) options.Option

SetIdle 设置最大空闲连接数

func SetIdleTimeout

func SetIdleTimeout(idleTimeout time.Duration) options.Option

SetIdleTimeout 设置空闲等待时间

func SetWait

func SetWait(wait bool, waitTimeout time.Duration) options.Option

SetWait 设置期望等待

Types

type IShutdown

type IShutdown interface {
	Shutdown() error
}

type List

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

List 双向链表

func (*List) Get

func (l *List) Get(ctx context.Context) (IShutdown, error)

Get 获取

Example
v, err := pool.Get(context.TODO())
if err != nil {
	// 处理错误
}
// v 获取到的资源
_ = v
Output:

func (*List) Init

func (l *List) Init(d time.Duration)

Init 初始化

func (*List) New

func (l *List) New(f func(ctx context.Context) (IShutdown, error))

New 设置创建资源函数

func (*List) Put

func (l *List) Put(ctx context.Context, s IShutdown, forceClose bool) error

Put 回收

Example
v, err := pool.Get(context.TODO())
if err != nil {
	// 处理错误
}

// Put: 资源回收
// forceClose: true 内部帮你调用 Shutdown回收, 否则判断是否是可回收,挂载到list上
err = pool.Put(context.TODO(), v, false)
if err != nil {
	// 处理错误
}
Output:

func (*List) Reload

func (l *List) Reload(options ...options.Option)

Reload 重新设置配置文件

func (*List) Shutdown

func (l *List) Shutdown() error

Shutdown 关闭

Example
// Shutdown 回收资源,关闭所有资源
_ = pool.Shutdown()
Output:

func (*List) Timer

func (l *List) Timer(d time.Duration)

Timer 定时任务

type Pool

type Pool interface {
	New(f func(ctx context.Context) (IShutdown, error))
	Get(ctx context.Context) (IShutdown, error)
	Put(ctx context.Context, c IShutdown, forceClose bool) error
	Shutdown() error
}

Pool interface.

func NewList

func NewList(options ...options.Option) Pool

NewList 实例化

Example
// NewList(options ...)
// 默认配置
// pool = NewList()

// 可供选择配置选项

// 设置 Pool 连接数, 如果 == 0 则无限制
// SetActive(100)

// 设置最大空闲连接数
// SetIdle(20)

// 设置空闲等待时间
// SetIdleTimeout(time.Second)

// 设置期望等待
// SetWait(false,time.Second)

// 自定义配置
pool = NewList(
	SetActive(100),
	SetIdle(20),
	SetIdleTimeout(time.Second),
	SetWait(false, time.Second))

// New需要实例化,否则在 pool.Get() 会无法获取到资源
pool.New(getResources)
Output:

Jump to

Keyboard shortcuts

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