Skip to content

Commit

Permalink
Prevent shutdown race, and refuse new requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Redivo committed Nov 25, 2014
1 parent 8942c8c commit c35d6c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
17 changes: 13 additions & 4 deletions graceful/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,19 @@ func WrapConn(c net.Conn) net.Conn {
return nil
}

wg.Add(1)
return &conn{
Conn: c,
id: atomic.AddUint64(&idleSet.id, 1),
// Avoid race with termination code.
wgLock.Lock()
defer wgLock.Unlock()

// Determine whether the app is shutting down.
if acceptingRequests {
wg.Add(1)
return &conn{
Conn: c,
id: atomic.AddUint64(&idleSet.id, 1),
}
} else {
return nil
}
}

Expand Down
9 changes: 9 additions & 0 deletions graceful/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ var kill = make(chan struct{})
// closed once all the posthooks have been called.
var wait = make(chan struct{})

// Whether new requests should be accepted. When false, new requests are refused.
var acceptingRequests bool = true

// This is the WaitGroup that indicates when all the connections have gracefully
// shut down.
var wg sync.WaitGroup
var wgLock sync.Mutex

// This lock protects the list of pre- and post- hooks below.
var hookLock sync.Mutex
Expand Down Expand Up @@ -91,6 +95,11 @@ func PostHook(f func()) {
func waitForSignal() {
<-sigchan

// Prevent servicing of any new requests.
wgLock.Lock()
acceptingRequests = false
wgLock.Unlock()

hookLock.Lock()
defer hookLock.Unlock()

Expand Down

0 comments on commit c35d6c0

Please sign in to comment.