A sketch for a cromlech.closer.

The need

We manage connection through a context manager. On exit it normally close the connexion.

However you may want to stream a response which require the connexion to stay open.

the solution

The connection already have an optional close callback. In this callback you do not close the connection but you ask for say ICloser(connection).close()

This adapter may close the connection immediately if it's not already registered on the Closer, else it knows the closer will do the job.

The Closer is a middleware which may as lists of objects (or callbacks) to close at the begining of start_response, or at the end of it, or on call to response.close() (end of streaming).

Somewhere in the code we can signal we want the connection to close later with something like ICloser(connection).defer(ON_CLOSE).