util

package
v0.0.0-...-ee7ecfe Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

通用的限流器(基于线程并发数)

Index

Constants

View Source
const (
	VariantNCS = iota
	VariantRFC4122
	VariantMicrosoft
	VariantFuture
)

UUID layout variants.

View Source
const (
	DomainPerson = iota
	DomainGroup
	DomainOrg
)

UUID DCE domains.

Variables

View Source
var (
	NamespaceDNS, _  = FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	NamespaceURL, _  = FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
	NamespaceOID, _  = FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
	NamespaceX500, _ = FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)

Predefined namespace UUIDs.

View Source
var Nil = UUID{}

The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.

Functions

func Equal

func Equal(u1 UUID, u2 UUID) bool

Equal returns true if u1 and u2 equals, otherwise returns false.

func GetPid

func GetPid() uint64

获取携程id

func RandHit

func RandHit(b1, b2 int32) bool

随机计算 方法调用者随机权重值为b1,权重池为b2 返回调用者的命中情况:true命中 false未命中 此方法不适用于有自定义的起始区域的数值命中

func RandInterval

func RandInterval(b1, b2 int32) int32

随机从[b1,b2]中获取一个数值

func RandIntervalN

func RandIntervalN(b1, b2 int32, n uint32) []int32

随机从[b1,b2]中获取n个数值

func StartProfile

func StartProfile(cpuprofile, memprofile string)

func StopProfile

func StopProfile()

Types

type NullUUID

type NullUUID struct {
	UUID  UUID
	Valid bool
}

NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database

func (*NullUUID) Scan

func (u *NullUUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (NullUUID) Value

func (u NullUUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type Semaphore

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

func New

func New(permits int) *Semaphore

构建一个新的并发限流器

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

获取许可 如果不可用,会一直block直到可用

func (*Semaphore) AcquireContext

func (s *Semaphore) AcquireContext(ctx context.Context, n int) bool

获取许可,和AcquireMany不同的是,增加了context.Context,方便上下文同步 使用场景父子进程上下文 返回true则获取成功,false则失败

func (*Semaphore) AcquireMany

func (s *Semaphore) AcquireMany(n int)

获取许可,和Acquire不同的是,可以一次获取n个许可 如果不可用,会一直block直到可用

func (*Semaphore) AcquireWithin

func (s *Semaphore) AcquireWithin(n int, d time.Duration) bool

获取许可,和AcquireMany不同的是,不会一直block,增加了超时时间d 返回true则获取成功,false则超时失败

func (*Semaphore) AvailablePermits

func (s *Semaphore) AvailablePermits() int

获取当前可用的许可数量

func (*Semaphore) DrainPermits

func (s *Semaphore) DrainPermits() int

获取当前剩余的全部许可

func (*Semaphore) Release

func (s *Semaphore) Release()

释放一个权限 如果不可用,会一直block直到可用

func (*Semaphore) ReleaseMany

func (s *Semaphore) ReleaseMany(n int)

释放多个权限 如果不可用,会一直block直到可用

type UUID

type UUID [16]byte

UUID representation compliant with specification described in RFC 4122.

func And

func And(u1 UUID, u2 UUID) UUID

And returns result of binary AND of two UUIDs.

func FromBytes

func FromBytes(input []byte) (u UUID, err error)

FromBytes returns UUID converted from raw byte slice input. It will return error if the slice isn't 16 bytes long.

func FromBytesOrNil

func FromBytesOrNil(input []byte) UUID

FromBytesOrNil returns UUID converted from raw byte slice input. Same behavior as FromBytes, but returns a Nil UUID on error.

func FromString

func FromString(input string) (u UUID, err error)

FromString returns UUID parsed from string input. Input is expected in a form accepted by UnmarshalText.

func FromStringOrNil

func FromStringOrNil(input string) UUID

FromStringOrNil returns UUID parsed from string input. Same behavior as FromString, but returns a Nil UUID on error.

func NewV1

func NewV1() UUID

NewV1 returns UUID based on current timestamp and MAC address.

func NewV2

func NewV2(domain byte) UUID

NewV2 returns DCE Security UUID based on POSIX UID/GID.

func NewV3

func NewV3(ns UUID, name string) UUID

NewV3 returns UUID based on MD5 hash of namespace UUID and name.

func NewV4

func NewV4() UUID

NewV4 returns random generated UUID.

func NewV5

func NewV5(ns UUID, name string) UUID

NewV5 returns UUID based on SHA-1 hash of namespace UUID and name.

func Or

func Or(u1 UUID, u2 UUID) UUID

Or returns result of binary OR of two UUIDs.

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns bytes slice representation of UUID.

func (UUID) MarshalBinary

func (u UUID) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (UUID) MarshalText

func (u UUID) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.

func (*UUID) Scan

func (u *UUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface. A 16-byte slice is handled by UnmarshalBinary, while a longer byte slice or a string is handled by UnmarshalText.

func (*UUID) SetVariant

func (u *UUID) SetVariant()

SetVariant sets variant bits as described in RFC 4122.

func (*UUID) SetVersion

func (u *UUID) SetVersion(v byte)

SetVersion sets version bits.

func (UUID) String

func (u UUID) String() string

Returns canonical string representation of UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

func (UUID) ToString

func (uuid UUID) ToString() string

uuid toString

func (*UUID) UnmarshalBinary

func (u *UUID) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long.

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported: "6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

func (UUID) Variant

func (u UUID) Variant() uint

Variant returns UUID layout variant.

func (UUID) Version

func (u UUID) Version() uint

Version returns algorithm version used to generate UUID.

Jump to

Keyboard shortcuts

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