简体   繁体   中英

Python & Autobahn with Twisted: reset maxRetries within ReconnectingClientFactory

I'm trying to work on reconnecting a client if for some reason the connection is 'broken' using Python & Autobahn with Twisted.

There is a nice example here using ReconnectingClientFactory settings but my question is around the maxRetries.

The way it is setup 5 are allowed in total during the script execution.

class EchoClientFactory(ReconnectingClientFactory, WebSocketClientFactory):
    protocol = EchoClientProtocol
    # http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.ReconnectingClientFactory.html
    #
    maxDelay = 10
    maxRetries = 5
    def startedConnecting(self, connector):
        print('Started to connect.')
    def clientConnectionLost(self, connector, reason):
        print('Lost connection. Reason: {}'.format(reason))
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
    def clientConnectionFailed(self, connector, reason):
        print('Connection failed. Reason: {}'.format(reason))
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)

If for example there is a problem and the client connects after 2 retries then there are only 3 left and the number is not 'reset' after a successful connection.

How can this be achieved, meaning reset the maxRetries back to 5 after a successful connection - if possible?

Thanks!

Kostas

Arrange for your protocol to call ReconnectingClientFactory.resetDelay once it has connected successfully. This resets everything so the backoff logic starts fresh.

It's required that the application call this method instead of the state being reset automatically because even if the TCP connection succeeds you may still have connection problems that merit a retry-with-backoff. For example, perhaps the server responds to your authentication attempt with a "too busy, try again later" message. If these kinds of things can happen in your protocol, you should put your resetDelay call after chance of them happening has passed. ReconnectingClientFactory can't know when this is by itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM