utils

package
v0.0.0-...-2f8f7b3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

一致性hash算法,初始化n个虚拟节点,然后将真实的服务器节点包装成虚拟节点,每个真实节点对应100个虚拟节点 根据虚拟节点获取提供服务的真实节点 ServerID_Index

Index

Constants

View Source
const VNODE_NUMBER = 100

----------------------------------------------------------------------------------

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	Epoch int64 = 1288834974657
	// NodeBits holds the number of bits to use for Node
	NodeBits uint8 = 10
	// StepBits holds the number of bits to use for Step
	StepBits uint8 = 12
)
View Source
var File file

========================================================================

View Source
var (
	Separator = string(filepath.Separator)
)
View Source
var SnowFlake *snowFlake

============================================================================

Functions

func DayTimeZero

func DayTimeZero() int64

当天零点时间戳

func DayTimeZero2

func DayTimeZero2() int64

func GenUID

func GenUID(serverID uint64) uint64

基于服务器生成唯一ID

func GetDiffDays

func GetDiffDays(t1 int64, t2 int64) int

两个时间戳相差天数

func GetGUID

func GetGUID() uint64

func GetLocalIPV4

func GetLocalIPV4() string

func GoID

func GoID() uint64

get go routine id

func IsEmpty

func IsEmpty(value interface{}) bool

IsEmpty checks whether `value` is empty.

func IsFloat

func IsFloat(value interface{}) bool

IsFloat checks whether `value` is type of float.

func IsInt

func IsInt(value interface{}) bool

IsInt checks whether `value` is type of int.

func IsMap

func IsMap(value interface{}) bool

IsMap checks whether `value` is type of map.

func IsNil

func IsNil(value interface{}) bool

IsNil checks whether `value` is nil.

func IsSameDay

func IsSameDay(t1 int64, t2 int64) bool

判断两个时间是否是同一天

func IsSlice

func IsSlice(value interface{}) bool

IsSlice checks whether `value` is type of slice.

func IsStruct

func IsStruct(value interface{}) bool

IsStruct checks whether `value` is type of struct.

func IsUint

func IsUint(value interface{}) bool

IsUint checks whether `value` is type of uint.

func MonthStartEnd

func MonthStartEnd() (int64, int64)

当月开始时间戳

func NewSnowFlake

func NewSnowFlake(node int64) *snowFlake

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func NewSpinLock

func NewSpinLock() sync.Locker

func Str2Time

func Str2Time(timeStr string) (time.Time, error)

时间字符串-》时间对象

func Str2UnixTime

func Str2UnixTime(timeStr string) (int64, error)

时间字符串-》时间戳

func Time2Str

func Time2Str(t int64) string

时间戳-》时间字符串

func Time2TimeObj

func Time2TimeObj(t int64) time.Time

时间戳-》时间对象

func TimeMillisecond

func TimeMillisecond() int64

当前毫秒数

func TimeSecond

func TimeSecond() int64

当前时间戳

func UniqID

func UniqID() string

func WeekStartEnd

func WeekStartEnd() (int64, int64)

当周零点时间戳(周一作为一周第一天)

func YeadAndWeek

func YeadAndWeek() (y, w int)

Types

type ConsistentHash

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

func NewConsistentHash

func NewConsistentHash() *ConsistentHash

func (*ConsistentHash) Delete

func (c *ConsistentHash) Delete(serverID int)

func (*ConsistentHash) Get

func (c *ConsistentHash) Get(key string) int

获取某个服务

func (*ConsistentHash) Insert

func (c *ConsistentHash) Insert(serverID int)

type IObjPool

type IObjPool interface {
	GetObj() interface{}
	FreeObj(obj interface{})
	UseCount() int
	Count() int
	Clear()
}

type ObjPool

type ObjPool struct {
	New func() interface{}
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewObjPool

func NewObjPool(newFunc func() interface{}, defaultCount int, incrStep int) *ObjPool

func (*ObjPool) Clear

func (p *ObjPool) Clear()

func (*ObjPool) Count

func (p *ObjPool) Count() int

func (*ObjPool) FreeObj

func (p *ObjPool) FreeObj(obj interface{})

func (*ObjPool) GetObj

func (p *ObjPool) GetObj() interface{}

func (*ObjPool) UseCount

func (p *ObjPool) UseCount() int

type ObjService

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

func NewObjService

func NewObjService(rep interface{}) *ObjService

func (*ObjService) Call

func (s *ObjService) Call(methodName string, args ...interface{})

type Random

type Random struct {
	*rand.Rand
}
var GRand *Random = NewRandom()

==========================================================================

func NewRandom

func NewRandom() *Random

func (*Random) RandInt

func (r *Random) RandInt(a, b int) int

func (*Random) RandIntn

func (r *Random) RandIntn(num int) int

func (*Random) Shuffle

func (r *Random) Shuffle(arr []interface{})

type SortKeys

type SortKeys []uint32

func (SortKeys) Len

func (s SortKeys) Len() int

func (SortKeys) Less

func (s SortKeys) Less(i, j int) bool

func (SortKeys) Swap

func (s SortKeys) Swap(i, j int)

type UniqueCode

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

func NewUniqueCode

func NewUniqueCode() *UniqueCode

func (*UniqueCode) NewUniqID

func (u *UniqueCode) NewUniqID() string

type VirtualNode

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

定义虚拟节点

func NewVNode

func NewVNode(serverID int, index int) *VirtualNode

func (*VirtualNode) GetHash

func (v *VirtualNode) GetHash() uint32

func (*VirtualNode) Index

func (v *VirtualNode) Index() int

func (*VirtualNode) ServerID

func (v *VirtualNode) ServerID() int

Jump to

Keyboard shortcuts

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