Question

When using a Twisted ReconnectingClientFactory and the connection is lost do I need to call connector.connect() from within the clientConnectionLost method or does that happen automagically?

The answer might seem obvious because it is, after all, the ReconnectingClientFactory but the Twisted documentation says something here that has me wondering:

"It may be useful to call connector.connect() - this will reconnect."

The wording and use of the term 'may be useful' leads to the question since the api doc for the basic client factory says the same thing.

Max's answer is correct but after further research I think the 'corrector' answer is as below:

def clientConnectionLost(self, connector, reason):
    # do stuff here that is unique to your own requirements, then:
    ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

This allows you to do specialised things required by your application and then call into the factory code to allow Twisted to take care of calling retry() for you.

Was it helpful?

Solution

My old answer was not fully correct. Instead do this:

def clientConnectionLost(self, connector, reason):
    # do stuff here that is unique to your own requirements, then:
    ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

This allows you to do specialised things required by your application and then call into the factory code to allow Twisted to take care of calling retry() for you.

OTHER TIPS

Calling ReconnectingClientFactory.clientConnectionLost(self, connector, reason) is the right thing to do, as it:

  1. Checks 'self.continueTrying' before calling self.retry (which is key, as the connection may have been lost due to a call to 'stopTrying()'
  2. Sets self.connector to the connector passed in.
  3. Calls self.retry() (which due to the lack of a passed-in connector uses self.connector set in #2).
  4. And if there are changes to the ReconnectingClientFactory implementation in the future which would require more actions in the reconnection path, they would be handled automatically without code changes.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top