goutil

package module
v1.4.10 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 29 Imported by: 6

README

goutil

golang util 常用函数和工具类

jsonexp

一个json表达式组件,赋予json的语义化的能力

linked_list

双向链表, 支持线程安全和非安全,最大化满足性能要求

queue

分段式slice,在内存分配和性能之间取得平衡,类似c++ stl的deque

ring_queue

环形队列

safe_rand

线程安全的随机数生成器

token_bucket

令牌桶,典型的使用场景是用于流量控制和调节

mysqlutil

mysql的工具类函数,最主要的是实现对sql语句的参数化处理,避免sql注入的同时方便sql的日志化(将参数实例代入sql,产生明确的sql)

goutil.go

常用的通用化函数和类

lru_file_cache.go

基于LRU淘汰策略的文件缓存组件

支持各种负载均衡策略的http client

Documentation

Overview

线程安全、持久化的map 包含一个持久化的map和一个先进先出的队列 fangyousong 2023/3/17

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorFileSizeLimited = errors.New("file size limited")
	ErrorFileNotExists   = errors.New("file not exists")
	ErrorFileNotModified = errors.New("file not modified")
)
View Source
var MaxMapFileSize int64 = 1024 * 1024 * 1024
View Source
var MimeTypeMap map[string]string = map[string]string{}/* 103 elements not displayed */

Functions

func AcquireTimer added in v1.4.6

func AcquireTimer(timeout time.Duration) *time.Timer

func BytesMd5

func BytesMd5(b []byte) (string, error)

func Daemonize deprecated

func Daemonize(nochdir, noclose int) int

Deprecated: use syscall is Deprecated

func FileExists

func FileExists(file string) bool

func FileMd5

func FileMd5(fn string) (string, error)

func FileModTime

func FileModTime(file string) string

func FilePathExists added in v1.2.4

func FilePathExists(filePath string) bool

func GBK2UTF8 added in v1.2.4

func GBK2UTF8(srcGbk []byte) ([]byte, error)

func GetExeName added in v1.4.6

func GetExeName() string

func GetExePath added in v1.4.6

func GetExePath() string

func GetFloatValue added in v1.4.6

func GetFloatValue(v interface{}) (float64, bool)

func GetIntValue added in v1.4.6

func GetIntValue(v interface{}) (int64, bool)

func GetIntValueDefault added in v1.4.6

func GetIntValueDefault(v interface{}, dft int64) int64

func GetStringValue added in v1.3.5

func GetStringValue(v interface{}) string

func Gunzip

func Gunzip(data []byte) ([]byte, error)

func Gzip

func Gzip(data []byte) ([]byte, error)

func InStringList added in v1.4.6

func InStringList(list []string, s string) bool

func IsUtf8 added in v1.2.4

func IsUtf8(bts []byte) bool

func JsonMarshalNoHtmlEncode added in v1.4.6

func JsonMarshalNoHtmlEncode(t interface{}) ([]byte, error)

func LogStack added in v1.4.6

func LogStack(fd *os.File)

func Md5

func Md5(b []byte) ([]byte, error)

func Rc4

func Rc4(key []byte, data []byte) []byte

func ReleaseTimer added in v1.4.6

func ReleaseTimer(t *time.Timer)

func RestartProcess

func RestartProcess(keepFiles []*os.File, envVars []string, cleaner func() error) error

func SplitByLine

func SplitByLine(s string) (ret []string)

func SplitLR

func SplitLR(s string, separator string) (l string, r string)

func StringMd5

func StringMd5(s string) (string, error)

func UTF82GBK added in v1.2.4

func UTF82GBK(srcUtf8 []byte) ([]byte, error)

func UnsafeBytesToString added in v1.4.10

func UnsafeBytesToString(b []byte) string

func UnsafeStringToBytes added in v1.4.10

func UnsafeStringToBytes(s string) (b []byte)

func UrlEncodedMarshal added in v1.3.5

func UrlEncodedMarshal(structObj interface{}) string

Types

type CachedFile added in v1.4.6

type CachedFile struct {
	ModifyTime time.Time
	Data       []byte
	Md5        string
}

type Context added in v1.4.7

type Context interface {
	GetCtxData(key string) (interface{}, bool)
	SetCtxData(key string, value interface{})
	RemoveCtxData(key string)
}

type DefaultContext added in v1.4.7

type DefaultContext struct {
	WithLock bool
	// contains filtered or unexported fields
}

func (*DefaultContext) GetCtxData added in v1.4.7

func (m *DefaultContext) GetCtxData(key string) (interface{}, bool)

func (*DefaultContext) RemoveCtxData added in v1.4.7

func (m *DefaultContext) RemoveCtxData(key string)

func (*DefaultContext) SetCtxData added in v1.4.7

func (m *DefaultContext) SetCtxData(key string, value interface{})

type DefaultErrorHolder added in v1.4.7

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

func (*DefaultErrorHolder) GetError added in v1.4.7

func (m *DefaultErrorHolder) GetError() error

func (*DefaultErrorHolder) SetError added in v1.4.7

func (m *DefaultErrorHolder) SetError(err error)

type Error added in v1.4.7

type Error struct {
	Code    int
	Message string
	Tm      time.Time
}

func NewError added in v1.4.7

func NewError(code int, message string) Error

func NewErrorf added in v1.4.7

func NewErrorf(code int, message string, args ...interface{}) Error

func (*Error) Error added in v1.4.7

func (m *Error) Error() string

type ErrorHolder added in v1.4.7

type ErrorHolder interface {
	GetError() error
	SetError(err error)
}

type FileInfoListModTimeAsc added in v1.4.10

type FileInfoListModTimeAsc []os.FileInfo

文件列表按修改时间从旧到新排序

func (FileInfoListModTimeAsc) Len added in v1.4.10

func (p FileInfoListModTimeAsc) Len() int

func (FileInfoListModTimeAsc) Less added in v1.4.10

func (p FileInfoListModTimeAsc) Less(i, j int) bool

func (FileInfoListModTimeAsc) Swap added in v1.4.10

func (p FileInfoListModTimeAsc) Swap(i, j int)

type FileInfoListModTimeDesc added in v1.4.10

type FileInfoListModTimeDesc []os.FileInfo

文件列表按修改时间从新到旧排序

func (FileInfoListModTimeDesc) Len added in v1.4.10

func (p FileInfoListModTimeDesc) Len() int

func (FileInfoListModTimeDesc) Less added in v1.4.10

func (p FileInfoListModTimeDesc) Less(i, j int) bool

func (FileInfoListModTimeDesc) Swap added in v1.4.10

func (p FileInfoListModTimeDesc) Swap(i, j int)

type FileMap added in v1.4.10

type FileMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

持久化的map

func NewFileMap added in v1.4.10

func NewFileMap(tag string, filePath string, enableKeyQueue bool) (*FileMap, error)

func (*FileMap) Close added in v1.4.10

func (m *FileMap) Close()

func (*FileMap) Delete added in v1.4.10

func (m *FileMap) Delete(key string)

func (*FileMap) Flush added in v1.4.10

func (m *FileMap) Flush()

func (*FileMap) Get added in v1.4.10

func (m *FileMap) Get(key string) []byte

func (*FileMap) Len added in v1.4.10

func (m *FileMap) Len() int

func (*FileMap) Pop added in v1.4.10

func (m *FileMap) Pop(waitTimeout time.Duration) (retKey string, timeout bool)

FIFO queue

func (*FileMap) Put added in v1.4.10

func (m *FileMap) Put(key string, packedJsonData []byte, overrideIfExists bool, liveDuration time.Duration) error

func (*FileMap) RemoveExpiredData added in v1.4.10

func (m *FileMap) RemoveExpiredData()

type JsonFloat64 added in v1.3.5

type JsonFloat64 float64

func (*JsonFloat64) Scan added in v1.4.6

func (m *JsonFloat64) Scan(value interface{}) error

func (*JsonFloat64) UnmarshalJSON added in v1.3.5

func (m *JsonFloat64) UnmarshalJSON(bts []byte) error

func (JsonFloat64) Value added in v1.4.6

func (m JsonFloat64) Value() (driver.Value, error)

type JsonInt added in v1.3.5

type JsonInt int

func (*JsonInt) Scan added in v1.4.6

func (m *JsonInt) Scan(value interface{}) error

func (*JsonInt) UnmarshalJSON added in v1.3.5

func (m *JsonInt) UnmarshalJSON(bts []byte) error

func (JsonInt) Value added in v1.4.6

func (m JsonInt) Value() (driver.Value, error)

type JsonStr added in v1.3.5

type JsonStr string

func (*JsonStr) Scan added in v1.4.6

func (m *JsonStr) Scan(value interface{}) error

func (*JsonStr) UnmarshalJSON added in v1.3.5

func (m *JsonStr) UnmarshalJSON(bts []byte) error

func (JsonStr) Value added in v1.4.6

func (m JsonStr) Value() (driver.Value, error)

type LRUFileCache added in v1.4.6

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

func NewLRUFileCache added in v1.4.6

func NewLRUFileCache(fileSizeLimit int64, cacheSizeLimit int64) *LRUFileCache

func (*LRUFileCache) Clear added in v1.4.6

func (m *LRUFileCache) Clear()

func (*LRUFileCache) Get added in v1.4.6

func (m *LRUFileCache) Get(abstractFilePath string, since time.Time) (*CachedFile, error)

func (*LRUFileCache) GetStatis added in v1.4.6

func (m *LRUFileCache) GetStatis() *LRUFileCacheStatis

type LRUFileCacheStatis added in v1.4.6

type LRUFileCacheStatis struct {
	FileCount int             `json:"file_count"`
	CacheSize int64           `json:"cache_size"`
	FileList  []LRUFileStatis `json:"file_list"`
}

type LRUFileStatis added in v1.4.6

type LRUFileStatis struct {
	AbsPath    string `json:"abs_path"`
	Size       int    `json:"size"`
	Md5        string `json:"md5"`
	ModifyTime int64  `json:"modify_time"`
	ReadCount  int64  `json:"read_count"`
}

type LinkedList

type LinkedList struct {
	Len int
	// contains filtered or unexported fields
}

双向链表

func NewLinkedList

func NewLinkedList(threadSafe bool) *LinkedList

func (*LinkedList) Delete

func (m *LinkedList) Delete(node *LinkedNode)

func (*LinkedList) InsertAfter

func (m *LinkedList) InsertAfter(data interface{}, relative *LinkedNode) bool

func (*LinkedList) InsertBefore

func (m *LinkedList) InsertBefore(data interface{}, relative *LinkedNode) bool

func (*LinkedList) Iterate

func (m *LinkedList) Iterate(iterator func(data interface{}, canceled *bool))

func (*LinkedList) PopHead

func (m *LinkedList) PopHead(lock bool) interface{}

func (*LinkedList) PopTail

func (m *LinkedList) PopTail(lock bool) interface{}

func (*LinkedList) PushHead

func (m *LinkedList) PushHead(data interface{}, lock bool)

func (*LinkedList) PushTail

func (m *LinkedList) PushTail(data interface{}, lock bool)

func (*LinkedList) ReverseIterate

func (m *LinkedList) ReverseIterate(iterator func(nodeData interface{}, canceled *bool))

type LinkedNode

type LinkedNode struct {
	PriorNode *LinkedNode
	NextNode  *LinkedNode
	Data      interface{}
}

type Queue

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

由多个环形队列拼接成的长队列,借鉴了c++ stl中dequeue的思想

func NewQueue

func NewQueue(capacity int, chunkSize int) *Queue

func (*Queue) Consume

func (m *Queue) Consume(waitTime time.Duration) (interface{}, error)

func (*Queue) Produce

func (m *Queue) Produce(data interface{}, waitTime time.Duration) error

type RingQueue

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

环形双向队列

func NewRingQueue

func NewRingQueue(capacity int, threadSafe bool) *RingQueue

func (*RingQueue) PopHead

func (m *RingQueue) PopHead(waitTime time.Duration) (interface{}, error)

func (*RingQueue) PopTail

func (m *RingQueue) PopTail(waitTime time.Duration) (interface{}, error)

func (*RingQueue) PushHead

func (m *RingQueue) PushHead(value interface{}, waitTime time.Duration) error

func (*RingQueue) PushTail

func (m *RingQueue) PushTail(value interface{}, waitTime time.Duration) error

func (*RingQueue) Size

func (m *RingQueue) Size() int

type SafeRand

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

线程安全的随机数生成器

func NewSafeRand

func NewSafeRand() *SafeRand

func (*SafeRand) Int63n

func (m *SafeRand) Int63n(n int64) int64

type TokenBucket added in v1.2.4

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

令牌桶,用于限流. 令牌生产的时间间隔固定为100ms,每次生产的令牌数根据qps的大小计算出: qps/10+1

func NewTokenBucket added in v1.2.4

func NewTokenBucket(capacity int64, qps int64) *TokenBucket

func (*TokenBucket) GetToken added in v1.2.4

func (m *TokenBucket) GetToken() bool

获取令牌

func (*TokenBucket) SetQps added in v1.2.4

func (m *TokenBucket) SetQps(qps int64)

修改qps

func (*TokenBucket) Stop added in v1.2.4

func (m *TokenBucket) Stop()

停止生产令牌

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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