From 3a8af268f74a3abc2389306be030ee42c0fc7120 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Mon, 5 Jul 2021 23:24:47 +0200 Subject: add thread safe writer --- skate/xio/util.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'skate/xio/util.go') diff --git a/skate/xio/util.go b/skate/xio/util.go index 4fc8905..49f38a3 100644 --- a/skate/xio/util.go +++ b/skate/xio/util.go @@ -6,8 +6,28 @@ import ( "io" "os" "strings" + "sync" ) +// SingleWriter makes any writer thread safe. +type SingleWriter struct { + sync.Mutex + w io.Writer +} + +// NewSingleWriter returns an io.Writer that can be safely accessed by multiple +// goroutines. +func NewSingleWriter(w io.Writer) *SingleWriter { + return &SingleWriter{w: w} +} + +// Write wraps the underlying writer and gives exclusive access. +func (w *SingleWriter) Write(p []byte) (n int, err error) { + w.Lock() + defer w.Unlock() + return w.w.Write(p) +} + // OpenTwo opens two files. The caller needs to check for a single error only. func OpenTwo(f0, f1 string) (g0, g1 *os.File, err error) { if g0, err = os.Open(f0); err != nil { -- cgit v1.2.3 From e9a3ce1f7112a55bffeeb6d7a9dd26584da8276d Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Mon, 5 Jul 2021 23:38:38 +0200 Subject: we need a safe encoder, not just a safe writer --- skate/reduce.go | 2 +- skate/xio/util.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'skate/xio/util.go') diff --git a/skate/reduce.go b/skate/reduce.go index 8325bc0..61e8cd6 100644 --- a/skate/reduce.go +++ b/skate/reduce.go @@ -64,7 +64,7 @@ func groupLogf(g *zipkey.Group, s string, vs ...interface{}) { // match result, e.g. for doi matches. func ZippyExact(releases, refs io.Reader, matchResult MatchResult, w io.Writer) error { var ( - enc = json.NewEncoder(xio.NewSingleWriter(w)) + enc = xio.NewSafeEncoder(json.NewEncoder(w)) keyer = makeKeyFunc("\t", 1) i = 0 bref BiblioRef diff --git a/skate/xio/util.go b/skate/xio/util.go index 49f38a3..9f6cc26 100644 --- a/skate/xio/util.go +++ b/skate/xio/util.go @@ -9,6 +9,25 @@ import ( "sync" ) +type Encoder interface { + Encode(interface{}) error +} + +type SafeEncoder struct { + sync.Mutex + enc Encoder +} + +func NewSafeEncoder(enc Encoder) *SafeEncoder { + return &SafeEncoder{enc: enc} +} + +func (s *SafeEncoder) Encode(v interface{}) error { + s.Lock() + defer s.Unlock() + return s.enc.Encode(v) +} + // SingleWriter makes any writer thread safe. type SingleWriter struct { sync.Mutex -- cgit v1.2.3 From 15790f0be8af9b9cfe79d5de338d39cd164188c1 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Tue, 6 Jul 2021 01:25:45 +0200 Subject: util: cleanup encoder --- skate/xio/util.go | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'skate/xio/util.go') diff --git a/skate/xio/util.go b/skate/xio/util.go index 9f6cc26..49f38a3 100644 --- a/skate/xio/util.go +++ b/skate/xio/util.go @@ -9,25 +9,6 @@ import ( "sync" ) -type Encoder interface { - Encode(interface{}) error -} - -type SafeEncoder struct { - sync.Mutex - enc Encoder -} - -func NewSafeEncoder(enc Encoder) *SafeEncoder { - return &SafeEncoder{enc: enc} -} - -func (s *SafeEncoder) Encode(v interface{}) error { - s.Lock() - defer s.Unlock() - return s.enc.Encode(v) -} - // SingleWriter makes any writer thread safe. type SingleWriter struct { sync.Mutex -- cgit v1.2.3