aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-07-05 23:38:38 +0200
committerMartin Czygan <martin.czygan@gmail.com>2021-07-05 23:38:38 +0200
commite9a3ce1f7112a55bffeeb6d7a9dd26584da8276d (patch)
tree490cfc38b56f77963bbb044f1c2d3b9e7fe551b5
parentdb954963e097238f9df13e6c7820f19d9d83f0f0 (diff)
downloadrefcat-e9a3ce1f7112a55bffeeb6d7a9dd26584da8276d.tar.gz
refcat-e9a3ce1f7112a55bffeeb6d7a9dd26584da8276d.zip
we need a safe encoder, not just a safe writer
-rw-r--r--skate/reduce.go2
-rw-r--r--skate/xio/util.go19
2 files changed, 20 insertions, 1 deletions
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