Documentation ¶
Overview ¶
chan实现trylock乐观锁
* 采用cas实现乐观锁及TryLock Compare And Swap 简称CAS,在sync/atomic包种 这类原子操作由名称以‘CompareAndSwap’为前缀的若干个函数代表。 声明如下
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
调用函数后,会先判断参数addr指向的被操作值与参数old的值是否相等 仅当此判断得到肯定的结果之后,才会用参数new代表的新值替换掉原先的旧值,否则操作就会被忽略。 so, 需要用for循环不断进行尝试,直到成功为止
使用锁的做法趋于悲观
我们总假设会有并发的操作要修改被操作的值,并使用锁将相关操作放入临界区中加以保护
使用CAS操作的做法趋于乐观
总是假设被操作值未曾被改变(即与旧值相等),并一旦确认这个假设的真实性就立即进行值替换。
*
- 每天流动式日志实现
- 操作日志记录到文件,支持info,error,debug,notice,alert等
- 写日志文件的时候,采用乐观锁方式对文件句柄进行加锁
- 等级参考php Monolog/logger.php
- 日志切割机制参考lumberjack包实现
- json encode采用jsoniter库快速json encode处理
Index ¶
- Constants
- Variables
- func AlterLog(v interface{}, options map[string]interface{})
- func Bin2hex(src []byte) string
- func CatchStack() []byte
- func CheckPanic()
- func CheckPathExist(path string) bool
- func Chown(name string, info os.FileInfo) error
- func Chr(ascii int) string
- func CopyFile(distName, srcName string) (w int64, err error)
- func Crc32(str string) uint32
- func CritLog(v interface{}, options map[string]interface{})
- func DeAES(in, key, iv []byte) ([]byte, error)
- func DebugLog(v interface{}, options map[string]interface{})
- func EmergLog(v interface{}, options map[string]interface{})
- func EnAES(in, key, iv []byte) ([]byte, error)
- func ErrorLog(v interface{}, options map[string]interface{})
- func Explode(delimiter, str string) []string
- func Filebase(file string) string
- func Fileline(file string, line int) string
- func Float64ToStr(f64 float64) string
- func FormatNow() string
- func FormatTime(t time.Time) string
- func FormatTime19(t time.Time) string
- func FormatUTC() string
- func GetCurrentLocalTime() string
- func GetLoc(zone string) *time.Location
- func GetTimeByTimeZone(zone string) string
- func Gunzip(in []byte) ([]byte, error)
- func Gzip(in []byte) ([]byte, error)
- func HTMLSpecialchars(str string) string
- func HTMLSpecialcharsDecode(str string) string
- func Hash(mem []byte) uint64
- func Hex2bin(str string) []byte
- func Implode(glue string, pieces []string) string
- func InfoLog(v interface{}, options map[string]interface{})
- func Int64ToStr(i64 int64) string
- func IntToStr(n int) string
- func Krand(size int, kind int) string
- func LoadGobData(data interface{}, fileName string)
- func LogSize(n int64)
- func LogSplit(b bool)
- func Md5(str string) string
- func Md5File(filename string) (string, error)
- func NewUUID() string
- func NoticeLog(v interface{}, options map[string]interface{})
- func NumberNow() uint64
- func NumberTime(t time.Time) uint64
- func NumberUTC() uint64
- func Ord(character string) rune
- func ParseNumber(t uint64) (time.Time, error)
- func ParseNumberUTC(t uint64) (time.Time, error)
- func ParseTime(s string) (time.Time, error)
- func ParseTimeUTC(s string) (time.Time, error)
- func RandInt64(min, max int64) int64
- func RecoverLog()
- func RndUuid() string
- func RndUuidMd5() string
- func Round(f float64, n int) float64
- func RunShell(exeStr string) (string, error)
- func SetLogDir(dir string)
- func SetLogTimeZone(timezone string)
- func SetTimeZone(zone string)
- func Sha1(str string) string
- func Sha1File(path string) (string, error)
- func Stack() []byte
- func StoreGobData(data interface{}, fileName string) error
- func StrJoin(sep string, str ...string) string
- func StrJoinByBuf(str ...string) string
- func StrToFloat64(str string) float64
- func StrToInt(s string) int
- func StrToInt64(s string) int64
- func StringMapKeys(m interface{}) (res []string)
- func Stripos(haystack, needle string) int
- func Strlen(str string) int
- func Strpos(haystack, needle string) int
- func Strripos(haystack, needle string) int
- func Strrpos(haystack, needle string) int
- func Uuid() string
- func WarnLog(v interface{}, options map[string]interface{})
- func Xss(str string) string
- func XssUnescape(str string) string
- type ChanLock
- type EmptyArray
- type EmptyStruct
- type H
- type Lock
- type Mutex
- type Semaphore
- type StringList
- type TaskRes
- func DoTask(fn func() interface{}) *TaskRes
- func DoTaskWithArgs(fn func(args ...interface{}) interface{}, args ...interface{}) *TaskRes
- func DoTaskWithContext(ctx context.Context, fn func() interface{}, timeout time.Duration) *TaskRes
- func DoTaskWithContextArgs(ctx context.Context, fn func(args ...interface{}) interface{}, ...) *TaskRes
- func DoTaskWithTimeout(fn func() interface{}, timeout time.Duration) *TaskRes
- func DoTaskWithTimeoutArgs(fn func(args ...interface{}) interface{}, timeout time.Duration, ...) *TaskRes
Constants ¶
const ( EMERGENCY = "emerg" // 严重错误: 导致系统崩溃无法使用 ALTER = "alter" // 警戒性错误: 必须被立即修改的错误 CRIT = "crit" // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样 ERR = "error" // 一般错误:比如类型错误,数据库连接不上等等 WARN = "warn" // 警告性错误: 需要发出警告的错误 NOTICE = "notice" // 通知: 程序可以运行但是还不够完美的错误 INFO = "info" // 信息: 程序输出信息 DEBUG = "debug" // 调试: 调试信息 )
日志级别 从上到下,由高到低
Variables ¶
var LogLevelMap = map[string]int{ EMERGENCY: 600, ALTER: 550, CRIT: 500, ERR: 400, WARN: 300, NOTICE: 250, INFO: 200, DEBUG: 100, }
var TimeZone = "Asia/Shanghai" //默认时区设置,可以是Local本地时区
Functions ¶
func CatchStack ¶
func CatchStack() []byte
CatchStack 捕获指定stack信息,一般在处理panic/recover中处理 返回完整的堆栈信息和函数调用信息
func CheckPathExist ¶ added in v1.1.6
CheckPathExist check file or path exist
func Chown ¶ added in v1.1.3
Chown 清空文件并保持文件权限不变,并非linux chown操作 Empty the file and keep the file permissions unchanged not the linux chown operation
func Chr ¶ added in v1.2.13
================str chr,ord func====================== Chr returns a one-character string containing the character specified by ascii
func EnAES ¶
=================str aes/des func========================== key = "abcdefghijklmnopqrstuvwxyz123456" iv = "0123456789ABCDEF"
func Explode ¶ added in v1.2.13
=================str explode,implode,strlen================ Explode returns an slice of strings, each of which is a substring of str formed by splitting it on boundaries formed by the string delimiter.
func Float64ToStr ¶ added in v1.2.13
Float64ToStr float64 to string 'e' (-d.dddde±dd,十进制指数)
func FormatTime ¶
format a time.Time to string as 2006-01-02 15:04:05.999
func FormatTime19 ¶
format a time.Time to string as 2006-01-02 15:04:05 将time.time转换为日期格式
func GetTimeByTimeZone ¶
func HTMLSpecialchars ¶ added in v1.2.13
=====================html special characters================ HTMLSpecialchars converts special characters to HTML entities
func HTMLSpecialcharsDecode ¶ added in v1.2.13
HTMLSpecialcharsDecode converts special HTML entities back to characters
func Implode ¶ added in v1.2.13
Implode returns a string containing a string representation of all the slice elements in the same order, with the glue string between each element.
func IntToStr ¶
================str,int,int64,float64 conv func======================= IntToStr int-->string
func Krand ¶
================str krand func=========== Krand 根据kind生成不同风格的指定区间随机字符串 纯数字kind=0,小写字母kind=1 大写字母kind=2,数字+大小写字母kind=3
func LoadGobData ¶
func LoadGobData(data interface{}, fileName string)
LoadGobData 将gob写入的内容,载入到data中
func Md5 ¶
==============str md5,md5File,sha1,crc32,bin2hex,hex2bin,hash func======= Other advanced functions, please see the thinkgo/crypto package. md5 func
func NewUUID ¶
func NewUUID() string
=================uuid,rnduuid func==================== NewUUID 通过随机数的方式生成uuid 如果rand.Read失败,就按照当前时间戳+随机数进行md5方式生成 该方式生成的uuid有可能存在重复值 返回格式:7999b726-ca3c-42b6-bda2-259f4ac0879a
func NumberTime ¶
format a time.Time to number as 20060102150405999
func ParseTimeUTC ¶
parse a string as "2006-01-02 15:04:05.999" to time.Time
func RecoverLog ¶ added in v1.1.9
func RecoverLog()
RecoverLog 异常捕获处理,对于异常或者panic进行捕获处理 记录到日志中,方便定位问题
func RndUuid ¶
func RndUuid() string
RndUuid 基于时间ns和随机数实现唯一的uuid 在单台机器上是不会出现重复的uuid 如果要在分布式的架构上生成不重复的uuid 只需要在rndStr的前面加一些自定义的字符串 返回格式:eba1e8cd-0460-4910-49c6-44bdf3cf024d
func SetTimeZone ¶
func SetTimeZone(zone string)
func Stack ¶
func Stack() []byte
Stack 获取完整的堆栈信息 Stack returns a formatted stack trace of the goroutine that calls it. It calls runtime.Stack with a large enough buffer to capture the entire trace.
func StoreGobData ¶
func StrToFloat64 ¶ added in v1.2.13
StrToFloat64 string--->float64
func StringMapKeys ¶
func StringMapKeys(m interface{}) (res []string)
func Stripos ¶ added in v1.2.13
Stripos find position of the first occurrence of a case-insensitive substring in a string
func Strpos ¶ added in v1.2.13
=================str strpos,Strrpos,stripos,Strripos func==================== Strpos find position of first occurrence of string in a string It's multi-byte safe. return -1 if can not find the substring
func Strripos ¶ added in v1.2.13
Strripos find the position of the last occurrence of a case-insensitive substring in a string
func Strrpos ¶ added in v1.2.13
Strrpos find the position of the last occurrence of a substring in a string
func XssUnescape ¶
Types ¶
type ChanLock ¶
type ChanLock struct {
// contains filtered or unexported fields
}
func NewChanLock ¶
func NewChanLock() *ChanLock
func (*ChanLock) TryLockTimeout ¶
指定时间内的乐观锁
type EmptyArray ¶ added in v1.1.5
type EmptyArray []struct{}
EmptyArray 兼容其他语言的[]空数组,一般在tojson的时候转换为[]
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
分配安全的bool chan
func NewSemaphore ¶
type StringList ¶
type StringList []string
StringList: str list
func (StringList) Count ¶
func (this StringList) Count() int
func (StringList) Delete ¶
func (this StringList) Delete(token string) int
func (StringList) IsEmpty ¶
func (this StringList) IsEmpty() bool
func (StringList) Len ¶
func (this StringList) Len() int
func (StringList) Less ¶
func (this StringList) Less(i, j int) bool
func (StringList) Swap ¶
func (this StringList) Swap(i, j int)
func (StringList) UniqueAdd ¶
func (this StringList) UniqueAdd(token string) StringList
type TaskRes ¶ added in v1.1.5
dotask返回的结果
func DoTask ¶ added in v1.1.5
func DoTask(fn func() interface{}) *TaskRes
DoTask 在独立携程中运行fn 这里返回结果设计为interface{},因为有时候返回结果可以是error
func DoTaskWithArgs ¶ added in v1.1.5
func DoTaskWithArgs(fn func(args ...interface{}) interface{}, args ...interface{}) *TaskRes
DoTaskWithArgs 在独立携程中执行有参数的fn
func DoTaskWithContext ¶ added in v1.1.5
DoTaskWithContext 通过上下文context+done+select实现goroutine超时调用
func DoTaskWithContextArgs ¶ added in v1.1.5
func DoTaskWithContextArgs(ctx context.Context, fn func(args ...interface{}) interface{}, timeout time.Duration, args ...interface{}) *TaskRes
DoTaskWithContextArgs 通过上下文context+done+select实现goroutine超时调用
func DoTaskWithTimeout ¶ added in v1.1.5
DoTaskWithTimeout 采用done+select+time.After实现goroutine超时调用
func DoTaskWithTimeoutArgs ¶ added in v1.1.5
func DoTaskWithTimeoutArgs(fn func(args ...interface{}) interface{}, timeout time.Duration, args ...interface{}) *TaskRes
DoTaskWithTimeoutArgs 采用done+select+time.After实现goroutine超时调用