Skip to content

Commit

Permalink
fix snappy not close connection, fix ehang-io#456, fix ehang-io#584, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ffdfgdfg committed Oct 5, 2020
1 parent a741a4d commit f25bdb2
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/conn/snappy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package conn

import (
"errors"
"io"

"github.com/golang/snappy"
Expand All @@ -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
}

Expand All @@ -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
}

0 comments on commit f25bdb2

Please # to comment.