localshred/protobuf — pull request #144 by liveh2o (2014-02-09 03:51:26+00:00)
Title: Rework the server API

Ahead of adding a middleware stack to connect the server and dispatcher, it made sense to rework the server API. As the various servers were added, shared functionality was extracted into the `Protobuf::Rpc::Server` module. In the process the server API became a little tangled. Instance variables were being initialized in the class and accessed by the module (`@request_data`, `@response`), the module was reliant on the class defining methods (`send_data`) that it then called, etc. This made the [first cut](https://github.com/localshred/protobuf/pull/137) at adding a middleware stack a bit hard to digest.

The idea here was to untangle the servers from the server module and create a clear API for the servers to mix-in and use. To that end, here's an overview of the changes made:

* Serialization (both decoding and encoding) is handled entirely by `handle_request`
* Servers now pull raw request data off the wire and pass it to a single method: `handle_request`
* Servers now receive encoded responses from `handle_request` that are ready to buffer and/or stream
* Pausing GC is now done by calling `handle_request` and sending the response in a block passed to `pause_gc`
* `@request` and `@response` instance variables have been removed in favor of passing variables to the decode/encode methods which handle initialization of the request/response wrappers respectively
* Logging and stat tracking are entirely handled inside the `handle_request` method

I think this is a step in the right direction and leaves a clear path for adding a middleware stack. Because the changes were concentrated into a few files, the diffs may be a bit hard to digest. Hopefully walking through the commits or simply looking at the actual new files will help. Let me know what you think.

// @localshred @abrandoned