Skip to content

Commit

Permalink
Change from Load and then Store to LoadOrStore
Browse files Browse the repository at this point in the history
  • Loading branch information
shleikes committed Sep 11, 2024
1 parent 30cf18b commit 11650ca
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions protocol/rpcconsumer/rpcconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,36 +239,34 @@ func (rpcc *RPCConsumer) Start(ctx context.Context, options *rpcConsumerStartOpt
var loaded bool
var err error

optimizer, loaded, err = optimizers.Load(chainID)
baseLatency := common.AverageWorldLatency / 2 // we want performance to be half our timeout or better
tempOptimizer := provideroptimizer.NewProviderOptimizer(options.strategy, averageBlockTime, baseLatency, options.maxConcurrentProviders)
optimizer, loaded, err = optimizers.LoadOrStore(chainID, tempOptimizer)
if err != nil {
optimizer = nil
return utils.LavaFormatError("failed loading optimizer", err, utils.LogAttr("endpoint", rpcEndpoint.Key()))
}
if !loaded {
// doesn't exist for this chain create a new one
baseLatency := common.AverageWorldLatency / 2 // we want performance to be half our timeout or better
optimizer = provideroptimizer.NewProviderOptimizer(options.strategy, averageBlockTime, baseLatency, options.maxConcurrentProviders)
optimizers.Store(chainID, optimizer)
if !loaded { // stored, use the new one
optimizer = tempOptimizer
}

consumerConsistency, loaded, err = consumerConsistencies.Load(chainID)
tempConsumerConsistency := NewConsumerConsistency(chainID)
consumerConsistency, loaded, err = consumerConsistencies.LoadOrStore(chainID, tempConsumerConsistency)
if err != nil {
return utils.LavaFormatError("failed loading consumer consistency", err, utils.LogAttr("endpoint", rpcEndpoint.Key()))
}
if !loaded { // doesn't exist for this chain create a new one
consumerConsistency = NewConsumerConsistency(chainID)
consumerConsistencies.Store(chainID, consumerConsistency)
if !loaded { // stored, use the new one
consumerConsistency = tempConsumerConsistency
}

finalizationConsensus, loaded, err = finalizationConsensuses.Load(chainID)
tempFinalizationConsensus := finalizationconsensus.NewFinalizationConsensus(rpcEndpoint.ChainID)
finalizationConsensus, loaded, err = finalizationConsensuses.LoadOrStore(chainID, tempFinalizationConsensus)
if err != nil {
return utils.LavaFormatError("failed loading finalization consensus", err, utils.LogAttr("endpoint", rpcEndpoint.Key()))
}
if !loaded {
// doesn't exist for this chain create a new one
finalizationConsensus = finalizationconsensus.NewFinalizationConsensus(rpcEndpoint.ChainID)
if !loaded { // stored, use the new one
finalizationConsensus = tempFinalizationConsensus
consumerStateTracker.RegisterFinalizationConsensusForUpdates(ctx, finalizationConsensus)
finalizationConsensuses.Store(chainID, finalizationConsensus)
}
return nil
}
Expand Down

0 comments on commit 11650ca

Please sign in to comment.