Skip to content

Commit

Permalink
Lock agents repo map on Get
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne0nd0g committed Oct 15, 2024
1 parent 90ef16b commit 30bb87f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/agents/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (

// Repository structure implements an in-memory database that holds a map of agent's the server communicates with
type Repository struct {
// Don't use pointers because this is map is the source and should only be modified here in the repository
// Don't use pointers because this is a map is the source and should only be modified here in the repository
agents map[uuid.UUID]agents.Agent
sync.Mutex
}
Expand All @@ -64,7 +64,7 @@ func (r *Repository) Add(agent agents.Agent) error {
return ErrAgentExists
}

// AddLinkedAgent updates the Agent's linkedAgents list the contains all child agents for which it is the parent
// AddLinkedAgent updates the Agent's linkedAgents list that contains all child agents for which it is the parent
func (r *Repository) AddLinkedAgent(id uuid.UUID, link uuid.UUID) error {
agent, err := r.Get(id)
if err != nil {
Expand All @@ -79,6 +79,8 @@ func (r *Repository) AddLinkedAgent(id uuid.UUID, link uuid.UUID) error {

// Exists check's to see if the Agent is in the repository
func (r *Repository) Exists(id uuid.UUID) bool {
r.Lock()
defer r.Unlock()
for a := range r.agents {
if a == id {
return true
Expand All @@ -90,7 +92,9 @@ func (r *Repository) Exists(id uuid.UUID) bool {
// Get returns a COPY of the Agent entity. The caller should not try to modify the copy as it won't be updated
// in the repository
func (r *Repository) Get(id uuid.UUID) (agents.Agent, error) {
r.Lock()
agent, ok := r.agents[id]
r.Unlock()
if ok {
return agent, nil
}
Expand Down

0 comments on commit 30bb87f

Please sign in to comment.