41 lines
714 B
Go
41 lines
714 B
Go
package closer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.optclblast.xyz/draincloud/draincloud-light/internal/logger"
|
|
)
|
|
|
|
var globalCloser *Closer = &Closer{
|
|
closeFns: make([]func() error, 0),
|
|
}
|
|
|
|
type Closer struct {
|
|
closeFns []func() error
|
|
}
|
|
|
|
func (c *Closer) Add(fn func() error) {
|
|
c.closeFns = append(c.closeFns, fn)
|
|
}
|
|
|
|
func (c *Closer) Close() error {
|
|
var commonErr error
|
|
for _, fn := range c.closeFns {
|
|
if err := fn(); err != nil {
|
|
logger.Error(context.Background(), "[closer][Close] error at close func call", logger.Err(err))
|
|
commonErr = errors.Join(commonErr, err)
|
|
}
|
|
}
|
|
|
|
return commonErr
|
|
}
|
|
|
|
func Add(fn func() error) {
|
|
globalCloser.Add(fn)
|
|
}
|
|
|
|
func Close() error {
|
|
return globalCloser.Close()
|
|
}
|