This commit is contained in:
Suyono 2023-09-05 10:20:49 +10:00
parent 6eada2d348
commit 9f7c004105
2 changed files with 0 additions and 31 deletions

View File

@ -1,26 +0,0 @@
package log
import "sync"
type MapWriter struct {
writers map[string]*Writer
mtx *sync.Mutex
}
func (m *MapWriter) GetWriter(key string) *Writer {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.writers[key]
}
func (m *MapWriter) Add(key string, w *Writer) {
if w == nil || len(key) == 0 {
return
}
m.mtx.Lock()
defer m.mtx.Unlock()
m.writers[key] = w
}

View File

@ -8,20 +8,16 @@ import (
type Writer struct {
writer io.Writer
mtx *sync.Mutex
wg *sync.WaitGroup
} //TODO: create a writer implementation using this struct, ensure sync and prevent abrupt truncation when stopping
func NewWriter(w io.Writer) *Writer {
return &Writer{
writer: w,
mtx: &sync.Mutex{},
wg: &sync.WaitGroup{},
}
}
func (w *Writer) Write(b []byte) (int, error) {
defer w.wg.Done()
w.mtx.Lock()
defer w.mtx.Unlock()
return w.writer.Write(b)
@ -32,5 +28,4 @@ func (w *Writer) Close() {
_ = wc.Close()
}
w.wg.Wait()
}