Skip to content

Commit

Permalink
use and swap two buffer (#8)
Browse files Browse the repository at this point in the history
* log new buffer

* use rotate buffers
  • Loading branch information
secmask authored Oct 1, 2024
1 parent 2c6b57c commit f65b8c5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
1 change: 0 additions & 1 deletion lib/agent/agent.go

This file was deleted.

32 changes: 18 additions & 14 deletions lib/agent/logholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion lib/agent/server.go

This file was deleted.

7 changes: 2 additions & 5 deletions lib/client/async_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f65b8c5

Please sign in to comment.