Try to abstract the main part.

This commit is contained in:
zicla
2019-04-27 23:24:09 +08:00
parent 14b6a74666
commit 2732b14eae
14 changed files with 130 additions and 105 deletions

View File

@ -197,7 +197,7 @@ func (table *Table) checkExpire() {
table.cleanupInterval = smallestDuration
if smallestDuration > 0 {
table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
go util.SafeMethod(table.checkExpire)
go util.RunWithRecovery(table.checkExpire)
})
}
table.Unlock()

17
code/tool/inter/logger.go Normal file
View File

@ -0,0 +1,17 @@
package inter
//日志系统必须高保
//全局唯一的日志对象(在main函数中初始化)
var LOGGER Logger
type Logger interface {
//处理日志的统一方法。
Log(prefix string, format string, v ...interface{})
//不同级别的日志处理
Debug(format string, v ...interface{})
Info(format string, v ...interface{})
Warn(format string, v ...interface{})
Error(format string, v ...interface{})
Panic(format string, v ...interface{})
}

View File

@ -40,8 +40,7 @@ func GetDevHomePath() string {
panic("cannot get dev home path.")
}
fmt.Println(file)
//$DevHomePath/code/tool/util/util_file.go
dir := GetDirOfPath(file)
dir = GetDirOfPath(dir)
dir = GetDirOfPath(dir)
@ -69,9 +68,11 @@ func GetHomePath() string {
//如果exPath中包含了 \\AppData\\Local\\Temp 我们认为是在Win的开发环境中
systemUser, err := user.Current()
winDev := strings.HasPrefix(exPath, systemUser.HomeDir+"\\AppData\\Local\\Temp")
if winDev {
exPath = GetDevHomePath() + "/tmp"
if systemUser != nil {
winDev := strings.HasPrefix(exPath, systemUser.HomeDir+"\\AppData\\Local\\Temp")
if winDev {
exPath = GetDevHomePath() + "/tmp"
}
}
return exPath

View File

@ -1,16 +1,14 @@
package util
//带有panic恢复的方法
func PanicHandler() {
if err := recover(); err != nil {
//TODO 全局日志记录
//LOGGER.Error("异步任务错误: %v", err)
}
}
func RunWithRecovery(f func()) {
defer func() {
if err := recover(); err != nil {
//TODO 全局日志记录
//LOGGER.Error("异步任务错误: %v", err)
}
}()
//带有panic恢复的方法
func SafeMethod(f func()) {
defer PanicHandler()
//执行函数
f()
}