Shopify/dashing — pull request #383 by dylanahsmith (2014-06-19 06:41:11+00:00)
Title: Close event connections when the server is gracefully shutting down.

@airhorns for review

## Problem

When the TERM signal is sent to TERM, then it waits for existing connections to finish before exiting (https://github.com/macournoyer/thin/blob/v1.6.2/lib/thin/server.rb#L175). There is a 30 second timeout for most requests, but the [/events endpoint](https://github.com/Shopify/dashing/blob/v1.3.4/lib/dashing/app.rb#L67-75) uses `stream :keep_open do |out|` to keep the connection open for server sent events. As a result, a graceful shutdown can take over an hour to complete.

## Solution

I couldn't find a simple hook in thin to close connections when the server is shutting down, but I ended up wrapping the Thin::Server#stop method to close the event connections.

I tested manually by sending the TERM signal to the thin processes in development after visiting a page which connected to the /events endpoint. Previously this would get stuck waiting until I close the page, but now it completes quickly.