Revert "Upgrade to go 1.19 and update dependencies (#3069)" (#3085)

This reverts commit bba7c23957.
This commit is contained in:
WithoutPants
2022-11-07 12:33:15 +11:00
committed by GitHub
parent bba7c23957
commit 2609095c7a
939 changed files with 43785 additions and 101302 deletions

View File

@@ -4,56 +4,36 @@ import (
"sync"
)
type CloseFunc func()
type CloseFuncWithError func() error
// CloseFunc is a method that closes something
type CloseFunc func() error
// Closer is an object that can close several things
type Closer struct {
closed bool
fs []CloseFuncWithError
// We need to split into 2 mutexes to allow using .Add() in .Do()
mc *sync.Mutex // Locks .Close()
mf *sync.Mutex // Locks fs
onClosed CloserOnClosed
fs []CloseFunc
m *sync.Mutex
}
type CloserOnClosed func(err error)
// NewCloser creates a new closer
func NewCloser() *Closer {
return &Closer{
mc: &sync.Mutex{},
mf: &sync.Mutex{},
m: &sync.Mutex{},
}
}
// Close implements the io.Closer interface
func (c *Closer) Close() error {
// Lock
c.mc.Lock()
defer c.mc.Unlock()
// Get funcs
c.mf.Lock()
fs := c.fs
c.mf.Unlock()
c.m.Lock()
defer c.m.Unlock()
// Loop through closers
err := NewErrors()
for _, f := range fs {
for _, f := range c.fs {
err.Add(f())
}
// Reset closers
c.fs = []CloseFuncWithError{}
// Update attribute
c.closed = true
// Callback
if c.onClosed != nil {
c.onClosed(err)
}
c.fs = []CloseFunc{}
// Return
if err.IsNil() {
@@ -62,65 +42,16 @@ func (c *Closer) Close() error {
return err
}
// Add adds a close func at the beginning of the list
func (c *Closer) Add(f CloseFunc) {
c.AddWithError(func() error {
f()
return nil
})
}
func (c *Closer) AddWithError(f CloseFuncWithError) {
// Lock
c.mf.Lock()
defer c.mf.Unlock()
// Append
c.fs = append([]CloseFuncWithError{f}, c.fs...)
}
func (c *Closer) Append(dst *Closer) {
// Lock
c.mf.Lock()
dst.mf.Lock()
defer c.mf.Unlock()
defer dst.mf.Unlock()
// Append
c.fs = append(c.fs, dst.fs...)
c.m.Lock()
defer c.m.Unlock()
c.fs = append([]CloseFunc{f}, c.fs...)
}
// NewChild creates a new child closer
func (c *Closer) NewChild() (child *Closer) {
child = NewCloser()
c.AddWithError(child.Close)
c.Add(child.Close)
return
}
// Do executes a callback while ensuring :
// - closer hasn't been closed before
// - closer can't be closed in between
func (c *Closer) Do(fn func()) {
// Lock
c.mc.Lock()
defer c.mc.Unlock()
// Closer already closed
if c.closed {
return
}
// Callback
fn()
}
func (c *Closer) OnClosed(fn CloserOnClosed) {
c.mc.Lock()
defer c.mc.Unlock()
c.onClosed = fn
}
func (c *Closer) IsClosed() bool {
c.mc.Lock()
defer c.mc.Unlock()
return c.closed
}