Skip to content

Commit

Permalink
BCF-2652: upgrade sync.Once to sync.Func/Value(s) (#11040)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Oct 24, 2023
1 parent a5b3fd7 commit 61e5564
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 64 deletions.
20 changes: 7 additions & 13 deletions core/logger/zap_disk_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,15 @@ func newRotatingFileLogger(zcfg zap.Config, c Config, cores ...zapcore.Core) (*z

go lggr.pollDiskSpace()

var once sync.Once
closeLogger := func() error {
var innerErr error
once.Do(func() {
defer defaultCloseFn()
defer diskCloseFn()
closeLogger := sync.OnceValue(func() error {
defer defaultCloseFn()
defer diskCloseFn()

close(lggr.pollDiskSpaceStop)
<-lggr.pollDiskSpaceDone
close(lggr.pollDiskSpaceStop)
<-lggr.pollDiskSpaceDone

innerErr = lggr.Sync()
})

return innerErr
}
return lggr.Sync()
})

return lggr, closeLogger, err
}
28 changes: 12 additions & 16 deletions core/services/ocr2/validate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,16 @@ func ToLocalConfig(ocr2Config OCR2Config, insConf InsecureConfig, spec job.OCR2O
return lc, nil
}

var (
minOCR2MaxDurationQuery = 100 * time.Millisecond
minOCR2MaxDurationQueryErr error
minOCR2MaxDurationQueryOnce sync.Once
)
const defaultMinOCR2MaxDurationQuery = 100 * time.Millisecond

func getMinOCR2MaxDurationQuery() (time.Duration, error) {
minOCR2MaxDurationQueryOnce.Do(func() {
if v := env.MinOCR2MaxDurationQuery.Get(); v != "" {
minOCR2MaxDurationQuery, minOCR2MaxDurationQueryErr = time.ParseDuration(v)
if minOCR2MaxDurationQueryErr != nil {
minOCR2MaxDurationQueryErr = fmt.Errorf("failed to parse %s: %w", env.MinOCR2MaxDurationQuery, minOCR2MaxDurationQueryErr)
}
}
})
return minOCR2MaxDurationQuery, minOCR2MaxDurationQueryErr
}
var getMinOCR2MaxDurationQuery = sync.OnceValues(func() (time.Duration, error) {
str := env.MinOCR2MaxDurationQuery.Get()
if str == "" {
return defaultMinOCR2MaxDurationQuery, nil
}
d, err := time.ParseDuration(str)
if err != nil {
return -1, fmt.Errorf("failed to parse %s: %w", env.MinOCR2MaxDurationQuery, err)
}
return d, nil
})
12 changes: 5 additions & 7 deletions core/services/pg/q.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ func (q Q) GetNamed(sql string, dest interface{}, arg interface{}) error {
}

func (q Q) newQueryLogger(query string, args []interface{}) *queryLogger {
return &queryLogger{Q: q, query: query, args: args}
return &queryLogger{Q: q, query: query, args: args, str: sync.OnceValue(func() string {
return sprintQ(query, args)
})}
}

// sprintQ formats the query with the given args and returns the resulting string.
Expand Down Expand Up @@ -315,15 +317,11 @@ type queryLogger struct {
query string
args []interface{}

str string
strOnce sync.Once
str func() string
}

func (q *queryLogger) String() string {
q.strOnce.Do(func() {
q.str = sprintQ(q.query, q.args)
})
return q.str
return q.str()
}

func (q *queryLogger) logSqlQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,8 @@ type abiValues struct {
randomnessRequestRawDataArgs abi.Arguments
}

var dontUseThisUseGetterFunctionsAbove abiValues
var parseABIOnce sync.Once

func coordinatorABIValues() *abiValues {
parseABIOnce.Do(readCoordinatorABI)
return &dontUseThisUseGetterFunctionsAbove
}

func readCoordinatorABI() {
v := &dontUseThisUseGetterFunctionsAbove
var coordinatorABIValues = sync.OnceValue(func() (v *abiValues) {
v = new(abiValues)
var err error
v.coordinatorABI, err = abi.JSON(strings.NewReader(
solidity_vrf_coordinator_interface.VRFCoordinatorABI))
Expand All @@ -57,8 +49,7 @@ func readCoordinatorABI() {
var found bool
v.fulfillMethod, found = v.coordinatorABI.Methods[fulfillMethodName]
if !found {
panic(fmt.Errorf("could not find method %s in VRFCoordinator ABI",
fulfillMethodName))
panic(fmt.Errorf("could not find method %s in VRFCoordinator ABI", fulfillMethodName))
}
v.fulfillSelector = hexutil.Encode(v.fulfillMethod.ID)
randomnessRequestABI := v.coordinatorABI.Events["RandomnessRequest"]
Expand All @@ -68,4 +59,5 @@ func readCoordinatorABI() {
v.randomnessRequestRawDataArgs = append(v.randomnessRequestRawDataArgs, arg)
}
}
}
return
})
18 changes: 7 additions & 11 deletions core/utils/lazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
type LazyLoad[T any] struct {
f func() (T, error)
state T
ok bool
lock sync.Mutex
once sync.Once
}

func NewLazyLoad[T any](f func() (T, error)) *LazyLoad[T] {
Expand All @@ -21,20 +21,16 @@ func (l *LazyLoad[T]) Get() (out T, err error) {
l.lock.Lock()
defer l.lock.Unlock()

// fetch only once (or whenever cleared)
l.once.Do(func() {
l.state, err = l.f()
})
// if err, clear so next get will retry
if err != nil {
l.once = sync.Once{}
if l.ok {
return l.state, nil
}
out = l.state
return
l.state, err = l.f()
l.ok = err == nil
return l.state, err
}

func (l *LazyLoad[T]) Reset() {
l.lock.Lock()
defer l.lock.Unlock()
l.once = sync.Once{}
l.ok = false
}
5 changes: 1 addition & 4 deletions integration-tests/reorg/reorg_confirmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type ReorgController struct {
initConsensusReady chan struct{}
reorgStarted chan struct{}
depthReached chan struct{}
once *sync.Once
once sync.Once
mutex sync.Mutex
ctx context.Context
cancel context.CancelFunc
Expand All @@ -82,11 +82,8 @@ func NewReorgController(cfg *ReorgConfig) (*ReorgController, error) {
initConsensusReady: make(chan struct{}, 100),
reorgStarted: make(chan struct{}, 100),
depthReached: make(chan struct{}, 100),
once: &sync.Once{},
mutex: sync.Mutex{},
ctx: ctx,
cancel: ctxCancel,
complete: false,
}
rc.networkStep.Store(InitConsensus)
for _, c := range cfg.Network.GetClients() {
Expand Down

0 comments on commit 61e5564

Please sign in to comment.