diff --git a/lib/agent/agent.go b/lib/agent/agent.go deleted file mode 100644 index 4883155..0000000 --- a/lib/agent/agent.go +++ /dev/null @@ -1 +0,0 @@ -package agent diff --git a/lib/agent/logholder.go b/lib/agent/logholder.go index 0f7015f..d9b8dbc 100644 --- a/lib/agent/logholder.go +++ b/lib/agent/logholder.go @@ -7,35 +7,39 @@ import ( var ( BufferSize = 1024 * 1024 - BufferPool = sync.Pool{New: func() interface{} { - return bytes.NewBuffer(make([]byte, 0, BufferSize)) - }} ) +const bufferCount = 2 + type LogHolder struct { - buffer *bytes.Buffer - lock sync.Mutex + useIdx int + buffers [bufferCount]*bytes.Buffer + lock sync.RWMutex } func NewLogHolder() *LogHolder { return &LogHolder{ - buffer: BufferPool.Get().(*bytes.Buffer), + buffers: [bufferCount]*bytes.Buffer{ + bytes.NewBuffer(make([]byte, 0, BufferSize)), + bytes.NewBuffer(make([]byte, 0, BufferSize)), + }, } } func (b *LogHolder) Write(d []byte) { - b.lock.Lock() - defer b.lock.Unlock() - b.buffer.Write(d) + b.lock.RLock() + defer b.lock.RUnlock() + b.buffers[b.useIdx].Write(d) } -func (b *LogHolder) GetAndClear() (*bytes.Buffer, bool) { +func (b *LogHolder) GetAndSwapBuffer() (*bytes.Buffer, bool) { b.lock.Lock() defer b.lock.Unlock() - if b.buffer.Len() == 0 { + current := b.buffers[b.useIdx] + if current.Len() == 0 { return nil, false } - resp := b.buffer - b.buffer = BufferPool.Get().(*bytes.Buffer) - return resp, true + b.useIdx = (b.useIdx + 1) % bufferCount + b.buffers[b.useIdx].Reset() + return current, true } diff --git a/lib/agent/server.go b/lib/agent/server.go deleted file mode 100644 index 4883155..0000000 --- a/lib/agent/server.go +++ /dev/null @@ -1 +0,0 @@ -package agent diff --git a/lib/client/async_client.go b/lib/client/async_client.go index 1efb034..93f22ce 100644 --- a/lib/client/async_client.go +++ b/lib/client/async_client.go @@ -8,9 +8,8 @@ import ( "time" "github.com/KyberNetwork/cclog/lib/agent" - "github.com/pierrec/lz4/v3" - "github.com/KyberNetwork/cclog/lib/common" + "github.com/pierrec/lz4/v3" ) type SendFailedFn = func(error) @@ -122,10 +121,8 @@ func (l *AsyncLogClient) loop() { for { select { case <-tick.C: - if buffer, ok := l.logHolder.GetAndClear(); ok { + if buffer, ok := l.logHolder.GetAndSwapBuffer(); ok { write(buffer.Bytes()) - buffer.Reset() - agent.BufferPool.Put(buffer) } case <-l.closeChan: break