-
Notifications
You must be signed in to change notification settings - Fork 1
/
tee-writer.go
84 lines (71 loc) · 1.86 KB
/
tee-writer.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
© 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
// TeeWriter is a writer that copies its writes to one or more other writers.
package pio
import (
"io"
"sync"
"sync/atomic"
"github.com/haraldrudell/parl"
"github.com/haraldrudell/parl/perrors"
)
// TeeWriter is a writer that copies its writes to one or more other writers.
type TeeWriter struct {
closeCallback func() (err error)
writers []io.Writer
isClosed atomic.Bool
wg sync.WaitGroup
}
// TeeWriter is a writer that copies its writes to one or more other writers.
func NewTeeWriter(closeCallback func() (err error), writers ...io.Writer) (teeWriter io.WriteCloser) {
length := len(writers)
if length == 0 {
panic(perrors.NewPF("Must have one or more writers, writers is empty"))
}
t := TeeWriter{closeCallback: closeCallback, writers: make([]io.Writer, length)}
for i, w := range writers {
if w == nil {
panic(perrors.ErrorfPF("Writers#%d nil", i))
}
t.writers[i] = w
}
t.wg.Add(1)
return &t
}
func (tw *TeeWriter) Write(p []byte) (n int, err error) {
if tw.isClosed.Load() {
err = perrors.NewPF("Write after Close")
return
}
length := len(p)
for _, writer := range tw.writers {
written := 0
for written < length {
n, err = writer.Write(p)
written += n
if err != nil {
return // write error return
}
}
}
return // good write return
}
func (w *TeeWriter) Close() (err error) {
// prevent multiple Close invocations
if !w.isClosed.CompareAndSwap(false, true) {
err = perrors.NewPF("Second Close invocation")
return
}
// invoke callback if there is one
if w.closeCallback != nil {
err = w.invokeCallback()
}
return
}
func (w *TeeWriter) invokeCallback() (err error) {
defer parl.RecoverErr(func() parl.DA { return parl.A() }, &err)
err = w.closeCallback()
return
}