[Twisted-Python] stop/start client connections with loseConnection in ReconnectingClientFactory

Chris Withers chris at withers.org
Mon Apr 1 15:15:33 MDT 2019


On 26/03/2019 09:53, Tobias Oberstein wrote:
>>> The Autobahn guys still show ReconnectingClientFactory in their docs, 
> 
> Where did you find that? That would be a doc bug, but in the _docs_, 
> there is no reference to ReconnectingClientFactory
> 
> (cpy372_3) oberstet at intel-nuci7:~$ find 
> ~/scm/crossbario/autobahn-python/docs/ -type f -exec grep -Hi 
> "ReconnectingClientFactory" {} \;
> (cpy372_3) oberstet at intel-nuci7:~$
> 
> We do have some example code using ReconnectingClientFactory though:
> 
> (cpy372_3) oberstet at intel-nuci7:~$ find 
> ~/scm/crossbario/autobahn-python/examples/ -type f -exec grep -Hi 
> "ReconnectingClientFactory" {} \; | wc -l
> 8

Yeah, this one: 
https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/reconnecting

Would be good to get that converted...

>>  1. ReconnectingClientFactory is destined for deprecation, eventually.
>>       You should just adopt the "new" thing now so that if we get more
>>     energy to cycle the APIs and delete the old stuff, you'll have less
>>     hassle to deal with.  ("New" is in quotes here since it's been
>>     around for well over 3 years at this point; Autobahn should update
>>     too, not just you.)
> 
> Autobahn will automatically use
> 
> twisted.application.internet.ClientService
> 
> for auto-reconnect when on Twisted 16.1.0+
> 
> https://github.com/crossbario/autobahn-python/blob/master/autobahn/twisted/wamp.py#L349 

I'm not using WAMP, just raw websocket.

>>  2. ClientService works with endpoints, which means you can use it with
>>     /any/ kind of transport, like SSH transports, subprocesses, etc.
>>       Most practically, it works with HostnameEndpoint which is a much
>>     better way to get TLS than connectSSL; 

What's the simplest way to connect a ClientService to a websocket url 
(ie: ws://host/endpoint or wss://host/endpoint)

>>      1. if you want to shut down a ReconnectingClientFactory:
>>          1. you have to call stopTrying, then uh...
>>          2. find the last protocol it built with buildProtocol, then

This hasn't proved to hard.

>>          3. grab its transport (hope it's saving that transport as
>>             '.transport', because it doesn't actually have to)
>>          4. call loseConnection

Why? Sending a WebSocket protocol-level close from the client seems to 
work just fine?

>>  5. It's more testable because it just takes its clock and reactor as
>>     constructor parameters, rather than requiring post-hoc
>>     poorly-documented attribute patching to become testable.

I've abstracted all that away into https://github.com/cjw296/carly.
See 
https://github.com/cjw296/carly/blob/master/tests/test_autobahn_websocket_client.py 
for example.
I do still have writing docs for carly on my list of things to do.

It would be great if Twisted ever had a real testing reactor, but I 
think we all know that's never going to happen ;-)

Besides, it's actually useful to test with real networking...

Chris



More information about the Twisted-Python mailing list