Skip to content

Commit

Permalink
Fix data race on service Proposal
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Litvinov <[email protected]>
  • Loading branch information
Zensey committed Mar 21, 2024
1 parent 1c6d7c3 commit fe40a6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
32 changes: 25 additions & 7 deletions core/service/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package service
import (
"sync"

"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -149,13 +150,15 @@ func NewInstance(

// Instance represents a run service
type Instance struct {
ID ID
state servicestate.State
stateLock sync.RWMutex
ProviderID identity.Identity
Type string
Options Options
service Service
ID ID
state servicestate.State
stateLock sync.RWMutex
ProviderID identity.Identity
Type string
Options Options
service Service

muProposal sync.Mutex
Proposal market.ServiceProposal
policyProvider policy.Provider
discovery Discovery
Expand Down Expand Up @@ -189,7 +192,9 @@ func (i *Instance) proposalWithCurrentLocation() market.ServiceProposal {
return i.Proposal
}

i.muProposal.Lock()
i.Proposal.Location = *market.NewLocation(location)
i.muProposal.Unlock()

return i.Proposal
}
Expand Down Expand Up @@ -237,3 +242,16 @@ func (i *Instance) toEvent() servicestate.AppEventServiceStatus {
Status: string(i.state),
}
}

// CopyProposal returns a copy of Proposal
func (i *Instance) CopyProposal() market.ServiceProposal {
i.muProposal.Lock()
defer i.muProposal.Unlock()

var proposal market.ServiceProposal
if err := copier.CopyWithOption(&proposal, i.Proposal, copier.Option{DeepCopy: true}); err != nil {
panic(err)
}

return proposal
}
5 changes: 3 additions & 2 deletions core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ func (k *Keeper) updateServices() {
// merge in the connection statistics
match, _ := k.getServiceByID(string(v.ID))

priced, err := k.deps.ProposalPricer.EnrichProposalWithPrice(v.Proposal)
proposal := v.CopyProposal()
priced, err := k.deps.ProposalPricer.EnrichProposalWithPrice(proposal)
if err != nil {
log.Warn().Msgf("could not load price for proposal %v(%v)", v.Proposal.ProviderID, v.Proposal.ServiceType)
log.Warn().Msgf("could not load price for proposal %v(%v)", proposal.ProviderID, proposal.ServiceType)
}

prop := contract.NewProposalDTO(priced)
Expand Down

0 comments on commit fe40a6d

Please sign in to comment.