Skip to content

Commit

Permalink
locks around certain structs
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Sep 3, 2024
1 parent 6b2d38e commit 35771fd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"regexp"
"runtime"
"strings"
"sync"
"syscall"

"github.com/google/uuid"
Expand All @@ -43,7 +44,6 @@ import (
//go:generate mv fake_environment_fixed.go fake_environment_test.go
type Environment interface {
NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo
// NewHostInfoWithContext(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo
GetHostname() (hostname string)
GetSystemUUID() (hostId string)
ReadDirectory(dir string, ext string) ([]string, error)
Expand All @@ -70,6 +70,7 @@ type EnvironmentType struct {
host *proto.HostInfo
virtualizationFunc func(ctx context.Context) (string, string, error)
isContainerFunc func() bool
hostMu sync.Mutex
}

type Process struct {
Expand Down Expand Up @@ -106,6 +107,7 @@ const (
IsContainerKey = "isContainer"
GetContainerIDKey = "GetContainerID"
GetSystemUUIDKey = "GetSystemUUIDKey"
ReleaseInfoFile = "/etc/os-release"
)

var (
Expand All @@ -125,6 +127,8 @@ func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, con

func (env *EnvironmentType) NewHostInfoWithContext(ctx context.Context, agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo {
defer ctx.Done()
env.hostMu.Lock()
defer env.hostMu.Unlock()
// temp cache measure
if env.host == nil || clearCache {
hostInformation, err := host.InfoWithContext(ctx)
Expand Down Expand Up @@ -154,7 +158,7 @@ func (env *EnvironmentType) NewHostInfoWithContext(ctx context.Context, agentVer
Partitons: disks,
Network: env.networks(),
Processor: env.processors(hostInformation.KernelArch),
Release: releaseInfo("/etc/os-release"),
Release: releaseInfo(ReleaseInfoFile),
Tags: *tags,
AgentAccessibleDirs: configDirs,
}
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/dataplane_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type DataPlaneStatus struct {
softwareDetails map[string]*proto.DataplaneSoftwareDetails
nginxConfigActivityStatuses map[string]*proto.AgentActivityStatus
softwareDetailsMutex sync.RWMutex
hostInfoMu sync.Mutex
processes []*core.Process
}

Expand All @@ -67,7 +68,6 @@ func NewDataPlaneStatus(config *config.Config, meta *proto.Metadata, binary core
tags: &config.Tags,
configDirs: config.ConfigDirs,
reportInterval: config.Dataplane.Status.ReportInterval,
softwareDetailsMutex: sync.RWMutex{},
nginxConfigActivityStatuses: make(map[string]*proto.AgentActivityStatus),
softwareDetails: make(map[string]*proto.DataplaneSoftwareDetails),
processes: processes,
Expand Down Expand Up @@ -214,6 +214,8 @@ func (dps *DataPlaneStatus) dataplaneStatus(forceDetails bool) *proto.DataplaneS

func (dps *DataPlaneStatus) hostInfo(send bool) (info *proto.HostInfo) {
// this sets send if we are forcing details, or it has been 24 hours since the last send
dps.hostInfoMu.Lock()
defer dps.hostInfoMu.Unlock()
hostInfo := dps.env.NewHostInfo(dps.version, dps.tags, dps.configDirs, send)
if !send && cmp.Equal(dps.envHostInfo, hostInfo) {
return nil
Expand All @@ -222,6 +224,7 @@ func (dps *DataPlaneStatus) hostInfo(send bool) (info *proto.HostInfo) {
dps.envHostInfo = hostInfo
log.Tracef("hostInfo: %v", hostInfo)


return hostInfo
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35771fd

Please sign in to comment.