Skip to content

Commit

Permalink
fix 并发时避免同时new对象
Browse files Browse the repository at this point in the history
  • Loading branch information
alex cai committed Nov 4, 2016
1 parent 06680d8 commit 32ae01a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

type asyncLogType struct {
files map[string]*LogFile // 下标是文件名

// 避免并发new对象
sync.Mutex
}

type LogFile struct {
Expand Down Expand Up @@ -110,7 +113,9 @@ func init() {
}

func NewLogFile(filename string) *LogFile {
asyncLog.Lock()
if lf, ok := asyncLog.files[filename]; ok {
asyncLog.Unlock()
return lf
}

Expand All @@ -119,6 +124,9 @@ func NewLogFile(filename string) *LogFile {
flag: StdFlag,
}

asyncLog.files[filename] = lf
asyncLog.Unlock()

// 默认按小时切割文件
lf.logRotate.rotate = RotateHour

Expand All @@ -127,8 +135,6 @@ func NewLogFile(filename string) *LogFile {

// TODO 同步的时间周期,缓存开启才有效
lf.sync.cycle = time.Second

asyncLog.files[filename] = lf
return lf
}

Expand Down

0 comments on commit 32ae01a

Please sign in to comment.