Closed gdude2002 closed 8 years ago
In my opinion, I think we should do something like..
FactoryManager
instanciates the Factory
as before and calls setup()
.Factory
is a protocol-specific class derived from a base class that does the reconnection things in a Twisted way.
buildProtocol
would indeed build a protocolconnect()
function as well, so we can generify without losing flexibilityreactor.connect<proto>()
.setup()
does all of the connection-specific stuff, but doesn't instanciate the protocol and all it should do is track itsetup()
should call connect()
to get things started.protocol
attribute as-is, but I don't think that should be a problem.What would setup()
do exactly? Isn't everything covered by connect()
and buildProtocol()
?
Ah, hm, yeah, I suppose that's all one would need.. I can't think of anything I'd put in setup() that would be bad to run more than once.
Why would we run more than once? Isn't the point that connect/setup()
gets called once to start it, and buildProtocol()
gets called on each connection? What needs done other than building the protocol? Reloading it, but that's currently done at protocol build time anyway.
It would be run again if the connection fails or is lost, of course. Not all factories would be calling the same reactor.connect<proto>(...)
function with the same args, and we need some generic way of doing this if we want to have reconnect management in the base protocol.
When would connect()
be called other than to begin with though? ClientFactory
already has a way to deal with this. Considering ReconnectingClientFactory
. You only call reactor.connect...
on it once, then it uses the connector
to schedule a reconnect when clientConnectionLost/Failed
is called.
OK, sure, but then the protocol instance gets reused - do we want that?
What? It calls buildProtocol()
...
Oh, it does? I didn't know that. Alright then, then... yeah. That.
Time for another draft, I guess.
FactoryManager
instanciates the Factory
as before and calls connect()
.Factory
is a protocol-specific class derived from a base class that does the reconnection things in a Twisted way.
buildProtocol()
would indeed build a protocol, and keep track of the latest protocol instanceconnect()
function, so we can generify without losing flexibilityreactor.connect<proto>()
connector.connect()
instead of rebuilding everything by calling self.connect()
protocol
attribute as-is, but I don't think that should be a problem.What's the point of setup()
? Isn't that the point of __init__()
?
Better reconnect logic (re. timing) would be a good idea.
If we need to keep track of the Protocol instance anyway, no point breaking the API by renaming protocol
attribute.
Forgot to rename to connect()
. :P
Progress on this is progressing nicely. Couldn't think of a better way to put that.
@rakiru I think you're good to review the commits so far.
We're done - #91
Here's the current path we take when we connect a protocol.
FactoryManager
instanciatesFactory
with the name of the protocol, its config and the factory manager instance.FactoryManager
callsFactory.setup()
to begin the process.Factory
loads up the corresponding protocol module if it hasn't been loaded already - or reloads it if it has been.Factory
then instanciates the protocol with its name, config, and the instance of theFactory
.__init__
, callsreactor.connect<proto>(...)
, where<proto>
is the connection protocol, passing in the instance of the factory.Factory.buildProtocol(addr)
, which returns the current instance of the protocol that we've already created.Twisted, however, expects something like this:
Factory
is instanciated.reactor.connect<proto>(...)
is called, passing in the instance of the factory.Factory.buildProtocol(addr)
, which creates an instance of the protocol and returns it, allowing Twisted to connect with it.What would be our ideal path? Let's fix that and make it happen! As a side effect, this ticket will result in protocols having their own factories, which is a requirement for all kinds of things.