Skip to content

Commit

Permalink
Added a reaper to remove older entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
scudette committed Jan 2, 2025
1 parent 70ed006 commit 9ece251
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions reporting/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Velocidex/ordereddict"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/services/debug"
"www.velocidex.com/golang/velociraptor/utils"
"www.velocidex.com/golang/vfilter"
)

Expand All @@ -16,6 +17,8 @@ var (
)

type WriterInfo struct {
id uint64

Name string
TmpFile string
CompressedSize int
Expand All @@ -26,6 +29,7 @@ type WriterInfo struct {
}

type ContainerInfo struct {
id uint64
Name string
BackingFile string
Stats *api_proto.ContainerStats
Expand All @@ -45,7 +49,9 @@ func (self *_ContainerTracker) UpdateContainerWriter(
self.UpdateContainer(container_id, func(info *ContainerInfo) {
writer_info, pres := info.InFlightWriters[writer_id]
if !pres {
writer_info = &WriterInfo{}
writer_info = &WriterInfo{
id: writer_id,
}
info.InFlightWriters[writer_id] = writer_info
}

Expand All @@ -61,6 +67,7 @@ func (self *_ContainerTracker) UpdateContainer(
record, pres := self.containers[id]
if !pres {
record = &ContainerInfo{
id: id,
InFlightWriters: make(map[uint64]*WriterInfo),
}
self.containers[id] = record
Expand All @@ -69,6 +76,37 @@ func (self *_ContainerTracker) UpdateContainer(
cb(record)
}

func (self *_ContainerTracker) reap() {
self.mu.Lock()
defer self.mu.Unlock()

containers := make([]*ContainerInfo, 0, len(self.containers))
for _, c := range self.containers {
containers = append(containers, c)
}

oldest := utils.GetTime().Now().Add(-time.Minute * 10)
for _, c := range containers {
// reap old containers completely.
if !c.CloseTime.IsZero() &&
c.CloseTime.Before(oldest) {
delete(self.containers, c.id)
continue
}

writers := make([]*WriterInfo, 0, len(c.InFlightWriters))
for _, w := range c.InFlightWriters {
writers = append(writers, w)
}
for _, w := range writers {
if !w.Closed.IsZero() &&
w.Closed.Before(oldest) {
delete(c.InFlightWriters, w.id)
}
}
}
}

func (self *_ContainerTracker) WriteMetrics(
ctx context.Context, scope vfilter.Scope,
output_chan chan vfilter.Row) {
Expand All @@ -94,9 +132,18 @@ func (self *_ContainerTracker) WriteMetrics(
}

func NewContainerTracker() *_ContainerTracker {
return &_ContainerTracker{
res := &_ContainerTracker{
containers: make(map[uint64]*ContainerInfo),
}

go func() {
for {
time.Sleep(time.Minute)
res.reap()
}
}()

return res
}

func init() {
Expand Down

0 comments on commit 9ece251

Please sign in to comment.