gormpool

package
v0.0.0-...-35ea8dd Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGetFromClosedPool if try to get a Conn from the closed pool
	ErrGetFromClosedPool = errors.New("pool: get from closed pool")
	// ErrExceedingMaxWaitingDuration try to get a Conn from the pool but exceeding the max waiting time
	ErrExceedingMaxWaitingDuration = errors.New("pool: exceeding the maximum waiting duration")
	// ErrSQLType try to connect the SQL which does not support by the pool
	ErrSQLType = errors.New("pool: sql type does not support")
	// ErrKeepLTCapacity keepConn larger than the pool capacity
	ErrKeepLTCapacity = errors.New("pool: KeepConn larger than Capacity")
	// ErrCapacity provide a invalid capacity
	ErrCapacity = errors.New("pool: invalid capacity size")
	// ErrEmptyArgs DB arg is empty
	ErrEmptyArgs = errors.New("pool: args cannot be empty")
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	DB               *gorm.DB
	Key              string
	Created, Updated time.Time
	UsageCounter     uint64
}

Conn is a struct wraps the internal DB connection

type ConnsState

type ConnsState struct {
	Size  uint64
	Conns map[string]*Conn
}

ConnsState shows Connections List state

type Options

type Options struct {
	// DB type, e.g. MySQL, SQLite3...
	Type godbpool.SQLType

	// DB connection configuration
	Args interface{}

	// Conn key generate function
	KeyFunc func() string

	// how many idle conn to keep when there are no work to do
	// this field should smaller than Capacity
	KeepConn uint64

	// Maximum number of connections allocated by the pool at a given time.
	Capacity uint64

	// Maximum waiting duration to get a conn from the pool
	MaxWaitDuration time.Duration
	// contains filtered or unexported fields
}

Options is pool configuration

type Pool

type Pool struct {
	Type godbpool.SQLType

	Args interface{}
	// contains filtered or unexported fields
}

Pool is connection pool

func NewPool

func NewPool(ctx context.Context, opts Options) (p *Pool, err error)

NewPool will create a new pool

func (*Pool) Close

func (p *Pool) Close()

Close the pool

func (*Pool) Get

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

Get a SQL connection from the pool

func (*Pool) Put

func (p *Pool) Put(conn *Conn)

Put back a connection in the pool

func (*Pool) Status

func (p *Pool) Status() (ps PoolState)

Status shows the current pool status of the pool

type PoolState

type PoolState struct {
	IdleConnsState       ConnsState
	BusyConnsState       ConnsState
	Capacity             uint64
	Size                 uint64
	Closed               bool
	TotalWaitingDuration time.Duration
	CurrentWaitCount     uint64
	TotalWaitCount       uint64
	DroppedGetCount      uint64
}

PoolState shows pool state

Directories

Path Synopsis
my

Jump to

Keyboard shortcuts

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