util

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnPoolGetTimeout = errors.New("ErrConnPoolGetTimeout")
	ErrConnPoolClosed     = errors.New("ErrConnPoolClosed")
	ErrNoIdleConn         = errors.New("NoIdleConn")

	IdleTimeout      = 60
	GetConnTimeout   = 60
	GetConnSleepTime = 100 * time.Millisecond
	ReDailTimes      = 2
)

Functions

func GetCallInArgs

func GetCallInArgs(funcValue reflect.Value, funcArgs []string, inStart int) ([]reflect.Value, error)

func GetStrMd5

func GetStrMd5(s string) string

func GetStrSha1

func GetStrSha1(data string) string

func GoValuesToYJsonSlice

func GoValuesToYJsonSlice(values []reflect.Value) ([]string, error)

func GoVarToYJson

func GoVarToYJson(v interface{}) (string, error)

func GoVarsToYJsonSlice

func GoVarsToYJsonSlice(args ...interface{}) ([]string, error)

func Max

func Max(a, b int) int

func Min

func Min(a, b int) int

Types

type ConnInterface

type ConnInterface interface {
	Dail(timeout int) error
	Close()
	GetId() string
	// SetId 注意,这个id要存下来。这个id其实是上次使用的时间戳,用于判断是否过期。格式是: {random_int}-{time}
	SetId(id string)
	// Clone 复制配置(host,pass等),然后返回指针(注意,必须返回【指针】!!!)
	Clone() interface{}
	// IsActive 判断链接是否存活,通常是调用ping。
	// 这个操作会浪费一点时间,若觉得不必要或是没有ping方法 也可以直接返回true
	// 如果直接返回true,使用pool.Get() 返回的链接时注意判断错误,因为链接可能因各种情况关闭。
	//     注意!!就算链接已经关闭,仍然需要调用pool.Put() (此时isBad为true),然后再调用pool.Get()重新获取链接
	IsActive() bool
}

type ConnPool

type ConnPool struct {
	Conf     ConnPoolConfig
	Conn     ConnInterface
	Mu       sync.Mutex
	IdleConn map[string]interface{} // 用slice会频繁修改,chan比较适合跨协程场景,因此用map
	Locked   bool
	NumOpen  int
	IsClose  bool
}

func NewConnPool

func NewConnPool(conn ConnInterface, conf ConnPoolConfig) ConnPool

func (*ConnPool) Close

func (p *ConnPool) Close()

func (*ConnPool) CloseConn

func (p *ConnPool) CloseConn(conn interface{})

func (*ConnPool) DecodeId

func (p *ConnPool) DecodeId(id string) (n int, t int64)

func (*ConnPool) GenId

func (p *ConnPool) GenId(randomNum int) string

func (*ConnPool) Get

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

func (*ConnPool) IsConnTimeout

func (p *ConnPool) IsConnTimeout(conn interface{}) bool

func (*ConnPool) Lock

func (p *ConnPool) Lock()

func (*ConnPool) NewConn

func (p *ConnPool) NewConn() (interface{}, error)

func (*ConnPool) Put

func (p *ConnPool) Put(conn interface{}, isBadConn bool)

Put isBadConn: 用户Get获取的链接时,如果链接已关闭或者因其他原因不能在用,则isBadConn为true

func (*ConnPool) UnLock

func (p *ConnPool) UnLock()

func (*ConnPool) UpdateConnTime

func (p *ConnPool) UpdateConnTime(conn interface{})

type ConnPoolConfig

type ConnPoolConfig struct {
	Size             int
	IdleTimeout      int // 链接超时时间. Second
	GetConnTimeout   int
	GetConnSleepTime time.Duration
	ReDailTimes      int // Dail报错的重试次数
}

func NewConnPoolConfig

func NewConnPoolConfig() ConnPoolConfig

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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