fix data races around use of l.closed

fixes #1
This commit is contained in:
Naitik Shah 2013-03-27 18:29:36 -07:00
parent 3efb5417dc
commit 40271cd1a7

View File

@ -59,7 +59,6 @@ type Listener interface {
// A goroutine based counter that provides graceful Close for listeners.
type listener struct {
FileListener
closed bool // Indicates we're already closed.
closeRequest chan bool // Send a bool here to indicate we want to Close.
allClosed chan bool // Receive from here will indicate a clean Close.
counter chan bool // Use the inc/dec counters.
@ -94,7 +93,7 @@ func (l *listener) enabler() {
for {
select {
case <-l.closeRequest:
l.closed = true
l.closeRequest = nil
case change = <-l.counter:
if change == inc {
counter++
@ -102,19 +101,20 @@ func (l *listener) enabler() {
counter--
}
}
if l.closed && counter == 0 {
l.allClosed <- true
if l.closeRequest == nil && counter == 0 {
close(l.allClosed)
break
}
}
}
func (l *listener) CloseRequest() {
if l.closed == true {
select {
case l.closeRequest <- true:
<-l.allClosed
case <-l.allClosed:
return
}
l.closeRequest <- true
<-l.allClosed
}
func (l *listener) Close() error {
@ -123,9 +123,10 @@ func (l *listener) Close() error {
}
func (l *listener) Accept() (net.Conn, error) {
if l.closed == true {
select {
case <-l.allClosed:
return nil, ErrAlreadyClosed
}
default:
c, err := l.FileListener.Accept()
if err != nil {
if strings.HasSuffix(err.Error(), errClosed) {
@ -139,6 +140,7 @@ func (l *listener) Accept() (net.Conn, error) {
counter: l.counter,
}, nil
}
}
// Wait for signals to gracefully terminate or restart the process.
func Wait(listeners []Listener) (err error) {