utee

package module
v1.4.16 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 31 Imported by: 113

README

utee

utilities for golang

Documentation

Index

Constants

View Source
const (
	MAX_HTTP_CLIENT_CONCURRENT = 1000

	ContentTypeForm = "application/x-www-form-urlencoded"
	ContentTypeJson = "application/json; charset=utf-8"
)

Variables

View Source
var (
	// PlainMd5 string md5 function with empty salt
	PlainMd5 = Md5Str("")
	// PlainSha1 string sha-1 function with empty salt
	PlainSha1 = Sha1Str("")
)
View Source
var (
	ErrEmptyHeaderName = errors.New("header name must not be empty")
)
View Source
var ErrFull = errors.New("queue is full")

Functions

func Chk

func Chk(err error)

func Filter added in v1.3.0

func Filter[T any](collection []T, fn func(T) bool) []T

Filter lo.Filter without loop index

func FilterMap added in v1.3.0

func FilterMap[T any, R any](collection []T, fn func(T) (R, bool)) []R

FilterMap lo.FilterMap without loop index

func Finish added in v1.4.9

func Finish(ctx context.Context, concurrent int, fn ...func() error) error

Finish concurrent run fn, return error if any of fn return error similar to mapreduce.Finish, but with concurrent limit concurrent , limit of map reduce concurrency

func FinishWithGen added in v1.4.10

func FinishWithGen(ctx context.Context, concurrent int, gen func(source chan<- func() error)) error

FinishWithGen concurrent run fn (which is generated by gen), return error if any of fn return error concurrent , limit of map reduce concurrency

func FlatMap added in v1.3.0

func FlatMap[T any, R any](collection []T, fn func(T) []R) []R

FlatMap lo.FlatMap without loop index

func HmacSha256

func HmacSha256(s string, key string) string

func HttpGet

func HttpGet(getUrl string, credential ...string) ([]byte, error)

HttpGet Deprecated use HttpGet2 instead

func HttpGet2

func HttpGet2(getUrl string, contentType string, opt *HttpOpt) ([]byte, error)

func HttpPost

func HttpPost(postUrl string, q url.Values, credential ...string) ([]byte, error)

HttpPost Deprecated use HttpPost2 instead

func HttpPost2

func HttpPost2(postUrl string, contentType string, body io.Reader, opt *HttpOpt) ([]byte, error)

func IsPemExpire

func IsPemExpire(b []byte) (bool, error)

func Map added in v1.3.0

func Map[T any, R any](collection []T, fn func(T) R) []R

Map lo.Map without loop index

func Max added in v1.2.10

func Max[T cmp.Ordered](n ...T) T

Max returns the biggest parameter

func Md5

func Md5(b []byte) []byte

func Md5Str

func Md5Str(salt string) func(string) string

Md5Str create string md5 function with salt

func Min added in v1.2.10

func Min[T cmp.Ordered](n ...T) T

Min returns the smallest parameter

func OrderBy added in v1.3.0

func OrderBy[T any, R cmp.Ordered](l []T, fn func(T) R) []T

OrderBy order by fn, return ordered copy of slice

func OrderByDescending added in v1.3.0

func OrderByDescending[T any, R cmp.Ordered](l []T, fn func(T) R) []T

OrderByDescending order by fn descending, return ordered copy of slice

func ParseAddr

func ParseAddr(s string) (string, int, error)

func PasswdHash added in v1.4.5

func PasswdHash(password string, cost ...int) (string, error)

PasswdHash generate password hash, compatible with PHP Yii framework

func Reject added in v1.3.0

func Reject[T any](collection []T, fn func(T) bool) []T

Reject lo.Reject without loop index

func SetHttpClient added in v1.2.7

func SetHttpClient(hc *http.Client)

SetHttpClient expose HTTP Client for further customize

func Sha1Str

func Sha1Str(salt string) func(string) string

Sha1Str create string sha-1 function with salt

func Sha256Str

func Sha256Str(salt string) func(string) string

Sha256Str create string sha-256 function with salt

func Shuffle

func Shuffle[T any](collection []T) []T

Shuffle return a shuffled copy of collection

func SplitSliceIntoN added in v1.4.7

func SplitSliceIntoN[T any](a []T, n int) [][]T

SplitSliceIntoN split slice into several parts, no more than n

func TimeBetween added in v1.4.16

func TimeBetween(check, start, end time.Time) bool

TimeBetween check if check time is between start and end

func Truncate

func Truncate(s string, maxLen uint) string

Truncate , truncate string as []rune make sure the rune count of result is not more than maxLen

func VerifyPasswd added in v1.4.5

func VerifyPasswd(password, hash string) bool

VerifyPasswd validate password,compatible with PHP Yii framework

func Zip added in v1.4.6

func Zip(pathToZip string, dest io.Writer) error

Zip compress file or directory into writer pathToZip: the path to zip, could be a file or a directory dest: the writer to write zip content

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

type HttpOpt

type HttpOpt struct {
	Headers   map[string]string
	BasicAuth *BasicAuth
}

type J

type J map[string]interface{}

func (J) ToReader added in v1.2.5

func (p J) ToReader() io.Reader

func (J) ToString added in v1.2.5

func (p J) ToString() string

type MemQueue

type MemQueue chan interface{}

MemQueue memory queue

func NewMemQueue

func NewMemQueue(cap int) MemQueue

NewMemQueue create memory queue

func (MemQueue) Cap

func (p MemQueue) Cap() int

Cap queue capacity

func (MemQueue) Deq

func (p MemQueue) Deq() interface{}

Deq

func (MemQueue) DeqN

func (p MemQueue) DeqN(n int) []interface{}

DeqN dequeue less than n in a batch

func (MemQueue) Enq

func (p MemQueue) Enq(data interface{}) error

Enq enqueue, return error if queue is full

func (MemQueue) EnqBlocking

func (p MemQueue) EnqBlocking(data interface{})

EnqBlocking enqueue, block if queue is full

func (MemQueue) Len

func (p MemQueue) Len() int

Len queue length

type PerfLog

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

PerfLog performance log

func NewPerfLog

func NewPerfLog(maxMs uint32, logger *logrus.Entry) *PerfLog

NewPerfLog 生成PerfLog, maxMs 输出阈值,单位为毫秒

func (*PerfLog) Done

func (p *PerfLog) Done()

func (*PerfLog) Tick

func (p *PerfLog) Tick(label interface{})

type Throttle

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

Throttle , limit number of concurrent goroutines

func NewThrottle

func NewThrottle(max int) *Throttle

NewThrottle New Throttle max , max number of this throttle

func (*Throttle) Acquire

func (p *Throttle) Acquire()

Acquire , acquire 1

func (*Throttle) Available

func (p *Throttle) Available() int

Available , available number of this throttle

func (*Throttle) Current

func (p *Throttle) Current() int

Current , current number of this throttle

func (*Throttle) Release

func (p *Throttle) Release()

Release , release 1

func (*Throttle) Wait added in v1.4.2

func (p *Throttle) Wait()

Wait , wait until current number become 0

type Tick

type Tick int64

Tick unix timestamp in millisecond

func NewTick added in v1.2.2

func NewTick(t ...time.Time) Tick

NewTick create Tick. default value if now at local time

func (Tick) ToTime added in v1.2.2

func (p Tick) ToTime() time.Time

ToTime convert tick to local time

func (Tick) Value added in v1.2.9

func (p Tick) Value() int64

Value return int 64 tick value

type TimerCache2 added in v1.4.0

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

TimerCache2 generic timer cache

func NewTimerCache2 added in v1.4.0

func NewTimerCache2[K comparable, V any](ttl int, expireCb ...func(key K, value V)) *TimerCache2[K, V]

NewTimerCache2 create new TimerCache2 ttl in second expireCb, expire callback

func (*TimerCache2[K, V]) Get added in v1.4.0

func (p *TimerCache2[K, V]) Get(key K) (V, bool)

func (*TimerCache2[K, V]) Keys added in v1.4.0

func (p *TimerCache2[K, V]) Keys() []K

func (*TimerCache2[K, V]) Len added in v1.4.0

func (p *TimerCache2[K, V]) Len() int

func (*TimerCache2[K, V]) Put added in v1.4.0

func (p *TimerCache2[K, V]) Put(key K, val V) bool

func (*TimerCache2[K, V]) Remove added in v1.4.0

func (p *TimerCache2[K, V]) Remove(key K) (V, bool)

func (*TimerCache2[K, V]) TTL added in v1.4.0

func (p *TimerCache2[K, V]) TTL(key K) (int64, bool)

TTL Check ttl (in second)

Jump to

Keyboard shortcuts

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