helper

package
v0.0.0-...-52087c1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VAL_TYPE_DEFAULT             = 0x0
	VAL_TYPE_INT                 = 0x1
	VAL_TYPE_STRING              = 0x2
	VAL_TYPE_INTERFACE_ARRAY     = 0x3
	VAL_TYPE_STRING_TO_INTERFACE = 0x4
	VAL_TYPE_JSON_NUMBER         = 0x5 //float64
	VAL_TYPE_FLOAT64             = 0x5
)

Variables

View Source
var (
	ErrTreeNotFoundParent = errors.New("not found parent node")
)

Functions

func Abs

func Abs(x int) int

func ArrayFilter

func ArrayFilter[T any](arr []T, f func(T) bool) []T

Pointer type is recommended

func ArrayMap

func ArrayMap[T any, R any](arr []T, f func(T) R) []R

func GetAvaliablePort

func GetAvaliablePort() (int, error)

func GetCPUInfo

func GetCPUInfo() (*[]CPUInfo, error)

func GetCPUUsage

func GetCPUUsage() ([]float64, error)

func GetCPUUsageTotal

func GetCPUUsageTotal() (float64, error)

func GetCaptchaMethodKey

func GetCaptchaMethodKey(otype string, method string) string

根据验证码类型与操作名获取key

func GetConfigInteger

func GetConfigInteger(name string) int

func GetConfigString

func GetConfigString(name string) string

func GetContext

func GetContext() c.Context

func GetDiskUsage

func GetDiskUsage() (float64, uint64, uint64, error)

func GetInterfaceType

func GetInterfaceType(i interface{}) int

func GetMaxContainer

func GetMaxContainer() int

func GetMemUsage

func GetMemUsage() (float64, uint64, uint64, error)

ret:

  1. percent of usage, 2. total memory, 3. used memory, 4. error

func GetNetUsage

func GetNetUsage() (uint64, uint64, error)

ret: 1. recv bytes, 2. sent bytes, 3. error

func GetNetUsagePercent

func GetNetUsagePercent() (float64, float64, error)

func GetProcessInfo

func GetProcessInfo() (*[]ProcessInfo, error)

func HandleSensitiveString

func HandleSensitiveString(src string) string

func Hash2Int

func Hash2Int(src string) int

func HttpGet

func HttpGet(url string, data interface{}) (int, string, error)

https request get, data is a struct which will be converted to urlencoded query string example:

type User struct {
	Name string `json:"name"`
}

httpsGet("http://www.example.com?name=hello", &User{})

func HttpPayloadMultipartFile

func HttpPayloadMultipartFile(filename string, file io.Reader) httpPayloadMultipartFile

func HttpPostJson

func HttpPostJson(url string, data interface{}) (int, string, error)

https request post json data

func InitServerConfig

func InitServerConfig()

func Md5

func Md5(src string) string

func ParseIpFromStr

func ParseIpFromStr(ip string) int

根据ip字符串返回ip的int形式,对于ipv6,映射到ipv4上

func Random

func Random(min int, max int) int

func RandomStr

func RandomStr(len int) string

func SendAndParse

func SendAndParse[T any](method string, url string, options ...HttpOptions) (T, error)

func SendGetAndParse

func SendGetAndParse[T any](url string, options ...HttpOptions) (T, error)

func SendGetAsync

func SendGetAsync(url string, final func(io.ReadCloser, *ResponseHeader, error), options ...HttpOptions)

SendGetAsync is not a real async function, it only open the connection and pass the reader to user, to close the connection, the final function should be passed, and user will not be nervous about the connection leak

func SendPostAndParse

func SendPostAndParse[T any](url string, options ...HttpOptions) (T, error)

func SendPostAsync

func SendPostAsync(url string, final func(io.ReadCloser, *ResponseHeader, error), options ...HttpOptions)

SendPostAsync is not a real async function, it only open the connection and pass the reader to user, to close the connection, the final function should be passed, and user will not be nervous about the connection leak

func StringJoin

func StringJoin(strs ...string) string

func TimeoutWrapper

func TimeoutWrapper(timeout time.Duration, success_chan chan bool) bool

TimeoutWrapper is a wrapper for timeout, which will return true if timeout, if not, return false success_chan is required to be passed to infer that the task is done

Types

type CPUInfo

type CPUInfo struct {
	ModelName string  `json:"model_name"`
	Cores     int32   `json:"cores"`
	Mhz       float64 `json:"mhz"`
}

type Compareable

type Compareable interface {
	int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		string
}

type HighGranularityMutex

type HighGranularityMutex[K Compareable] struct {
	// contains filtered or unexported fields
}

func NewHighGranularityMutex

func NewHighGranularityMutex[K Compareable]() *HighGranularityMutex[K]

func (*HighGranularityMutex[K]) Lock

func (c *HighGranularityMutex[K]) Lock(id K)

func (*HighGranularityMutex[K]) Unlock

func (c *HighGranularityMutex[K]) Unlock(id K)

type HttpOptions

type HttpOptions struct {
	Type  string
	Value interface{}
}

func HttpHeader

func HttpHeader(header map[string]string) HttpOptions

func HttpNoRedirect

func HttpNoRedirect() HttpOptions

func HttpParams

func HttpParams(params map[string]string) HttpOptions

which is used for params with in url

func HttpPayload

func HttpPayload(payload map[string]string) HttpOptions

which is used for POST method only

func HttpPayloadJson

func HttpPayloadJson(payload interface{}) HttpOptions

which is used for POST method only

func HttpPayloadText

func HttpPayloadText(payload string) HttpOptions

which is used for POST method only

func HttpProxy

func HttpProxy(proxy string, user string, password string) HttpOptions

func HttpPyloadMultipart

func HttpPyloadMultipart(payload map[string]string, files ...httpPayloadMultipartFile) HttpOptions

func HttpTimeout

func HttpTimeout(timeout int64) HttpOptions

milliseconds

func HttpWithDirectReferer

func HttpWithDirectReferer() HttpOptions

func HttpWithRandomUA

func HttpWithRandomUA() HttpOptions

func HttpWithRetCode

func HttpWithRetCode(retCode *int) HttpOptions

type ProcessInfo

type ProcessInfo struct {
	Pid     int32   `json:"pid"`
	Name    string  `json:"name"`
	User    string  `json:"user"`
	CPU     float64 `json:"cpu"`
	Memory  uint64  `json:"memory"`
	MemoryP float64 `json:"memory_percent"`
}

type ResponseHeader

type ResponseHeader map[string]string

func NewResponseHeader

func NewResponseHeader(header http.Header) *ResponseHeader

func SendDelete

func SendDelete(url string, options ...HttpOptions) ([]byte, *ResponseHeader, error)

func SendGet

func SendGet(url string, options ...HttpOptions) ([]byte, *ResponseHeader, error)

func SendPost

func SendPost(url string, options ...HttpOptions) ([]byte, *ResponseHeader, error)

func SendPut

func SendPut(url string, options ...HttpOptions) ([]byte, *ResponseHeader, error)

func (*ResponseHeader) FindContentEncoding

func (r *ResponseHeader) FindContentEncoding() string

func (*ResponseHeader) FindContentLength

func (r *ResponseHeader) FindContentLength() int64

func (*ResponseHeader) FindContentType

func (r *ResponseHeader) FindContentType() string

func (*ResponseHeader) FindKey

func (r *ResponseHeader) FindKey(key string) string

func (*ResponseHeader) ToString

func (r *ResponseHeader) ToString() string

type Tree

type Tree[K Compareable, V any] struct {
	// contains filtered or unexported fields
}

Not thread safety

func NewTree

func NewTree[K, V Compareable]() *Tree[K, V]

func (*Tree[K, V]) AddNode

func (t *Tree[K, V]) AddNode(key K, value *V)

add node

func (*Tree[K, V]) AddParent

func (t *Tree[K, V]) AddParent(key K, value *V)

add parent if you want to add parent to a relative root node

func (*Tree[K, V]) AddToParent

func (t *Tree[K, V]) AddToParent(parent_id K, key K, value *V) error

add to parent

func (*Tree[K, V]) CheckIfParent

func (t *Tree[K, V]) CheckIfParent(parent_id K, client_id K) bool

check if the node holds parent_id as key is parent of node holds client_id as key

func (*Tree[K, V]) GetAllChildren

func (t *Tree[K, V]) GetAllChildren() map[K]*V

func (*Tree[K, V]) GetNode

func (t *Tree[K, V]) GetNode(id K) *Tree[K, V]

func (*Tree[K, V]) Remove

func (t *Tree[K, V]) Remove(key K)

remove key from children

func (*Tree[K, V]) WalkReverse

func (t *Tree[K, V]) WalkReverse(cb func(K, *V))

Walk tree from bottom leaf to root

Jump to

Keyboard shortcuts

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