common

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2019 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

* 采用cas实现乐观锁及TryLock Compare And Swap 简称CAS,在sync/atomic包种 这类原子操作由名称以‘CompareAndSwap’为前缀的若干个函数代表。 声明如下

func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)

调用函数后,会先判断参数addr指向的被操作值与参数old的值是否相等 仅当此判断得到肯定的结果之后,才会用参数new代表的新值替换掉原先的旧值,否则操作就会被忽略。 so, 需要用for循环不断进行尝试,直到成功为止

使用锁的做法趋于悲观

我们总假设会有并发的操作要修改被操作的值,并使用锁将相关操作放入临界区中加以保护

使用CAS操作的做法趋于乐观

总是假设被操作值未曾被改变(即与旧值相等),并一旦确认这个假设的真实性就立即进行值替换。

chan实现trylock乐观锁

*

  • 每天流动式日志实现
  • 操作日志记录到文件,支持info,error,debug,notice,alert等
  • 写日志文件的时候,采用chan实现的乐观锁方式对文件句柄进行加锁
  • 等级参考php Monolog/logger.php

Index

Constants

View Source
const (
	EMERGENCY = "emerg"  // 严重错误: 导致系统崩溃无法使用
	ALERT     = "alter"  // 警戒性错误: 必须被立即修改的错误
	CRIT      = "crit"   // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样
	ERR       = "error"  // 一般错误: 一般性错误
	WARN      = "warn"   // 警告性错误: 需要发出警告的错误
	NOTICE    = "notice" // 通知: 程序可以运行但是还不够完美的错误
	INFO      = "info"   // 信息: 程序输出信息
	DEBUG     = "debug"  // 调试: 调试信息
)

日志级别 从上到下,由高到低

Variables

View Source
var LogLevelMap = map[string]int{
	EMERGENCY: 600,
	ALERT:     550,
	CRIT:      500,
	ERR:       400,
	WARN:      300,
	NOTICE:    250,
	INFO:      200,
	DEBUG:     100,
}
View Source
var TimeZone = "PRC" //默认时区设置

Functions

func AlterLog

func AlterLog(v interface{})

func CatchStack

func CatchStack() []byte

捕获指定stack信息,一般在处理panic/recover中处理 返回完整的堆栈信息和函数调用信息

func CheckPanic

func CheckPanic()

check panic when exit

func CopyFile

func CopyFile(distName, srcName string) (w int64, err error)

复制文件到另一个文件

func CritLog

func CritLog(v interface{})

func DeAES

func DeAES(in, key, iv []byte) ([]byte, error)

func DebugLog

func DebugLog(v interface{})

func EmergLog

func EmergLog(v interface{})

func EnAES

func EnAES(in, key, iv []byte) ([]byte, error)

key = "abcdefghijklmnopqrstuvwxyz123456" iv = "0123456789ABCDEF"

func ErrorLog

func ErrorLog(v interface{})

func Filebase

func Filebase(file string) string

获取文件的名称不带后缀

func Fileline

func Fileline(file string, line int) string

获取文件名:行数

func FormatNow

func FormatNow() string

format time.Now() use FormatTime

func FormatTime

func FormatTime(t time.Time) string

format a time.Time to string as 2006-01-02 15:04:05.999

func FormatTime19

func FormatTime19(t time.Time) string

format a time.Time to string as 2006-01-02 15:04:05 将time.time转换为日期格式

func FormatUTC

func FormatUTC() string

format time.Now().UTC() use FormatTime

func GetCurrentLocalTime

func GetCurrentLocalTime() string

当前本地时间

func GetLoc

func GetLoc(zone string) *time.Location

获取时区loc

func GetTimeByTimeZone

func GetTimeByTimeZone(zone string) string

func Gunzip

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

decompress data user gunzip

func Gzip

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

compress data use gzip

func Hash

func Hash(mem []byte) uint64

hash : []byte to uint64

func HupSignal

func HupSignal() <-chan os.Signal

reload signal

func InfoLog

func InfoLog(v interface{})

func Init

func Init(writer io.Writer)

parse flag and print usage/value to writer

func Int64ToStr

func Int64ToStr(i64 int64) string

int64-->string

func IntToStr

func IntToStr(n int) string

int-->string

func IsSystemdService

func IsSystemdService() bool

func Krand

func Krand(size int, kind int) string

根据kind生成不同风格的指定区间随机字符串 纯数字kind=0,小写字母kind=1 大写字母kind=2,数字+大小写字母kind=3

func ListenRpc

func ListenRpc(addr string, obj interface{}) error

func ListenSocket

func ListenSocket(addr string, keepalive bool, reactiver func(net.Conn)) error

func ListenUdp

func ListenUdp(addr string, bufsize int, reactiver func(*net.UDPAddr, []byte) []byte) error

func LoadGobData

func LoadGobData(data interface{}, fileName string)

将gob写入的内容,载入到data中

func Md5

func Md5(str string) string

func NewUUID

func NewUUID() string

通过随机数的方式生成uuid 如果rand.Read失败,就按照当前时间戳+随机数进行md5方式生成 该方式生成的uuid有可能存在重复值 返回格式:7999b726-ca3c-42b6-bda2-259f4ac0879a

func NoticeLog

func NoticeLog(v interface{})

func NumberNow

func NumberNow() uint64

format time.Now() use NumberTime

func NumberTime

func NumberTime(t time.Time) uint64

format a time.Time to number as 20060102150405999

func NumberUTC

func NumberUTC() uint64

format time.Now().UTC() use NumberTime

func ParseNumber

func ParseNumber(t uint64) (time.Time, error)

func ParseNumberUTC

func ParseNumberUTC(t uint64) (time.Time, error)

func ParseTime

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

parse a string to time.Time

func ParseTimeUTC

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

parse a string as "2006-01-02 15:04:05.999" to time.Time

func QuitSignal

func QuitSignal() <-chan os.Signal

recive quit signal

func RandInt64

func RandInt64(min, max int64) int64

生成m-n之间的随机数

func RndUuid

func RndUuid() string

基于时间ns和随机数实现唯一的uuid 在单台机器上是不会出现重复的uuid 如果要在分布式的架构上生成不重复的uuid 只需要在rndStr的前面加一些自定义的字符串 返回格式:eba1e8cd-0460-4910-49c6-44bdf3cf024d

func RndUuidMd5

func RndUuidMd5() string

func Round

func Round(f float64, n int) float64

对浮点数进行四舍五入操作比如 12.125保留2位小数==>12.13

func RunShell

func RunShell(exeStr string) (string, error)

运行shell脚本

func SetLogDir

func SetLogDir(dir string)

日志存放目录

func SetLogTime

func SetLogTime(logtime bool)

设置是否显示时间和行号 当logtime=false自定义日志格式

func SetLogTimeZone

func SetLogTimeZone(timezone string)

设置日志记录时区

func SetTimeZone

func SetTimeZone(zone string)

func Sha1

func Sha1(str string) string

func Stack

func Stack() []byte

获取完整的堆栈信息 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 StoreGobData(data interface{}, fileName string) error

func StrJoin

func StrJoin(sep string, str ...string) string

多个字符串按照指定的分隔符进行拼接

func StrJoinByBuf

func StrJoinByBuf(str ...string) string

通过buf缓冲区的方式连接字符串

func StrToInt

func StrToInt(s string) int

string-->int

func StrToInt64

func StrToInt64(s string) int64

string--> int64

func StringMapKeys

func StringMapKeys(m interface{}) (res []string)

func WarnLog

func WarnLog(v interface{})

func Xss

func Xss(str string) string

防止xss攻击

func XssUnescape

func XssUnescape(str string) string

Types

type ChanLock

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

func NewChanLock

func NewChanLock() *ChanLock

func (*ChanLock) Lock

func (l *ChanLock) Lock()

func (*ChanLock) TryLock

func (l *ChanLock) TryLock() bool

乐观锁实现

func (*ChanLock) TryLockTimeout

func (l *ChanLock) TryLockTimeout(timeout time.Duration) bool

指定时间内的乐观锁

func (*ChanLock) Unlock

func (l *ChanLock) Unlock()

type EmptyStruct

type EmptyStruct struct{}

zero size, empty struct

type Lock

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

func NewLock

func NewLock() *Lock

func (*Lock) Lock

func (this *Lock) Lock()

func (*Lock) SchedLock

func (this *Lock) SchedLock()

func (*Lock) SpinLock

func (this *Lock) SpinLock(count int) bool

不断地尝试原子地更新flag的值,直到操作成功为止

func (*Lock) TimeoutSchedLock

func (this *Lock) TimeoutSchedLock(t time.Duration) bool

func (*Lock) TimeoutSpinLock

func (this *Lock) TimeoutSpinLock(t time.Duration) bool

func (*Lock) TryLock

func (this *Lock) TryLock() bool

尝试枷锁

func (*Lock) Unlock

func (this *Lock) Unlock()

释放锁

type Mutex

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

func NewMutexLock

func NewMutexLock() *Mutex

创建lock实例

func (*Mutex) Lock

func (m *Mutex) Lock()

枷锁

func (*Mutex) TryLock

func (m *Mutex) TryLock() bool

尝试枷锁

func (*Mutex) Unlock

func (m *Mutex) Unlock()

解锁

type Semaphore

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

分配安全的bool chan

func NewSemaphore

func NewSemaphore(capa int) *Semaphore

func (*Semaphore) Alloc

func (this *Semaphore) Alloc()

func (*Semaphore) Free

func (this *Semaphore) Free() bool

func (*Semaphore) TryAlloc

func (this *Semaphore) TryAlloc() bool

type StringList

type StringList []string

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

Jump to

Keyboard shortcuts

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