Skip to content

Commit

Permalink
fix consumer race
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Dec 8, 2024
1 parent b4afa20 commit 16ac6f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion protocol/lavasession/consumer_session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (csm *ConsumerSessionManager) UpdateAllProviders(epoch uint64, pairingList
}
csm.setValidAddressesToDefaultValue("", nil) // the starting point is that valid addresses are equal to pairing addresses.
// reset session related metrics
csm.consumerMetricsManager.ResetSessionRelatedMetrics()
go csm.consumerMetricsManager.ResetSessionRelatedMetrics()
go csm.providerOptimizer.UpdateWeights(CalcWeightsByStake(pairingList), epoch)

utils.LavaFormatDebug("updated providers", utils.Attribute{Key: "epoch", Value: epoch}, utils.Attribute{Key: "spec", Value: csm.rpcEndpoint.Key()})
Expand Down
9 changes: 7 additions & 2 deletions protocol/provideroptimizer/selection_weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func NewSelectionWeighter() SelectionWeighter {
func (sw *selectionWeighterInst) Weight(address string) int64 {
sw.lock.RLock()
defer sw.lock.RUnlock()
return sw.weightInner(address)
}

// assumes lock is held
func (sw *selectionWeighterInst) weightInner(address string) int64 {
weight, ok := sw.weights[address]
if !ok {
// default weight is 1
Expand All @@ -52,12 +57,12 @@ func (sw *selectionWeighterInst) WeightedChoice(entries []Entry) string {
defer sw.lock.RUnlock()
totalWeight := int64(0)
for _, entry := range entries {
totalWeight += int64(float64(sw.Weight(entry.Address)) * entry.Part)
totalWeight += int64(float64(sw.weightInner(entry.Address)) * entry.Part)
}
randWeight := rand.Int63n(totalWeight)
currentWeight := int64(0)
for _, entry := range entries {
currentWeight += int64(float64(sw.Weight(entry.Address)) * entry.Part)
currentWeight += int64(float64(sw.weightInner(entry.Address)) * entry.Part)
if currentWeight > randWeight {
return entry.Address
}
Expand Down

0 comments on commit 16ac6f1

Please sign in to comment.