Repository: moby/moby
File: daemon/reload.go (Go)

Code before:

// reloadTxn is used to defer side effects of a config reload.
type reloadTxn struct {
	onCommit, onRollback []func() error
}

// OnCommit defers a function to be called when a config reload is being finalized.
// The error returned from cb is purely informational.
func (tx *reloadTxn) OnCommit(cb func() error) {
	tx.onCommit = append(tx.onCommit, cb)
}

// OnRollback defers a function to be called when a config reload is aborted.
// The error returned from cb is purely informational.
func (tx *reloadTxn) OnRollback(cb func() error) {
	tx.onCommit = append(tx.onRollback, cb)
}

func (tx *reloadTxn) run(cbs []func() error) (err error) {
	tx.onCommit = nil
	tx.onRollback = nil

	for _, cb := range cbs {
		err = errors.Join(err, cb())
	}
	return err
}

// Commit calls all functions registered with OnCommit.
// Any errors returned by the functions are collated into a
// *github.com/hashicorp/go-multierror.Error value.
func (tx *reloadTxn) Commit() error {
	return tx.run(tx.onCommit)
}

// Rollback calls all functions registered with OnRollback.
// Any errors returned by the functions are collated into a
// *github.com/hashicorp/go-multierror.Error value.
func (tx *reloadTxn) Rollback() error {
	return tx.run(tx.onRollback)
}

// Reload modifies the live daemon configuration from conf.
// conf is assumed to be a validated configuration.
//
// These are the settings that Reload changes:
// - Platform runtime
// - Daemon debug log level
// - Daemon max concurrent downloads
// - Daemon max concurrent uploads
// - Daemon max download attempts

Review comment (suggestion):
I always get confused as "`errors.Join` in a loop" != `[]errs{}` -> `errors.Join(errs...)`, but I think in this case, the equivalent _may be_ to collect in a slice first;


```go
var errs []error
for _, cb := range cbs {
    errs = append(errs, cb())
}
return errors.Join(errs...)
```



Code after:

// reloadTxn is used to defer side effects of a config reload.
type reloadTxn struct {
	onCommit, onRollback []func() error
}

// OnCommit defers a function to be called when a config reload is being finalized.
// The error returned from cb is purely informational.
func (tx *reloadTxn) OnCommit(cb func() error) {
	tx.onCommit = append(tx.onCommit, cb)
}

// OnRollback defers a function to be called when a config reload is aborted.
// The error returned from cb is purely informational.
func (tx *reloadTxn) OnRollback(cb func() error) {
	tx.onCommit = append(tx.onRollback, cb)
}

func (tx *reloadTxn) run(cbs []func() error) error {
	tx.onCommit = nil
	tx.onRollback = nil

	var errs []error
	for _, cb := range cbs {
		errs = append(errs, cb())
	}
	return errors.Join(errs...)
}

// Commit calls all functions registered with OnCommit.
// Any errors returned by the functions are collated into a
// *github.com/hashicorp/go-multierror.Error value.
func (tx *reloadTxn) Commit() error {
	return tx.run(tx.onCommit)
}

// Rollback calls all functions registered with OnRollback.
// Any errors returned by the functions are collated into a
// *github.com/hashicorp/go-multierror.Error value.
func (tx *reloadTxn) Rollback() error {
	return tx.run(tx.onRollback)
}

// Reload modifies the live daemon configuration from conf.
// conf is assumed to be a validated configuration.
//
// These are the settings that Reload changes:
// - Platform runtime
// - Daemon debug log level
// - Daemon max concurrent downloads
// - Daemon max concurrent uploads