mirror of
https://github.com/soheilhy/cmux.git
synced 2024-11-10 11:41:52 +08:00
Merge pull request #16 from soheilhy/devel
Close the connections buffered for the listeners
This commit is contained in:
commit
1a2fcbde3a
8
cmux.go
8
cmux.go
@ -98,6 +98,10 @@ func (m *cMux) Serve() error {
|
||||
|
||||
for _, sl := range m.sls {
|
||||
close(sl.l.connc)
|
||||
// Drain the connections enqueued for the listener.
|
||||
for c := range sl.l.connc {
|
||||
_ = c.Close()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@ -126,12 +130,8 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
|
||||
if matched {
|
||||
select {
|
||||
case sl.l.connc <- muc:
|
||||
default:
|
||||
select {
|
||||
case <-donec:
|
||||
_ = c.Close()
|
||||
default:
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
13
cmux_test.go
13
cmux_test.go
@ -262,10 +262,11 @@ func TestHTTP2(t *testing.T) {
|
||||
})
|
||||
h2l := muxl.Match(HTTP2())
|
||||
go safeServe(errCh, muxl)
|
||||
muxedConn, err := h2l.Accept()
|
||||
close(l.connCh)
|
||||
if muxedConn, err := h2l.Accept(); err != nil {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
}
|
||||
var b [len(http2.ClientPreface)]byte
|
||||
if _, err := muxedConn.Read(b[:]); err != io.EOF {
|
||||
t.Fatal(err)
|
||||
@ -273,7 +274,6 @@ func TestHTTP2(t *testing.T) {
|
||||
if string(b[:]) != http2.ClientPreface {
|
||||
t.Errorf("got unexpected read %s, expected %s", b, http2.ClientPreface)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPGoRPC(t *testing.T) {
|
||||
@ -374,10 +374,15 @@ func TestClose(t *testing.T) {
|
||||
// Listener is closed.
|
||||
close(l.connCh)
|
||||
|
||||
// Second connection goes through.
|
||||
// Second connection either goes through or it is closed.
|
||||
if _, err := anyl.Accept(); err != nil {
|
||||
if err != ErrListenerClosed {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c2.Read([]byte{}); err != io.ErrClosedPipe {
|
||||
t.Fatalf("connection is not closed and is leaked: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cribbed from google.golang.org/grpc/test/end2end_test.go.
|
||||
|
Loading…
Reference in New Issue
Block a user