util

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2019 License: Apache-2.0 Imports: 15 Imported by: 16

Documentation

Overview

Package util ResourcePool provides functionality to manage and reuse resources like connections.

Index

Constants

View Source
const (
	// StmtType stmt type
	StmtType = "stmtType" // SQL类型, 值类型为int (对应parser.Preview()得到的值)
	// FromSlave if read from slave
	FromSlave = "fromSlave" // 读写分离标识, 值类型为int, false = 0, true = 1
)

Variables

View Source
var (
	// ErrClosed is returned if ResourcePool is used when it's closed.
	ErrClosed = errors.New("resource pool is closed")

	// ErrTimeout is returned if a resource get times out.
	ErrTimeout = errors.New("resource pool timed out")
)

Functions

func Concat

func Concat(strings ...string) string

func GetValueExprResult

func GetValueExprResult(n *driver.ValueExpr) (interface{}, error)

GetValueExprResult copy from ValueExpr.Restore() TODO: 分表列是否需要支持等值比较NULL

func HostName

func HostName(ip string) (hostname string, err error)

HostName return hostname by ip

func Int2TimeDuration

func Int2TimeDuration(t int) (time.Duration, error)

Int2TimeDuration convert int to Time.Duration

func ItoString

func ItoString(a interface{}) (bool, string)

ItoString interface to string

func Left

func Left(str string, length int, pad string) string

Left left-pads the string with pad up to len runes len may be exceeded if

func ResolveAddr

func ResolveAddr(network string, locAddr string) (string, error)

ResolveAddr return real ip by net interface

func Right(str string, length int, pad string) string

Right right-pads the string with pad up to len runes

Types

type BoolIndex

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

BoolIndex rolled array switch mark

func (*BoolIndex) Get

func (b *BoolIndex) Get() (int32, int32, bool)

Get return current, next, current bool value

func (*BoolIndex) Set

func (b *BoolIndex) Set(index bool)

Set set index value

type Factory

type Factory func() (Resource, error)

Factory is a function that can be used to create a resource.

type IPInfo

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

IPInfo ip information

func ParseIPInfo

func ParseIPInfo(v string) (IPInfo, error)

ParseIPInfo parse ip

func (*IPInfo) Info

func (t *IPInfo) Info() string

Info return information of ip

func (*IPInfo) Match

func (t *IPInfo) Match(ip net.IP) bool

Match check if ip matched

type MurmurHash

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

MurmurHash copy from guava Murmur3_32HashFunction

func NewMurmurHash

func NewMurmurHash(seed int) *MurmurHash

NewMurmurHash constructor of MurmurHash

func (*MurmurHash) HashUnencodedChars

func (m *MurmurHash) HashUnencodedChars(inputStr string) int

HashUnencodedChars check if has unencoded chars

type RequestContext

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

RequestContext means request scope context with values thread safe

func NewRequestContext

func NewRequestContext() *RequestContext

NewRequestContext return request scopre context

func (*RequestContext) Get

func (reqCtx *RequestContext) Get(key string) interface{}

Get return context in RequestContext

func (*RequestContext) Set

func (reqCtx *RequestContext) Set(key string, value interface{})

Set set value with specific key

type Resource

type Resource interface {
	Close()
}

Resource defines the interface that every resource must provide. Thread synchronization between Close() and IsClosed() is the responsibility of the caller.

type ResourcePool

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

ResourcePool allows you to use a pool of resources. ResourcePool允许你使用各种资源池,需要根据提供的factory创建特定的资源,比如连接

func NewResourcePool

func NewResourcePool(factory Factory, capacity, maxCap int, idleTimeout time.Duration) *ResourcePool

NewResourcePool creates a new ResourcePool pool. capacity is the number of possible resources in the pool: there can be up to 'capacity' of these at a given time. maxCap specifies the extent to which the pool can be resized in the future through the SetCapacity function. You cannot resize the pool beyond maxCap. If a resource is unused beyond idleTimeout, it's discarded. An idleTimeout of 0 means that there is no timeout. 创建一个资源池子,capacity是池子中可用资源数量 maxCap代表最大资源数量 超过设定空闲时间的连接会被丢弃 资源池会根据传入的factory进行具体资源的初始化,比如建立与mysql的连接

func (*ResourcePool) Active

func (rp *ResourcePool) Active() int64

Active returns the number of active (i.e. non-nil) resources either in the pool or claimed for use

func (*ResourcePool) Available

func (rp *ResourcePool) Available() int64

Available returns the number of currently unused and available resources.

func (*ResourcePool) Capacity

func (rp *ResourcePool) Capacity() int64

Capacity returns the capacity.

func (*ResourcePool) Close

func (rp *ResourcePool) Close()

Close empties the pool calling Close on all its resources. You can call Close while there are outstanding resources. It waits for all resources to be returned (Put). After a Close, Get is not allowed.

func (*ResourcePool) Get

func (rp *ResourcePool) Get(ctx context.Context) (resource Resource, err error)

Get will return the next available resource. If capacity has not been reached, it will create a new one using the factory. Otherwise, it will wait till the next resource becomes available or a timeout. A timeout of 0 is an indefinite wait. Get会返回下一个可用的资源 如果容量没有达到上线,它会根据factory创建一个新的资源,否则会一直等待直到资源可用或超时

func (*ResourcePool) IdleClosed

func (rp *ResourcePool) IdleClosed() int64

IdleClosed returns the count of resources closed due to idle timeout.

func (*ResourcePool) IdleTimeout

func (rp *ResourcePool) IdleTimeout() time.Duration

IdleTimeout returns the idle timeout.

func (*ResourcePool) InUse

func (rp *ResourcePool) InUse() int64

InUse returns the number of claimed resources from the pool

func (*ResourcePool) IsClosed

func (rp *ResourcePool) IsClosed() (closed bool)

IsClosed returns true if the resource pool is closed.

func (*ResourcePool) MaxCap

func (rp *ResourcePool) MaxCap() int64

MaxCap returns the max capacity.

func (*ResourcePool) Put

func (rp *ResourcePool) Put(resource Resource)

Put will return a resource to the pool. For every successful Get, a corresponding Put is required. If you no longer need a resource, you will need to call Put(nil) instead of returning the closed resource. The will eventually cause a new resource to be created in its place.

func (*ResourcePool) SetCapacity

func (rp *ResourcePool) SetCapacity(capacity int) error

SetCapacity changes the capacity of the pool. You can use it to shrink or expand, but not beyond the max capacity. If the change requires the pool to be shrunk, SetCapacity waits till the necessary number of resources are returned to the pool. A SetCapacity of 0 is equivalent to closing the ResourcePool.

func (*ResourcePool) SetIdleTimeout

func (rp *ResourcePool) SetIdleTimeout(idleTimeout time.Duration)

SetIdleTimeout sets the idle timeout. It can only be used if there was an idle timeout set when the pool was created.

func (*ResourcePool) StatsJSON

func (rp *ResourcePool) StatsJSON() string

StatsJSON returns the stats in JSON format.

func (*ResourcePool) WaitCount

func (rp *ResourcePool) WaitCount() int64

WaitCount returns the total number of waits.

func (*ResourcePool) WaitTime

func (rp *ResourcePool) WaitTime() time.Duration

WaitTime returns the total wait time.

type Task

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

Task means handle unit in time wheel

type TimeWheel

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

TimeWheel means time wheel

func NewTimeWheel

func NewTimeWheel(tick time.Duration, bucketsNum int) (*TimeWheel, error)

NewTimeWheel create new time wheel

func (*TimeWheel) Add

func (tw *TimeWheel) Add(delay time.Duration, key interface{}, callback func()) error

Add add an item into time wheel

func (*TimeWheel) Remove

func (tw *TimeWheel) Remove(key interface{}) error

Remove remove an item from time wheel

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start start the time wheel

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop stop the time wheel

Directories

Path Synopsis
Package cache implements a LRU cache.
Package cache implements a LRU cache.
Package sync2 provides extra functionality along the same lines as sync.
Package sync2 provides extra functionality along the same lines as sync.
Package timer provides various enhanced timer functions.
Package timer provides various enhanced timer functions.

Jump to

Keyboard shortcuts

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