未验证 提交 4e58e9d7 编写于 作者: D Davies Liu 提交者: GitHub

improve performance of accesslog (#155)

上级 efce9537
......@@ -21,24 +21,26 @@ import (
"time"
)
const (
maxLineLength = 1000
)
type logReader struct {
sync.Mutex
buffer chan []byte
last []byte
}
var (
readerLock sync.Mutex
readers map[uint64]chan []byte
readers map[uint64]*logReader
)
func init() {
readers = make(map[uint64]chan []byte)
readers = make(map[uint64]*logReader)
}
func logit(ctx Context, format string, args ...interface{}) {
used := ctx.Duration()
readerLock.Lock()
defer readerLock.Unlock()
if len(readers) == 0 || used > time.Second*10 {
if len(readers) == 0 && used < time.Second*10 {
return
}
......@@ -46,14 +48,14 @@ func logit(ctx Context, format string, args ...interface{}) {
t := time.Now()
ts := t.Format("2006.01.02 15:04:05.000000")
cmd += fmt.Sprintf(" <%.6f>", used.Seconds())
if ctx.Pid() != 0 && used > time.Second*10 {
if ctx.Pid() != 0 && used >= time.Second*10 {
logger.Infof("slow operation: %s", cmd)
}
line := []byte(fmt.Sprintf("%s [uid:%d,gid:%d,pid:%d] %s\n", ts, ctx.Uid(), ctx.Gid(), ctx.Pid(), cmd))
for _, ch := range readers {
for _, r := range readers {
select {
case ch <- line:
case r.buffer <- line:
default:
}
}
......@@ -62,7 +64,7 @@ func logit(ctx Context, format string, args ...interface{}) {
func openAccessLog(fh uint64) uint64 {
readerLock.Lock()
defer readerLock.Unlock()
readers[fh] = make(chan []byte, 1024)
readers[fh] = &logReader{buffer: make(chan []byte, 10240)}
return fh
}
......@@ -74,27 +76,35 @@ func closeAccessLog(fh uint64) {
func readAccessLog(fh uint64, buf []byte) int {
readerLock.Lock()
buffer, ok := readers[fh]
r, ok := readers[fh]
readerLock.Unlock()
if !ok {
return 0
}
r.Lock()
defer r.Unlock()
var n int
if len(r.last) > 0 {
n = copy(buf, r.last)
r.last = r.last[n:]
}
var t = time.NewTimer(time.Second)
select {
case l := <-buffer:
n = copy(buf, l)
for n+maxLineLength <= len(buf) {
select {
case l = <-buffer:
n += copy(buf[n:], l)
default:
defer t.Stop()
for n < len(buf) {
select {
case line := <-r.buffer:
l := copy(buf[n:], line)
n += l
if l < len(line) {
r.last = line[l:]
return n
}
case <-t.C:
if n == 0 {
n = copy(buf, []byte("#\n"))
}
return n
}
return n
case <-t.C:
n = copy(buf, []byte("#\n"))
}
return n
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册