I am implementing a small thrift (0.6.0) server in ruby to play a role of proxy to another protocol with several connections (multiple clients) to a single server. I want to be able and keep per-client data on the server side and track “session” parameters across multiple invocations of handler functions.
I currently use Thrift::NonblockingServer as SimpleServer does not seem to allow concurrent connections.
I know how to use TCPSocket::peeraddr but Thrift::NonblockingServer::IOManager::Worker::run creates a temporary MemoryBufferTransport with the frame it read and passes that as the input/output protocol down to the processor so it seems that the info is not passed down from there.
Is there a clean way to do this?
I was thinking of re-defining the above mentioned Thrift::NonblockingServer::IOManager::Worker::run to also include the fd, or other ID at an additional parameter to process or augment the proto instances but as I also have to worry about one layer of generated ruby code (process_* methods in class Processor) it seems a little heavy.
I wonder if someone did anything like this before.
Thanks!
p.s. this is similar problem to this C++ thrift question
Here is how I went about changing
Thrift::NonblockingServer::IOManager::Worker::runto support this.Few caveats:
$connectionsbe@@connectionsinConnEntryor a diff class?).Thread.currenthash for thread-local storage has a namespace pollution issueFirst, at some central module:
Then at any point in the handler you can access
Thread.current[:connEntry].addr_infofor connection specific data or store anything regarding the connection in theThread.current[:connEntry].attrhash.