add Remove listener method on cmux
This commit is contained in:
parent
5ec6847320
commit
653c057958
30
cmux.go
30
cmux.go
|
@ -103,6 +103,8 @@ type CMux interface {
|
||||||
HandleError(ErrorHandler)
|
HandleError(ErrorHandler)
|
||||||
// sets a timeout for the read of matchers
|
// sets a timeout for the read of matchers
|
||||||
SetReadTimeout(time.Duration)
|
SetReadTimeout(time.Duration)
|
||||||
|
|
||||||
|
Remove(l net.Listener) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type matchersListener struct {
|
type matchersListener struct {
|
||||||
|
@ -216,6 +218,34 @@ func (m *cMux) Close() {
|
||||||
m.closeDoneChans()
|
m.closeDoneChans()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *cMux) Remove(l net.Listener) error {
|
||||||
|
|
||||||
|
if l == nil {
|
||||||
|
return errors.New("mux: nil listener")
|
||||||
|
}
|
||||||
|
if l == m.root {
|
||||||
|
return errors.New("mux: cannot remove root listener")
|
||||||
|
}
|
||||||
|
cl, ok := l.(muxListener)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("mux: listener %v is not a mux listener", l)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
for i, sl := range m.sls {
|
||||||
|
if sl.l.Listener == l {
|
||||||
|
|
||||||
|
cl.donec <- struct{}{}
|
||||||
|
m.sls = append(m.sls[:i], m.sls[i+1:]...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("mux: listener %v not found", l)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *cMux) closeDoneChans() {
|
func (m *cMux) closeDoneChans() {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
Loading…
Reference in New Issue