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/xio/util.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'skate/xio/util.go') 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