fix data race in services/meta tests
parent
13e32f6880
commit
7ea00ea8d9
17
tcp/mux.go
17
tcp/mux.go
|
@ -18,6 +18,7 @@ const (
|
||||||
|
|
||||||
// Mux multiplexes a network connection.
|
// Mux multiplexes a network connection.
|
||||||
type Mux struct {
|
type Mux struct {
|
||||||
|
mu sync.RWMutex
|
||||||
ln net.Listener
|
ln net.Listener
|
||||||
m map[byte]*listener
|
m map[byte]*listener
|
||||||
|
|
||||||
|
@ -41,7 +42,9 @@ func NewMux() *Mux {
|
||||||
|
|
||||||
// Serve handles connections from ln and multiplexes then across registered listener.
|
// Serve handles connections from ln and multiplexes then across registered listener.
|
||||||
func (mux *Mux) Serve(ln net.Listener) error {
|
func (mux *Mux) Serve(ln net.Listener) error {
|
||||||
|
mux.mu.Lock()
|
||||||
mux.ln = ln
|
mux.ln = ln
|
||||||
|
mux.mu.Unlock()
|
||||||
for {
|
for {
|
||||||
// Wait for the next connection.
|
// Wait for the next connection.
|
||||||
// If it returns a temporary error then simply retry.
|
// If it returns a temporary error then simply retry.
|
||||||
|
@ -141,10 +144,18 @@ func (ln *listener) Close() error { return nil }
|
||||||
|
|
||||||
// Addr returns the Addr of the listener
|
// Addr returns the Addr of the listener
|
||||||
func (ln *listener) Addr() net.Addr {
|
func (ln *listener) Addr() net.Addr {
|
||||||
if ln.mux != nil && ln.mux.ln != nil {
|
if ln.mux == nil {
|
||||||
return ln.mux.ln.Addr()
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
ln.mux.mu.RLock()
|
||||||
|
defer ln.mux.mu.RUnlock()
|
||||||
|
|
||||||
|
if ln.mux.ln == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ln.mux.ln.Addr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dial connects to a remote mux listener with a given header byte.
|
// Dial connects to a remote mux listener with a given header byte.
|
||||||
|
|
Loading…
Reference in New Issue