How to identify a lost client connection on a server that inherits from pb.Root?

For example, I have a client that connects to a server with the following:

class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory): def __init__(self): pb.PBClientFactory.__init__(self) self.ipaddress = None def clientConnectionMade(self, broker): log.msg('Started to connect.') pb.PBClientFactory.clientConnectionMade(self, broker) def buildProtocol(self, addr): log.msg('Connected to %s' % addr) return pb.PBClientFactory.buildProtocol(self, addr) def clientConnectionLost(self, connector, reason): log.msg('Lost connection. Reason:', reason) ReconnectingClientFactory.clientConnectionLost(self, connector, reason) def clientConnectionFailed(self, connector, reason): log.msg('Connection failed. Reason:', reason) ReconnectingClientFactory.clientConnectionLost(self, connector, reason) 

Thus, the client can automatically detect when the connection is lost.

How can I get the same behavior from the server if the client goes down, for example, a failure?

Currently, I will catch a DeadReferenceError (by repeating a list of potentially related clients), but this is a non-event path and, frankly, it's too late.

Any ideas are welcome.

Thanks in advance.

Ben

+4
source share
1 answer

You can register a callback that will be called when the connection is lost. There are two APIs for this, one is Broker.notifyOnDisconnect , the other is RemoteReference.notifyOnDisconnect . They do the same, but one or the other may be more convenient to access depending on the details of your application.

I'm not sure that you can use this for a guarantee that you will never get a DeadReferenceError (for example, I'm not sure what will happen if the remote_foo method, if yours, you return the Deferral, the connection is lost and then the Deferral is triggered), but you you can at least drastically reduce the probability in the general case (i.e. you can always avoid getting a DeadReferenceError from callRemote if you never use callRemote after notifyOnDisconnect your function).

+3
source

Source: https://habr.com/ru/post/1300064/


All Articles