Cancel context of broken devices to release resources (#28423)
This commit is contained in:
parent
5220bbfa0b
commit
d96b045117
1 changed files with 17 additions and 7 deletions
24
cmd/setup.go
24
cmd/setup.go
|
|
@ -236,23 +236,28 @@ NEXT:
|
|||
type newFromConfFunc[T any] func(context.Context, string, map[string]any) (T, error)
|
||||
|
||||
func staticInstance[T any](typ string, cc config.Named, newFromConf newFromConfFunc[T], h config.Handler[T]) error {
|
||||
ctx := util.WithLogger(context.TODO(), util.NewLogger(cc.Name))
|
||||
ctx, cancel := context.WithCancel(util.WithLogger(context.TODO(), util.NewLogger(cc.Name))) //nolint:govet
|
||||
|
||||
instance, err := newFromConf(ctx, cc.Type, cc.Other)
|
||||
if err != nil {
|
||||
return &DeviceError{cc.Name, fmt.Errorf("cannot create %s '%s': %w", typ, cc.Name, err)}
|
||||
err = &DeviceError{cc.Name, fmt.Errorf("cannot create %s '%s': %w", typ, cc.Name, err)}
|
||||
}
|
||||
|
||||
if err := h.Add(config.NewStaticDevice(cc, instance)); err != nil {
|
||||
return &DeviceError{cc.Name, err}
|
||||
if e := h.Add(config.NewStaticDevice(cc, instance)); e != nil && err == nil {
|
||||
err = &DeviceError{cc.Name, e}
|
||||
}
|
||||
|
||||
return nil
|
||||
// release resources
|
||||
if err != nil {
|
||||
cancel()
|
||||
}
|
||||
|
||||
return err //nolint:govet
|
||||
}
|
||||
|
||||
func configurableInstance[T any](typ string, conf *config.Config, newFromConf newFromConfFunc[T], h config.Handler[T]) error {
|
||||
cc := conf.Named()
|
||||
ctx := util.WithLogger(context.TODO(), util.NewLogger(cc.Name))
|
||||
ctx, cancel := context.WithCancel(util.WithLogger(context.TODO(), util.NewLogger(cc.Name))) //nolint:govet
|
||||
|
||||
props, err := customDevice(cc.Other)
|
||||
if err != nil {
|
||||
|
|
@ -271,7 +276,12 @@ func configurableInstance[T any](typ string, conf *config.Config, newFromConf ne
|
|||
err = &DeviceError{cc.Name, e}
|
||||
}
|
||||
|
||||
return err
|
||||
// release resources
|
||||
if err != nil {
|
||||
cancel()
|
||||
}
|
||||
|
||||
return err //nolint:govet
|
||||
}
|
||||
|
||||
func configureMeters(static []config.Named, names ...string) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue