Skip to content

Commit

Permalink
Fix console buffer to only process 1 line at a time
Browse files Browse the repository at this point in the history
If a program sends too much data to the cache at once, we would miss the point we need to cap the cache
  • Loading branch information
LordRalex committed Jul 24, 2023
1 parent 3af12c4 commit 5aaab56
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package pufferpanel

import (
"github.com/pufferpanel/pufferpanel/v2/config"
"sync"
"time"
)

Expand All @@ -35,6 +36,7 @@ type MemoryCache struct {
Cache
Buffer []Message
Capacity int
Lock sync.Locker
}

func CreateCache() *MemoryCache {
Expand All @@ -45,6 +47,7 @@ func CreateCache() *MemoryCache {
return &MemoryCache{
Buffer: make([]Message, 0),
Capacity: capacity,
Lock: &sync.Mutex{},
}
}

Expand Down Expand Up @@ -72,6 +75,8 @@ func (c *MemoryCache) ReadFrom(startTime int64) (msg []string, lastTime int64) {
}

func (c *MemoryCache) Write(b []byte) (n int, err error) {
c.Lock.Lock()
defer c.Lock.Unlock()
if len(c.Buffer) == c.Capacity {
c.Buffer = c.Buffer[1:]
}
Expand Down

0 comments on commit 5aaab56

Please sign in to comment.