From f25bdb2b3aaac71a590a8d787f6873c7f83dda0f Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Tue, 6 Oct 2020 00:28:33 +0800 Subject: [PATCH] fix snappy not close connection, fix #456, fix #584, fix #600 --- lib/conn/snappy.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/conn/snappy.go b/lib/conn/snappy.go index 2ea10341..b4764abb 100644 --- a/lib/conn/snappy.go +++ b/lib/conn/snappy.go @@ -1,6 +1,7 @@ package conn import ( + "errors" "io" "github.com/golang/snappy" @@ -9,12 +10,14 @@ import ( type SnappyConn struct { w *snappy.Writer r *snappy.Reader + c io.Closer } func NewSnappyConn(conn io.ReadWriteCloser) *SnappyConn { c := new(SnappyConn) c.w = snappy.NewBufferedWriter(conn) c.r = snappy.NewReader(conn) + c.c = conn.(io.Closer) return c } @@ -35,6 +38,16 @@ func (s *SnappyConn) Read(b []byte) (n int, err error) { } func (s *SnappyConn) Close() error { - s.w.Close() - return s.w.Close() + err := s.w.Close() + err2 := s.c.Close() + if err != nil && err2 == nil { + return err + } + if err == nil && err2 != nil { + return err2 + } + if err != nil && err2 != nil { + return errors.New(err.Error() + err2.Error()) + } + return nil }