zenazn/goji — pull request #91 by mredivo (2014-11-25 00:05:11+00:00)
Title: Prevent shutdown race, and refuse new requests.

A better explanation of why "go run -race" reports a race condition:

* main routine kicks off goroutine #6 at graceful/signal.go(30): **go waitForSignal()**
* main routine accesses the "wg" variable at graceful/net.go(61): **wg.Add(1)**
* time passes; SIGINT arrives
* goroutine #6 accesses the "wg" variable at graceful/signal.go(102): **wg.Wait()**

The access to variable "wg" is not synchronized between the main routine and goroutine #6, so a race is reported.

If you synchronize those two accesses, you eliminate the race warning.

If at graceful/net.go(61) you can detect that graceful/signal.go(102) has been reached (due to imminent shutdown) and then abort the connection request, you have also handled the connection/shutdown race.

This patch addresses both those issues.

It eliminates the race, but I have not confirmed that it refuses new requests. You can check the methodology in the playground with the following code:

```
package main

import (
"fmt"
)

var eoj = make(chan struct{}, 1)

func foo() {
select {
case _, ok := <-eoj:
fmt.Println("foo:", ok)
default:
fmt.Println("default")
}
}

func main() {
foo()
foo()
select {
case eoj <- struct{}{}:
default:
}
foo()
foo()
close(eoj)
foo()
foo()
}
```