thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthieu Imbert <matthieu.imb...@ens-lyon.fr>
Subject connexion timeouts with twisted thrift
Date Tue, 07 Jul 2009 15:08:17 GMT
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

Mime
View raw message