mirror of
https://github.com/soheilhy/cmux.git
synced 2025-01-18 18:56:26 +08:00
Remove use of mutex around done chan
This commit is contained in:
parent
e13d1cbf02
commit
a192073df5
42
cmux.go
42
cmux.go
@ -113,7 +113,6 @@ type cMux struct {
|
|||||||
donec chan struct{}
|
donec chan struct{}
|
||||||
sls []matchersListener
|
sls []matchersListener
|
||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
mu sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchersToMatchWriters(matchers []Matcher) []MatchWriter {
|
func matchersToMatchWriters(matchers []Matcher) []MatchWriter {
|
||||||
@ -149,7 +148,7 @@ func (m *cMux) Serve() error {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
m.closeDoneChanLocked()
|
m.closeDoneChan()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
for _, sl := range m.sls {
|
for _, sl := range m.sls {
|
||||||
@ -162,13 +161,16 @@ func (m *cMux) Serve() error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-m.donec:
|
||||||
|
// cmux was closed with cmux.Close()
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
c, err := m.root.Accept()
|
c, err := m.root.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
select {
|
|
||||||
case <-m.getDoneChan():
|
|
||||||
// cmux was closed with cmux.Close()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if !m.handleErr(err) {
|
if !m.handleErr(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -197,7 +199,7 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case sl.l.connc <- muc:
|
case sl.l.connc <- muc:
|
||||||
case <-m.getDoneChan():
|
case <-m.donec:
|
||||||
_ = c.Close()
|
_ = c.Close()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -213,31 +215,15 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *cMux) Close() {
|
func (m *cMux) Close() {
|
||||||
m.mu.Lock()
|
m.closeDoneChan()
|
||||||
defer m.mu.Unlock()
|
|
||||||
m.closeDoneChanLocked()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *cMux) getDoneChan() chan struct{} {
|
func (m *cMux) closeDoneChan() {
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
return m.getDoneChanLocked()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *cMux) getDoneChanLocked() chan struct{} {
|
|
||||||
if m.donec == nil {
|
|
||||||
m.donec = make(chan struct{})
|
|
||||||
}
|
|
||||||
return m.donec
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *cMux) closeDoneChanLocked() {
|
|
||||||
ch := m.getDoneChanLocked()
|
|
||||||
select {
|
select {
|
||||||
case <-ch:
|
case <-m.donec:
|
||||||
// Already closed. Don't close again
|
// Already closed. Don't close again
|
||||||
default:
|
default:
|
||||||
close(ch)
|
close(m.donec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user