utils

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 28 Imported by: 71

Documentation

Overview

Package utils contains utils.

Package utils .

Index

Constants

View Source
const (
	EnvConfDir  = "KITEX_CONF_DIR"
	EnvConfFile = "KITEX_CONF_FILE"
	EnvLogDir   = "KITEX_LOG_DIR"

	DefaultConfDir  = "conf"
	DefaultConfFile = "kitex.yml"
	DefaultLogDir   = "log"
)

Predefined env variables and default configurations.

View Source
const (
	EmptyJSON  = "{}"
	Comma      = ','
	Colon      = ':'
	DQuotation = '"'
	LeftBrace  = '{'
	RightBrace = '}'
)

const of json keyword char

View Source
const DefaultErrorSeparator = " | "

DefaultErrorSeparator is the separator for string representations of errors hold by ErrChain.

Variables

View Source
var ErrRingFull = errors.New("ring is full")

ErrRingFull means the ring is full.

Functions

func CalculateEventCost

func CalculateEventCost(rpcstats rpcinfo.RPCStats, start, end stats.Event) time.Duration

CalculateEventCost get events from rpcstats, and calculates the time duration of (end - start). It returns 0 when get nil rpcinfo event of either stats event.

func GetConfDir

func GetConfDir() string

GetConfDir gets dir of config file.

func GetConfFile

func GetConfFile() string

GetConfFile gets config file path.

func GetEnvLogDir added in v0.5.0

func GetEnvLogDir() string

GetEnvLogDir is to get log dir from env.

func GetFuncName

func GetFuncName(i interface{}) string

GetFuncName returns the function name of i.

func GetLogDir

func GetLogDir() string

GetLogDir gets dir of log file. Deprecated: it is suggested to use GetEnvLogDir instead of GetLogDir, and GetEnvLogDir won't return default log dir.

func GetUIntLen

func GetUIntLen(n uint64) int

GetUIntLen returns the length of the string representation of the given value n.

func JSONStr2Map

func JSONStr2Map(jsonStr string) (mapInfo map[string]string, err error)

JSONStr2Map transform json str to map[string]string, perf is better than use json lib directly

func Map2JSONStr

func Map2JSONStr(mapInfo map[string]string) (str string, err error)

Map2JSONStr transform map[string]string to json str, perf is better than use json lib directly

func MarshalError

func MarshalError(method string, err error) []byte

MarshalError convert go error to thrift exception, and encode exception over buffered binary transport.

func NewNetAddr

func NewNetAddr(network, address string) net.Addr

NewNetAddr creates a new NetAddr object with the network and address provided.

func SliceByteToString

func SliceByteToString(b []byte) string

SliceByteToString converts []byte to string without copy. DO NOT USE unless you know what you're doing.

func StringDeepCopy added in v0.6.0

func StringDeepCopy(s string) string

func StringToSliceByte

func StringToSliceByte(s string) []byte

StringToSliceByte converts string to []byte without copy. DO NOT USE unless you know what you're doing.

func UnmarshalError

func UnmarshalError(b []byte) error

UnmarshalError decode binary and return error message

func WriteInt64ToStringBuilder

func WriteInt64ToStringBuilder(sb *strings.Builder, value int64)

WriteInt64ToStringBuilder writes a int64 to a strings.Builder.

Types

type AtomicInt

type AtomicInt int32

AtomicInt is a shortcut for atomic.int32.

func (*AtomicInt) Dec

func (i *AtomicInt) Dec()

Dec subs 1 to the int.

func (*AtomicInt) Inc

func (i *AtomicInt) Inc()

Inc adds 1 to the int.

func (*AtomicInt) Value

func (i *AtomicInt) Value() int

Value returns the int.

type ErrChain

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

ErrChain is used to pack multiple errors. The zero value of ErrChain is ready to use.

func (*ErrChain) Append

func (e *ErrChain) Append(err error)

Append is not concurrency safe.

func (ErrChain) Error

func (e ErrChain) Error() string

Error implements the error interface.

func (ErrChain) HasError

func (e ErrChain) HasError() bool

HasError returns if there's error in the chain.

func (*ErrChain) UseSeparator

func (e *ErrChain) UseSeparator(sep string)

UseSeparator sets the separator of the current ErrChain instance.

type KitexArgs added in v0.4.0

type KitexArgs interface {
	GetFirstArgument() interface{}
}

KitexArgs is used for assert when get real request from XXXArgs. Thrift and KitexProtobuf will generate GetFirstArgument() interface{} for XXXArgs

type KitexResult added in v0.4.0

type KitexResult interface {
	GetResult() interface{}
	SetSuccess(interface{})
}

KitexResult is used for assert when get real response from XXXResult. Thrift and KitexProtobuf will generate the two functions for XXXResult.

type MaxCounter

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

MaxCounter is an integer counter with upper limit.

func NewMaxCounter

func NewMaxCounter(max int) *MaxCounter

NewMaxCounter returns a new MaxCounter.

func (*MaxCounter) Dec

func (cl *MaxCounter) Dec()

Dec decreases the counter by one.

func (*MaxCounter) DecN added in v0.4.3

func (cl *MaxCounter) DecN(n int64)

DecN decreases the counter by n.

func (*MaxCounter) Inc

func (cl *MaxCounter) Inc() bool

Inc increases the counter by one and returns if the operation succeeds.

func (*MaxCounter) Now added in v0.4.3

func (cl *MaxCounter) Now() int64

type NetAddr

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

NetAddr implements the net.Addr interface.

func (*NetAddr) Network

func (na *NetAddr) Network() string

Network implements the net.Addr interface.

func (*NetAddr) String

func (na *NetAddr) String() string

String implements the net.Addr interface.

type Ring

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

Ring implements a fixed size hash list to manage data

func NewRing

func NewRing(size int) *Ring

NewRing creates a ringbuffer with fixed size.

func (*Ring) Dump

func (r *Ring) Dump() interface{}

Dump dumps the data in the ring.

func (*Ring) Pop

func (r *Ring) Pop() interface{}

Pop returns the last item and removes it from the ring.

func (*Ring) Push

func (r *Ring) Push(obj interface{}) error

Push appends item to the ring.

type SharedTicker added in v0.5.0

type SharedTicker struct {
	sync.Mutex

	Interval time.Duration
	// contains filtered or unexported fields
}

func NewSharedTicker added in v0.5.0

func NewSharedTicker(interval time.Duration) *SharedTicker

NewSharedTicker constructs a SharedTicker with specified interval.

func (*SharedTicker) Add added in v0.5.0

func (t *SharedTicker) Add(b TickerTask)

func (*SharedTicker) Closed added in v0.5.0

func (t *SharedTicker) Closed() bool

func (*SharedTicker) Delete added in v0.5.0

func (t *SharedTicker) Delete(b TickerTask)

func (*SharedTicker) Tick added in v0.5.0

func (t *SharedTicker) Tick(interval time.Duration)

type Slice

type Slice []interface{}

Slice is an abstraction of []interface{}.

func (*Slice) Pop

func (s *Slice) Pop() (res interface{})

Pop pops the last element from the slice.

func (*Slice) Push

func (s *Slice) Push(any interface{})

Push pushes an interface to the slice.

type StringBuilder added in v0.7.2

type StringBuilder struct {
	sync.Mutex
	// contains filtered or unexported fields
}

StringBuilder is a concurrently safe wrapper for strings.Builder.

func (*StringBuilder) Cap added in v0.7.2

func (b *StringBuilder) Cap() int

func (*StringBuilder) Grow added in v0.7.2

func (b *StringBuilder) Grow(n int)

func (*StringBuilder) Len added in v0.7.2

func (b *StringBuilder) Len() int

func (*StringBuilder) RawStringBuilder added in v0.7.2

func (b *StringBuilder) RawStringBuilder() *strings.Builder

RawStringBuilder returns the inner strings.Builder of StringBuilder. It allows users to perform lock-free operations in scenarios without concurrency issues, thereby improving performance.

func (*StringBuilder) Reset added in v0.7.2

func (b *StringBuilder) Reset()

func (*StringBuilder) String added in v0.7.2

func (b *StringBuilder) String() string

func (*StringBuilder) WithLocked added in v0.7.2

func (b *StringBuilder) WithLocked(f func(sb *strings.Builder) error) error

WithLocked encapsulates a concurrent-safe interface for batch operations on strings.Builder. Please note that you should avoid calling member functions of StringBuilder within the input function, as it may lead to program deadlock.

func (*StringBuilder) Write added in v0.7.2

func (b *StringBuilder) Write(p []byte) (int, error)

func (*StringBuilder) WriteByte added in v0.7.2

func (b *StringBuilder) WriteByte(c byte) error

func (*StringBuilder) WriteRune added in v0.7.2

func (b *StringBuilder) WriteRune(r rune) (int, error)

func (*StringBuilder) WriteString added in v0.7.2

func (b *StringBuilder) WriteString(s string) (int, error)

type ThriftMessageCodec

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

ThriftMessageCodec is used to codec thrift messages.

func NewThriftMessageCodec

func NewThriftMessageCodec() *ThriftMessageCodec

NewThriftMessageCodec creates a new ThriftMessageCodec.

func (*ThriftMessageCodec) Decode

func (t *ThriftMessageCodec) Decode(b []byte, msg thrift.TStruct) (method string, seqID int32, err error)

Decode do thrift message decode, notice: msg must be XXXArgs/XXXResult that the wrap struct for args and result, not the actual args or result

func (*ThriftMessageCodec) Deserialize

func (t *ThriftMessageCodec) Deserialize(msg thrift.TStruct, b []byte) (err error)

Deserialize deserialize bytes into message. This is normal thrift deserialize func. Notice: Binary generic use Decode instead of Deserialize.

func (*ThriftMessageCodec) Encode

func (t *ThriftMessageCodec) Encode(method string, msgType thrift.TMessageType, seqID int32, msg thrift.TStruct) (b []byte, err error)

Encode do thrift message encode. Notice! msg must be XXXArgs/XXXResult that the wrap struct for args and result, not the actual args or result Notice! seqID will be reset in kitex if the buffer is used for generic call in client side, set seqID=0 is suggested when you call this method as client.

func (*ThriftMessageCodec) Serialize

func (t *ThriftMessageCodec) Serialize(msg thrift.TStruct) (b []byte, err error)

Serialize serialize message into bytes. This is normal thrift serialize func. Notice: Binary generic use Encode instead of Serialize.

type TickerTask added in v0.5.0

type TickerTask interface {
	Tick()
}

type YamlConfig

type YamlConfig struct {
	configutil.RichTypeConfig
	// contains filtered or unexported fields
}

YamlConfig contains configurations from yaml file.

func ReadYamlConfigFile

func ReadYamlConfigFile(yamlFile string) (*YamlConfig, error)

ReadYamlConfigFile creates a YamlConfig from the file.

func (*YamlConfig) Get

func (yc *YamlConfig) Get(key string) (interface{}, bool)

Get implements the configutil.Config interface.

func (*YamlConfig) GetDuration

func (yc *YamlConfig) GetDuration(key string) (time.Duration, bool)

GetDuration implements the configutil.RichTypeConfig interface. It converts string item to time.Duration.

Directories

Path Synopsis
Package kitexutil provides some util methods to get RPC information
Package kitexutil provides some util methods to get RPC information

Jump to

Keyboard shortcuts

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