summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/safewriter/writer.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/go/collectors/go.d.plugin/agent/safewriter/writer.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/agent/safewriter/writer.go b/src/go/collectors/go.d.plugin/agent/safewriter/writer.go
new file mode 100644
index 000000000..533c1055d
--- /dev/null
+++ b/src/go/collectors/go.d.plugin/agent/safewriter/writer.go
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package safewriter
+
+import (
+ "io"
+ "os"
+ "sync"
+)
+
+var Stdout = New(os.Stdout)
+
+func New(w io.Writer) io.Writer {
+ return &writer{
+ mx: &sync.Mutex{},
+ w: w,
+ }
+}
+
+type writer struct {
+ mx *sync.Mutex
+ w io.Writer
+}
+
+func (w *writer) Write(p []byte) (n int, err error) {
+ w.mx.Lock()
+ n, err = w.w.Write(p)
+ w.mx.Unlock()
+ return n, err
+}