-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo.go
65 lines (53 loc) · 2.14 KB
/
fifo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package shutdown
import (
"context"
"sync"
"go.uber.org/multierr"
)
// Fifo is a struct that manages a queue of resources that need to be closed, in First-In-First-Out order.
type Fifo struct {
queue []Closer // The list of resources to close
mx sync.Mutex // Mutex for thread safety
}
// Append adds a new closer to the end of the Fifo queue.
func (f *Fifo) Append(closer Closer) {
f.mx.Lock() // Acquiring the lock
defer f.mx.Unlock() // Making sure to release the lock after the function exits
f.queue = append(f.queue, closer)
}
// CloseContext attempts to close each resource in the Fifo queue with context support.
func (f *Fifo) CloseContext(ctx context.Context) error {
f.mx.Lock() // Acquiring the lock
defer f.mx.Unlock() // Making sure to release the lock after the function exits
var errs error // This will store the accumulated errors
for _, closer := range f.queue {
next := make(chan struct{}) // Channel to signal completion of the closer
go func() {
callClose(closer, &errs) // Call the close function and gather errors if any
close(next)
}()
select {
case <-ctx.Done():
// If the context is cancelled or times out, return the accumulated errors and the context error
return multierr.Append(errs, ctx.Err())
case <-next:
// Move to the next closer in the queue after the current one finishes
}
}
return errs // Return the accumulated errors
}
// Close attempts to close all resources in the Fifo queue without context support.
func (f *Fifo) Close() error {
return f.CloseContext(context.Background()) // Using a background context which will never be cancelled
}
// WithContext associates the Fifo instance with the given context.
// It utilizes the ClosureToContext function to store the Fifo instance as a Closure within the context.
func (f *Fifo) WithContext(ctx context.Context) context.Context {
return ClosureToContext(ctx, f)
}
// callClose safely calls the Close method of the given closer and appends any errors.
func callClose(closer Closer, errs *error) {
if err := closer.Close(); err != nil {
*errs = multierr.Append(*errs, err) // Accumulate the error if Close method fails
}
}