From 4fbc42db2ef3a20287993d231da481bb6bb8353f Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Thu, 12 Jul 2018 17:39:21 +0800 Subject: [PATCH] close chan instead of send value --- conn.go | 18 +++++++++++++----- stream.go | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/conn.go b/conn.go index 61756f1..ca615ad 100644 --- a/conn.go +++ b/conn.go @@ -118,7 +118,7 @@ func (c *ClientConn) run() { c.lock.Lock() c.err = err c.lock.Unlock() - errch <- struct{}{} + close(errch) break } //log.Printf("read %d bytes from network", n) @@ -132,9 +132,12 @@ func (c *ClientConn) run() { C.free(d1) if int(ret1) < 0 { + c.lock.Lock() c.err = fmt.Errorf("sesion recv error: %s", C.GoString(C.nghttp2_strerror(ret))) //log.Println(c.err) + c.lock.Unlock() + close(errch) break } } @@ -184,7 +187,7 @@ loop: C.GoString(C.nghttp2_strerror(ret))) c.lock.Unlock() //log.Println(c.err) - errch <- struct{}{} + close(errch) break } @@ -246,10 +249,12 @@ func (c *ClientConn) Connect(addr string) (cs *ClientStream, statusCode int, err errch: make(chan error), lock: new(sync.Mutex), } + c.lock.Lock() c.streams[int(streamID)] = s c.streamCount++ c.lock.Unlock() + //log.Printf("new stream id %d", int(streamID)) select { case err := <-s.errch: @@ -374,6 +379,9 @@ func (c *ClientConn) CreateRequest(req *http.Request) (*http.Response, error) { // CanTakeNewRequest check if the ClientConn can submit a new request func (c *ClientConn) CanTakeNewRequest() bool { + c.lock.Lock() + c.lock.Unlock() + if c.closed { return false } @@ -516,7 +524,7 @@ func (c *ServerConn) Run() { c.lock.Lock() c.err = err c.lock.Unlock() - errch <- struct{}{} + close(errch) break } @@ -534,7 +542,7 @@ func (c *ServerConn) Run() { C.GoString(C.nghttp2_strerror(ret))) c.lock.Unlock() //log.Println(c.err) - errch <- struct{}{} + close(errch) break } } @@ -562,7 +570,7 @@ loop: C.GoString(C.nghttp2_strerror(ret))) c.lock.Unlock() //log.Println(c.err) - errch <- struct{}{} + close(errch) break } diff --git a/stream.go b/stream.go index 67cbdea..e6e22ae 100644 --- a/stream.go +++ b/stream.go @@ -85,10 +85,11 @@ func (s *ClientStream) Close() error { } s.conn.lock.Lock() - defer s.conn.lock.Unlock() if _, ok := s.conn.streams[s.streamID]; ok { delete(s.conn.streams, s.streamID) } + s.conn.lock.Unlock() + return nil }