utils

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EnableKubeClient = false
)
View Source
var (
	KubeClient kubernetes.Interface
)
View Source
var SHARD_COUNT = 32

Functions

func CalculateCpuAndMem

func CalculateCpuAndMem(pod *corev1.Pod) (float64, float64)

计算 Pod 的 CPU 和 mem

func ClientIP

func ClientIP(r *http.Request) string

ClientIP 尽最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func CloneStringMap

func CloneStringMap(src map[string]string) map[string]string

CloneStringMap copy src to dst of type map[string]string

func ConvertNil

func ConvertNil(value string) string

func Dumps

func Dumps(v interface{}) string

Dumps dump interface as indented json object

func DumpsEventBasic

func DumpsEventBasic(event *audit.Event) string

DumpsEventBasic will remove some info from pod spec to reduce log size and sensitive data to be leaked

func DumpsEventKeyInfo

func DumpsEventKeyInfo(event *audit.Event) string

func FormatTime

func FormatTime(t time.Time) string

FormatTime format time to UTC time and with million seconds

func GetClientFromFile

func GetClientFromFile(masterUrl, kubeConfigFile string, qps float32, burst int) (kubernetes.Interface, error)

GetClientFromFile create a k8s client from config file

func GetClientFromIncluster

func GetClientFromIncluster(qps float32, burst int) (clientset.Interface, error)

func GetMetaName

func GetMetaName(dest interface{}) (string, string, string, error)

获取数据的 SqlTableName EsTableName TypeName

func GetPersistentVolumeClaimNamesFromPod

func GetPersistentVolumeClaimNamesFromPod(pod *corev1.Pod) []string

获取pod中所有pvc的资源名列表

func GetPulledImageFromEventMessage

func GetPulledImageFromEventMessage(s string) string

GetPulledImageFromEventMessage get image name from pulled event

func GetPullingImageFromEventMessage

func GetPullingImageFromEventMessage(s string) string

GetPullingImageFromEventMessage get image name from pulling event

func GoID

func GoID() int

func HandleCrash

func HandleCrash()

func IgnorePanic

func IgnorePanic(desc string)

func InAnnotations

func InAnnotations(annotations map[string]string, k string) bool

func InFinalizers

func InFinalizers(finalizers []string, k string) bool

func InitKube

func InitKube(kubeConfigPath string) error

func Marshal

func Marshal(v interface{}) string

Marshal Marshal object to string of the error message if failed

func MergeMaps

func MergeMaps(mapTo, mapFrom map[string]string)

func ParseJSONPath

func ParseJSONPath(input interface{}, name, template string) (string, error)

func ParseTags

func ParseTags(simpleTags []string, jsonTags []string) (map[string]string, error)

func ParseTime

func ParseTime(s string) (time.Time, error)

ParseTime parse string into time using layout "2006-01-02T15:04:05.000Z"

func RandString

func RandString(n int) string

RandString generate n-length random string

func ReTry

func ReTry(f func() error, interval time.Duration, retryCount int) (err error)

ReTry try to call a function until success.

func SetBoolParam

func SetBoolParam(values url.Values, name string, f *bool)

func SetStringParam

func SetStringParam(values url.Values, name string, f *string)

func SetTimeLayoutParam

func SetTimeLayoutParam(values url.Values, name string, f *time.Time)

func SetTimeParam

func SetTimeParam(values url.Values, name string, f *time.Time)

func ShouldIgnoreNamespace

func ShouldIgnoreNamespace(ns string) bool

ShouldIgnoreNamespace return true is namespace should be omit

func SliceContainsString

func SliceContainsString(arr []string, s string) bool

func StringHashcode

func StringHashcode(s string) int

func TimeDiffInMilliMinutes

func TimeDiffInMilliMinutes(start, end time.Time) float64

TimeDiffInMilliMinutes return time difference in minutes

func TimeDiffInMilliSeconds

func TimeDiffInMilliSeconds(start, end time.Time) float64

func TimeDiffInSeconds

func TimeDiffInSeconds(start, end time.Time) float64

TimeDiffInSeconds get time difference in unit second.

func TimeSinceInMilliSeconds

func TimeSinceInMilliSeconds(start time.Time) float64

func TimeSinceInMinutes

func TimeSinceInMinutes(start time.Time) float64

TimeSinceInMinutes return minutes since start

func TimeSinceInSeconds

func TimeSinceInSeconds(start time.Time) float64

func Warnf

func Warnf(format string, args ...interface{})

Types

type BufferData

type BufferData struct {
	sync.WaitGroup
	// contains filtered or unexported fields
}

func NewBufferData

func NewBufferData(sequential bool) *BufferData

type BufferUtils

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

func NewBufferUtils

func NewBufferUtils(name string, bufferSize int, clearPeriod time.Duration, sequential bool, saveFunc func(map[string]interface{}) error) *BufferUtils

func (*BufferUtils) DoClearData

func (b *BufferUtils) DoClearData()

func (*BufferUtils) SaveData

func (b *BufferUtils) SaveData(id string, data interface{}) error

func (*BufferUtils) Stop

func (b *BufferUtils) Stop()

wait to clear data when get stop signal

type ConcurrentMap

type ConcurrentMap []*ConcurrentMapShared

A "thread" safe map of type string:Anything. To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.

func New

func New() ConcurrentMap

Creates a new concurrent map.

func (ConcurrentMap) Clear

func (m ConcurrentMap) Clear()

Clear removes all items from map.

func (ConcurrentMap) Count

func (m ConcurrentMap) Count() int

Count returns the number of elements within the map.

func (ConcurrentMap) Get

func (m ConcurrentMap) Get(key string) (interface{}, bool)

Get retrieves an element from map under given key.

func (ConcurrentMap) GetShard

func (m ConcurrentMap) GetShard(key string) *ConcurrentMapShared

GetShard returns shard under given key

func (ConcurrentMap) Has

func (m ConcurrentMap) Has(key string) bool

Looks up an item under specified key

func (ConcurrentMap) IsEmpty

func (m ConcurrentMap) IsEmpty() bool

IsEmpty checks if map is empty.

func (ConcurrentMap) Items

func (m ConcurrentMap) Items() map[string]interface{}

Items returns all items as map[string]interface{}

func (ConcurrentMap) Iter deprecated

func (m ConcurrentMap) Iter() <-chan Tuple

Iter returns an iterator which could be used in a for range loop.

Deprecated: using IterBuffered() will get a better performence

func (ConcurrentMap) IterBuffered

func (m ConcurrentMap) IterBuffered() <-chan Tuple

IterBuffered returns a buffered iterator which could be used in a for range loop.

func (ConcurrentMap) IterCb

func (m ConcurrentMap) IterCb(fn IterCb)

Callback based iterator, cheapest way to read all elements in a map.

func (ConcurrentMap) IterCbConcurrent

func (m ConcurrentMap) IterCbConcurrent(fn IterCb)

Callback based iterator, cheapest way to read all elements in a map.

func (ConcurrentMap) Keys

func (m ConcurrentMap) Keys() []string

Keys returns all keys as []string

func (ConcurrentMap) MSet

func (m ConcurrentMap) MSet(data map[string]interface{})

func (ConcurrentMap) MarshalJSON

func (m ConcurrentMap) MarshalJSON() ([]byte, error)

Reviles ConcurrentMap "private" variables to json marshal.

func (ConcurrentMap) Pop

func (m ConcurrentMap) Pop(key string) (v interface{}, exists bool)

Pop removes an element from the map and returns it

func (ConcurrentMap) Remove

func (m ConcurrentMap) Remove(key string)

Remove removes an element from the map.

func (ConcurrentMap) RemoveCb

func (m ConcurrentMap) RemoveCb(key string, cb RemoveCb) bool

RemoveCb locks the shard containing the key, retrieves its current value and calls the callback with those params If callback returns true and element exists, it will remove it from the map Returns the value returned by the callback (even if element was not present in the map)

func (ConcurrentMap) Set

func (m ConcurrentMap) Set(key string, value interface{})

Sets the given value under the specified key.

func (ConcurrentMap) SetIfAbsent

func (m ConcurrentMap) SetIfAbsent(key string, value interface{}) bool

Sets the given value under the specified key if no value was associated with it.

func (ConcurrentMap) Upsert

func (m ConcurrentMap) Upsert(key string, value interface{}, cb UpsertCb) (res interface{})

Insert or Update - updates existing element or inserts a new one using UpsertCb

type ConcurrentMapShared

type ConcurrentMapShared struct {
	sync.RWMutex // Read Write mutex, guards access to internal map.
	// contains filtered or unexported fields
}

A "thread" safe string to anything map.

type EsTabler

type EsTabler interface {
	EsTableName() string
}

type ExpiringMap

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

func NewExpiringMap

func NewExpiringMap(timeout time.Duration) *ExpiringMap

func (*ExpiringMap) Delete

func (em *ExpiringMap) Delete(key string)

func (*ExpiringMap) Get

func (em *ExpiringMap) Get(key string) (interface{}, bool)

func (*ExpiringMap) Set

func (em *ExpiringMap) Set(key string, value interface{})

type FlowControl

type FlowControl struct {
	WindowMaxSize        int     // 窗口大小
	FastBackoffThreshold int     // 快速退避错误阈值
	FastBackoffFactor    float64 // 快速退避因子
	SlowRecoverFactor    float64 // 慢恢复因子
	MaxFlowSize          float64 // 最大流量值
	// contains filtered or unexported fields
}

func NewFlowControl

func NewFlowControl(wms int, fbt int, fbf float64, srf float64, mfs float64) *FlowControl

func (*FlowControl) DecideFlow

func (f *FlowControl) DecideFlow(desire float64) float64

func (*FlowControl) RecordFlow

func (f *FlowControl) RecordFlow(flowSuccess bool)

type Item

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

expired map

type IterCb

type IterCb func(key string, v interface{})

Iterator callback,called for every key,value found in maps. RLock is held for all calls for a given shard therefore callback sess consistent view of a shard, but not across the shards

type JsoniterDecoder

type JsoniterDecoder struct{}

func (*JsoniterDecoder) Decode

func (u *JsoniterDecoder) Decode(data []byte, v interface{}) error

Decode decodes with json.Unmarshal from the Go standard library.

type LRU

type LRU interface {
	Get(key string) interface{}
	Put(key string, value interface{})
}

func LRUCache

func LRUCache(name string, capacity int) LRU

type RemoveCb

type RemoveCb func(key string, v interface{}, exists bool) bool

RemoveCb is a callback executed in a map.RemoveCb() call, while Lock is held If returns true, the element will be removed from the map

type SafeMap

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

func NewSafeMap

func NewSafeMap() *SafeMap

func (*SafeMap) Delete

func (smap *SafeMap) Delete(key string)

func (*SafeMap) Get

func (smap *SafeMap) Get(key string) (interface{}, bool)

func (*SafeMap) IterateWithFunc

func (smap *SafeMap) IterateWithFunc(f func(interface{}))

IterateWithFunc iterate map

func (*SafeMap) Set

func (smap *SafeMap) Set(key string, data interface{})

func (*SafeMap) Size

func (smap *SafeMap) Size() int

type Tabler

type Tabler interface {
	TableName() string
}

type Tuple

type Tuple struct {
	Key string
	Val interface{}
}

Used by the Iter & IterBuffered functions to wrap two variables together over a channel,

type Typer

type Typer interface {
	TypeName() string
}

type UpsertCb

type UpsertCb func(exist bool, valueInMap interface{}, newValue interface{}) interface{}

Callback to return new element to be inserted into the map It is called while lock is held, therefore it MUST NOT try to access other keys in same map, as it can lead to deadlock since Go sync.RWLock is not reentrant

Jump to

Keyboard shortcuts

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