You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Matthieu Imbert <ma...@ens-lyon.fr> on 2009/07/07 17:08:17 UTC
connexion timeouts with twisted thrift
Hi,
I'm willing to implement timeouts to connexion using python twisted
thrift. I mean: if a connexion is inactive for some amount of time,
close it, and clean/release associated objects.
There is already support for that in twisted, so it's quite simple, it
looks like:
# subclass ThriftServerProtocol to override connectionLost method
class MyTxThriftServerProtocol(TTwisted.ThriftServerProtocol):
def connectionLost(self, reason):
# clean pending sessions on this connexion
self.factory.processor._handler.do_something_to_clean()
# subclass ThriftServerFactory to use our protocol instead
# of the default one
class MyTxThriftServerFactory(TTwisted.ThriftServerFactory):
protocol = MyTxThriftServerProtocol
# instanciate my factory
my_factory = MyTxThriftServerFactory(
processor = MyService.Processor(MyTxThriftHandler()),
iprot_factory=TBinaryProtocol.TBinaryProtocolFactory())
# wrap it inside a TimeoutFactory -> it will automatically
# disconnect if connexion idle for more than 100 seconds
my_factory =
twisted.protocols.policies.TimeoutFactory(
my_factory, 100)
# listen
reactor.listenTCP(9090, thrift_server_factory)
The only issue here is that whereas the do_something_to_clean() method
of my handler class is correctly called when a connexion is closed, the
handler instance is shared between all connexions, so i don't know how
to know what has to be cleaned.
Is there a way to get the Protocol instance when a method of a thrift
service handler is called?
cheers,
--
Matthieu
Re: connexion timeouts with twisted thrift
Posted by Esteve Fernandez <es...@sindominio.net>.
Hi
> I'm willing to implement timeouts to connexion using python twisted
> thrift. I mean: if a connexion is inactive for some amount of time,
> close it, and clean/release associated objects.
>
> There is already support for that in twisted, so it's quite simple, it
> looks like:
>
> # subclass ThriftServerProtocol to override connectionLost method
> class MyTxThriftServerProtocol(TTwisted.ThriftServerProtocol):
> def connectionLost(self, reason):
> # clean pending sessions on this connexion
> self.factory.processor._handler.do_something_to_clean()
I wouldn't use the handler for this stuff, though. IMHO it should belong in
the factory, and make the handler protocol-independent. For example:
class MyTxThriftServerProtocol(TTwisted.ThriftServerProtocol):
def connectionLost(self, reason):
# clean pending sessions on this connexion
self.factory.do_something_to_clean(self)
> Is there a way to get the Protocol instance when a method of a thrift
> service handler is called?
If you still want to use your handler for cleaning up pending connections, you
can do this:
# subclass ThriftServerProtocol to override connectionLost method
class MyTxThriftServerProtocol(TTwisted.ThriftServerProtocol):
def connectionLost(self, reason):
# clean pending sessions on this connexion
self.factory.processor._handler.do_something_to_clean(self)
and change do_something_to_clean take an additional argument (a protocol
instance in this case). The problem is that you cannot declare
do_something_to_clean in the Thrift service definition, since there's no
Thrift representation of what's a protocol (and shouldn't). That's why I think
moving all this stuff to the factory would make your code cleaner, separating
concerns (handler, protocol, transport, etc.)
Cheers.