-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloser.go
59 lines (48 loc) · 1.79 KB
/
closer.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
/*
© 2022–present Harald Rudell <[email protected]> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"io"
"github.com/haraldrudell/parl/perrors"
)
// Closer is a deferrable generic function closing a channel
// - ch: channel to close, closed or nil returns error
// - — [CloserSend] is same for send-only channel
// - errp non-nil: receives any panic using [perrors.AppendError]
// - thread-safe panic-free deferrable
// - if a thread is blocked in channel send for ch, close causes data race
func Closer[T any](ch chan T, errp *error) {
defer RecoverErr(func() DA { return A() }, errp)
close(ch)
}
// CloserSend is a deferrable function closing a send-only channel
// - ch: channel to close, closed or nil returns error
// - — similar to [Closer] but for send-only channel
// - errp non-nil: receives any panic using [perrors.AppendError]
// - thread-safe panic-free deferrable
// - if a thread is blocked in channel send for ch, close causes data race
func CloserSend[T any](ch chan<- T, errp *error) {
defer RecoverErr(func() DA { return A() }, errp)
close(ch)
}
// Close closes an [io.Closer] as deferred function
// - closable: type Closer interface { Close() error }
// - — nil returns error. Already closed may return error
// - errp: receives errors or panic via [perrors.AppendError]
// - thread-safe panic-free deferrable
func Close(closable io.Closer, errp *error) {
defer RecoverErr(func() DA { return A() }, errp)
var err error
if err = closable.Close(); err == nil {
return // successful close
}
// Close returned error
// ensure err has stack trace
if !perrors.HasStack(err) {
// the stack should begin with caller of parl.Close:
err = perrors.Stackn(err, 1)
}
*errp = perrors.AppendError(*errp, err)
}