internet.py 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. # -*- test-case-name: twisted.application.test.test_internet,twisted.test.test_application,twisted.test.test_cooperator -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Reactor-based Services
  6. Here are services to run clients, servers and periodic services using
  7. the reactor.
  8. If you want to run a server service, L{StreamServerEndpointService} defines a
  9. service that can wrap an arbitrary L{IStreamServerEndpoint
  10. <twisted.internet.interfaces.IStreamServerEndpoint>}
  11. as an L{IService}. See also L{twisted.application.strports.service} for
  12. constructing one of these directly from a descriptive string.
  13. Additionally, this module (dynamically) defines various Service subclasses that
  14. let you represent clients and servers in a Service hierarchy. Endpoints APIs
  15. should be preferred for stream server services, but since those APIs do not yet
  16. exist for clients or datagram services, many of these are still useful.
  17. They are as follows::
  18. TCPServer, TCPClient,
  19. UNIXServer, UNIXClient,
  20. SSLServer, SSLClient,
  21. UDPServer,
  22. UNIXDatagramServer, UNIXDatagramClient,
  23. MulticastServer
  24. These classes take arbitrary arguments in their constructors and pass
  25. them straight on to their respective reactor.listenXXX or
  26. reactor.connectXXX calls.
  27. For example, the following service starts a web server on port 8080:
  28. C{TCPServer(8080, server.Site(r))}. See the documentation for the
  29. reactor.listen/connect* methods for more information.
  30. """
  31. from __future__ import absolute_import, division
  32. from random import random as _goodEnoughRandom
  33. from twisted.python import log
  34. from twisted.logger import Logger
  35. from twisted.application import service
  36. from twisted.internet import task
  37. from twisted.python.failure import Failure
  38. from twisted.internet.defer import (
  39. CancelledError, Deferred, succeed, fail
  40. )
  41. from automat import MethodicalMachine
  42. def _maybeGlobalReactor(maybeReactor):
  43. """
  44. @return: the argument, or the global reactor if the argument is L{None}.
  45. """
  46. if maybeReactor is None:
  47. from twisted.internet import reactor
  48. return reactor
  49. else:
  50. return maybeReactor
  51. class _VolatileDataService(service.Service):
  52. volatile = []
  53. def __getstate__(self):
  54. d = service.Service.__getstate__(self)
  55. for attr in self.volatile:
  56. if attr in d:
  57. del d[attr]
  58. return d
  59. class _AbstractServer(_VolatileDataService):
  60. """
  61. @cvar volatile: list of attribute to remove from pickling.
  62. @type volatile: C{list}
  63. @ivar method: the type of method to call on the reactor, one of B{TCP},
  64. B{UDP}, B{SSL} or B{UNIX}.
  65. @type method: C{str}
  66. @ivar reactor: the current running reactor.
  67. @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
  68. C{IReactorSSL} or C{IReactorUnix}.
  69. @ivar _port: instance of port set when the service is started.
  70. @type _port: a provider of L{twisted.internet.interfaces.IListeningPort}.
  71. """
  72. volatile = ['_port']
  73. method = None
  74. reactor = None
  75. _port = None
  76. def __init__(self, *args, **kwargs):
  77. self.args = args
  78. if 'reactor' in kwargs:
  79. self.reactor = kwargs.pop("reactor")
  80. self.kwargs = kwargs
  81. def privilegedStartService(self):
  82. service.Service.privilegedStartService(self)
  83. self._port = self._getPort()
  84. def startService(self):
  85. service.Service.startService(self)
  86. if self._port is None:
  87. self._port = self._getPort()
  88. def stopService(self):
  89. service.Service.stopService(self)
  90. # TODO: if startup failed, should shutdown skip stopListening?
  91. # _port won't exist
  92. if self._port is not None:
  93. d = self._port.stopListening()
  94. del self._port
  95. return d
  96. def _getPort(self):
  97. """
  98. Wrapper around the appropriate listen method of the reactor.
  99. @return: the port object returned by the listen method.
  100. @rtype: an object providing
  101. L{twisted.internet.interfaces.IListeningPort}.
  102. """
  103. return getattr(_maybeGlobalReactor(self.reactor),
  104. 'listen%s' % (self.method,))(*self.args, **self.kwargs)
  105. class _AbstractClient(_VolatileDataService):
  106. """
  107. @cvar volatile: list of attribute to remove from pickling.
  108. @type volatile: C{list}
  109. @ivar method: the type of method to call on the reactor, one of B{TCP},
  110. B{UDP}, B{SSL} or B{UNIX}.
  111. @type method: C{str}
  112. @ivar reactor: the current running reactor.
  113. @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
  114. C{IReactorSSL} or C{IReactorUnix}.
  115. @ivar _connection: instance of connection set when the service is started.
  116. @type _connection: a provider of L{twisted.internet.interfaces.IConnector}.
  117. """
  118. volatile = ['_connection']
  119. method = None
  120. reactor = None
  121. _connection = None
  122. def __init__(self, *args, **kwargs):
  123. self.args = args
  124. if 'reactor' in kwargs:
  125. self.reactor = kwargs.pop("reactor")
  126. self.kwargs = kwargs
  127. def startService(self):
  128. service.Service.startService(self)
  129. self._connection = self._getConnection()
  130. def stopService(self):
  131. service.Service.stopService(self)
  132. if self._connection is not None:
  133. self._connection.disconnect()
  134. del self._connection
  135. def _getConnection(self):
  136. """
  137. Wrapper around the appropriate connect method of the reactor.
  138. @return: the port object returned by the connect method.
  139. @rtype: an object providing L{twisted.internet.interfaces.IConnector}.
  140. """
  141. return getattr(_maybeGlobalReactor(self.reactor),
  142. 'connect%s' % (self.method,))(*self.args, **self.kwargs)
  143. _doc={
  144. 'Client':
  145. """Connect to %(tran)s
  146. Call reactor.connect%(tran)s when the service starts, with the
  147. arguments given to the constructor.
  148. """,
  149. 'Server':
  150. """Serve %(tran)s clients
  151. Call reactor.listen%(tran)s when the service starts, with the
  152. arguments given to the constructor. When the service stops,
  153. stop listening. See twisted.internet.interfaces for documentation
  154. on arguments to the reactor method.
  155. """,
  156. }
  157. for tran in 'TCP UNIX SSL UDP UNIXDatagram Multicast'.split():
  158. for side in 'Server Client'.split():
  159. if tran == "Multicast" and side == "Client":
  160. continue
  161. if tran == "UDP" and side == "Client":
  162. continue
  163. base = globals()['_Abstract'+side]
  164. doc = _doc[side] % vars()
  165. klass = type(tran+side, (base,), {'method': tran, '__doc__': doc})
  166. globals()[tran+side] = klass
  167. class TimerService(_VolatileDataService):
  168. """
  169. Service to periodically call a function
  170. Every C{step} seconds call the given function with the given arguments.
  171. The service starts the calls when it starts, and cancels them
  172. when it stops.
  173. @ivar clock: Source of time. This defaults to L{None} which is
  174. causes L{twisted.internet.reactor} to be used.
  175. Feel free to set this to something else, but it probably ought to be
  176. set *before* calling L{startService}.
  177. @type clock: L{IReactorTime<twisted.internet.interfaces.IReactorTime>}
  178. @ivar call: Function and arguments to call periodically.
  179. @type call: L{tuple} of C{(callable, args, kwargs)}
  180. """
  181. volatile = ['_loop', '_loopFinished']
  182. def __init__(self, step, callable, *args, **kwargs):
  183. """
  184. @param step: The number of seconds between calls.
  185. @type step: L{float}
  186. @param callable: Function to call
  187. @type callable: L{callable}
  188. @param args: Positional arguments to pass to function
  189. @param kwargs: Keyword arguments to pass to function
  190. """
  191. self.step = step
  192. self.call = (callable, args, kwargs)
  193. self.clock = None
  194. def startService(self):
  195. service.Service.startService(self)
  196. callable, args, kwargs = self.call
  197. # we have to make a new LoopingCall each time we're started, because
  198. # an active LoopingCall remains active when serialized. If
  199. # LoopingCall were a _VolatileDataService, we wouldn't need to do
  200. # this.
  201. self._loop = task.LoopingCall(callable, *args, **kwargs)
  202. self._loop.clock = _maybeGlobalReactor(self.clock)
  203. self._loopFinished = self._loop.start(self.step, now=True)
  204. self._loopFinished.addErrback(self._failed)
  205. def _failed(self, why):
  206. # make a note that the LoopingCall is no longer looping, so we don't
  207. # try to shut it down a second time in stopService. I think this
  208. # should be in LoopingCall. -warner
  209. self._loop.running = False
  210. log.err(why)
  211. def stopService(self):
  212. """
  213. Stop the service.
  214. @rtype: L{Deferred<defer.Deferred>}
  215. @return: a L{Deferred<defer.Deferred>} which is fired when the
  216. currently running call (if any) is finished.
  217. """
  218. if self._loop.running:
  219. self._loop.stop()
  220. self._loopFinished.addCallback(lambda _:
  221. service.Service.stopService(self))
  222. return self._loopFinished
  223. class CooperatorService(service.Service):
  224. """
  225. Simple L{service.IService} which starts and stops a L{twisted.internet.task.Cooperator}.
  226. """
  227. def __init__(self):
  228. self.coop = task.Cooperator(started=False)
  229. def coiterate(self, iterator):
  230. return self.coop.coiterate(iterator)
  231. def startService(self):
  232. self.coop.start()
  233. def stopService(self):
  234. self.coop.stop()
  235. class StreamServerEndpointService(service.Service, object):
  236. """
  237. A L{StreamServerEndpointService} is an L{IService} which runs a server on a
  238. listening port described by an L{IStreamServerEndpoint
  239. <twisted.internet.interfaces.IStreamServerEndpoint>}.
  240. @ivar factory: A server factory which will be used to listen on the
  241. endpoint.
  242. @ivar endpoint: An L{IStreamServerEndpoint
  243. <twisted.internet.interfaces.IStreamServerEndpoint>} provider
  244. which will be used to listen when the service starts.
  245. @ivar _waitingForPort: a Deferred, if C{listen} has yet been invoked on the
  246. endpoint, otherwise None.
  247. @ivar _raiseSynchronously: Defines error-handling behavior for the case
  248. where C{listen(...)} raises an exception before C{startService} or
  249. C{privilegedStartService} have completed.
  250. @type _raiseSynchronously: C{bool}
  251. @since: 10.2
  252. """
  253. _raiseSynchronously = False
  254. def __init__(self, endpoint, factory):
  255. self.endpoint = endpoint
  256. self.factory = factory
  257. self._waitingForPort = None
  258. def privilegedStartService(self):
  259. """
  260. Start listening on the endpoint.
  261. """
  262. service.Service.privilegedStartService(self)
  263. self._waitingForPort = self.endpoint.listen(self.factory)
  264. raisedNow = []
  265. def handleIt(err):
  266. if self._raiseSynchronously:
  267. raisedNow.append(err)
  268. elif not err.check(CancelledError):
  269. log.err(err)
  270. self._waitingForPort.addErrback(handleIt)
  271. if raisedNow:
  272. raisedNow[0].raiseException()
  273. self._raiseSynchronously = False
  274. def startService(self):
  275. """
  276. Start listening on the endpoint, unless L{privilegedStartService} got
  277. around to it already.
  278. """
  279. service.Service.startService(self)
  280. if self._waitingForPort is None:
  281. self.privilegedStartService()
  282. def stopService(self):
  283. """
  284. Stop listening on the port if it is already listening, otherwise,
  285. cancel the attempt to listen.
  286. @return: a L{Deferred<twisted.internet.defer.Deferred>} which fires
  287. with L{None} when the port has stopped listening.
  288. """
  289. self._waitingForPort.cancel()
  290. def stopIt(port):
  291. if port is not None:
  292. return port.stopListening()
  293. d = self._waitingForPort.addCallback(stopIt)
  294. def stop(passthrough):
  295. self.running = False
  296. return passthrough
  297. d.addBoth(stop)
  298. return d
  299. class _ReconnectingProtocolProxy(object):
  300. """
  301. A proxy for a Protocol to provide connectionLost notification to a client
  302. connection service, in support of reconnecting when connections are lost.
  303. """
  304. def __init__(self, protocol, lostNotification):
  305. """
  306. Create a L{_ReconnectingProtocolProxy}.
  307. @param protocol: the application-provided L{interfaces.IProtocol}
  308. provider.
  309. @type protocol: provider of L{interfaces.IProtocol} which may
  310. additionally provide L{interfaces.IHalfCloseableProtocol} and
  311. L{interfaces.IFileDescriptorReceiver}.
  312. @param lostNotification: a 1-argument callable to invoke with the
  313. C{reason} when the connection is lost.
  314. """
  315. self._protocol = protocol
  316. self._lostNotification = lostNotification
  317. def connectionLost(self, reason):
  318. """
  319. The connection was lost. Relay this information.
  320. @param reason: The reason the connection was lost.
  321. @return: the underlying protocol's result
  322. """
  323. try:
  324. return self._protocol.connectionLost(reason)
  325. finally:
  326. self._lostNotification(reason)
  327. def __getattr__(self, item):
  328. return getattr(self._protocol, item)
  329. def __repr__(self):
  330. return '<%s wrapping %r>' % (
  331. self.__class__.__name__, self._protocol)
  332. class _DisconnectFactory(object):
  333. """
  334. A L{_DisconnectFactory} is a proxy for L{IProtocolFactory} that catches
  335. C{connectionLost} notifications and relays them.
  336. """
  337. def __init__(self, protocolFactory, protocolDisconnected):
  338. self._protocolFactory = protocolFactory
  339. self._protocolDisconnected = protocolDisconnected
  340. def buildProtocol(self, addr):
  341. """
  342. Create a L{_ReconnectingProtocolProxy} with the disconnect-notification
  343. callback we were called with.
  344. @param addr: The address the connection is coming from.
  345. @return: a L{_ReconnectingProtocolProxy} for a protocol produced by
  346. C{self._protocolFactory}
  347. """
  348. return _ReconnectingProtocolProxy(
  349. self._protocolFactory.buildProtocol(addr),
  350. self._protocolDisconnected
  351. )
  352. def __getattr__(self, item):
  353. return getattr(self._protocolFactory, item)
  354. def __repr__(self):
  355. return '<%s wrapping %r>' % (
  356. self.__class__.__name__, self._protocolFactory)
  357. def backoffPolicy(initialDelay=1.0, maxDelay=60.0, factor=1.5,
  358. jitter=_goodEnoughRandom):
  359. """
  360. A timeout policy for L{ClientService} which computes an exponential backoff
  361. interval with configurable parameters.
  362. @since: 16.1.0
  363. @param initialDelay: Delay for the first reconnection attempt (default
  364. 1.0s).
  365. @type initialDelay: L{float}
  366. @param maxDelay: Maximum number of seconds between connection attempts
  367. (default 60 seconds, or one minute). Note that this value is before
  368. jitter is applied, so the actual maximum possible delay is this value
  369. plus the maximum possible result of C{jitter()}.
  370. @type maxDelay: L{float}
  371. @param factor: A multiplicative factor by which the delay grows on each
  372. failed reattempt. Default: 1.5.
  373. @type factor: L{float}
  374. @param jitter: A 0-argument callable that introduces noise into the delay.
  375. By default, C{random.random}, i.e. a pseudorandom floating-point value
  376. between zero and one.
  377. @type jitter: 0-argument callable returning L{float}
  378. @return: a 1-argument callable that, given an attempt count, returns a
  379. floating point number; the number of seconds to delay.
  380. @rtype: see L{ClientService.__init__}'s C{retryPolicy} argument.
  381. """
  382. def policy(attempt):
  383. return min(initialDelay * (factor ** attempt), maxDelay) + jitter()
  384. return policy
  385. _defaultPolicy = backoffPolicy()
  386. def _firstResult(gen):
  387. """
  388. Return the first element of a generator and exhaust it.
  389. C{MethodicalMachine.upon}'s C{collector} argument takes a generator of
  390. output results. If the generator is exhausted, the later outputs aren't
  391. actually run.
  392. @param gen: Generator to extract values from
  393. @return: The first element of the generator.
  394. """
  395. return list(gen)[0]
  396. class _ClientMachine(object):
  397. """
  398. State machine for maintaining a single outgoing connection to an endpoint.
  399. @see: L{ClientService}
  400. """
  401. _machine = MethodicalMachine()
  402. def __init__(self, endpoint, factory, retryPolicy, clock, log):
  403. """
  404. @see: L{ClientService.__init__}
  405. @param log: The logger for the L{ClientService} instance this state
  406. machine is associated to.
  407. @type log: L{Logger}
  408. @ivar _awaitingConnected: notifications to make when connection
  409. succeeds, fails, or is cancelled
  410. @type _awaitingConnected: list of (Deferred, count) tuples
  411. """
  412. self._endpoint = endpoint
  413. self._failedAttempts = 0
  414. self._stopped = False
  415. self._factory = factory
  416. self._timeoutForAttempt = retryPolicy
  417. self._clock = clock
  418. self._connectionInProgress = succeed(None)
  419. self._awaitingConnected = []
  420. self._stopWaiters = []
  421. self._log = log
  422. @_machine.state(initial=True)
  423. def _init(self):
  424. """
  425. The service has not been started.
  426. """
  427. @_machine.state()
  428. def _connecting(self):
  429. """
  430. The service has started connecting.
  431. """
  432. @_machine.state()
  433. def _waiting(self):
  434. """
  435. The service is waiting for the reconnection period
  436. before reconnecting.
  437. """
  438. @_machine.state()
  439. def _connected(self):
  440. """
  441. The service is connected.
  442. """
  443. @_machine.state()
  444. def _disconnecting(self):
  445. """
  446. The service is disconnecting after being asked to shutdown.
  447. """
  448. @_machine.state()
  449. def _restarting(self):
  450. """
  451. The service is disconnecting and has been asked to restart.
  452. """
  453. @_machine.state()
  454. def _stopped(self):
  455. """
  456. The service has been stopped and is disconnected.
  457. """
  458. @_machine.input()
  459. def start(self):
  460. """
  461. Start this L{ClientService}, initiating the connection retry loop.
  462. """
  463. @_machine.output()
  464. def _connect(self):
  465. """
  466. Start a connection attempt.
  467. """
  468. factoryProxy = _DisconnectFactory(self._factory,
  469. lambda _: self._clientDisconnected())
  470. self._connectionInProgress = (
  471. self._endpoint.connect(factoryProxy)
  472. .addCallback(self._connectionMade)
  473. .addErrback(self._connectionFailed))
  474. @_machine.output()
  475. def _resetFailedAttempts(self):
  476. """
  477. Reset the number of failed attempts.
  478. """
  479. self._failedAttempts = 0
  480. @_machine.input()
  481. def stop(self):
  482. """
  483. Stop trying to connect and disconnect any current connection.
  484. @return: a L{Deferred} that fires when all outstanding connections are
  485. closed and all in-progress connection attempts halted.
  486. """
  487. @_machine.output()
  488. def _waitForStop(self):
  489. """
  490. Return a deferred that will fire when the service has finished
  491. disconnecting.
  492. @return: L{Deferred} that fires when the service has finished
  493. disconnecting.
  494. """
  495. self._stopWaiters.append(Deferred())
  496. return self._stopWaiters[-1]
  497. @_machine.output()
  498. def _stopConnecting(self):
  499. """
  500. Stop pending connection attempt.
  501. """
  502. self._connectionInProgress.cancel()
  503. @_machine.output()
  504. def _stopRetrying(self):
  505. """
  506. Stop pending attempt to reconnect.
  507. """
  508. self._retryCall.cancel()
  509. del self._retryCall
  510. @_machine.output()
  511. def _disconnect(self):
  512. """
  513. Disconnect the current connection.
  514. """
  515. self._currentConnection.transport.loseConnection()
  516. @_machine.input()
  517. def _connectionMade(self, protocol):
  518. """
  519. A connection has been made.
  520. @param protocol: The protocol of the connection.
  521. @type protocol: L{IProtocol}
  522. """
  523. @_machine.output()
  524. def _notifyWaiters(self, protocol):
  525. """
  526. Notify all pending requests for a connection that a connection has been
  527. made.
  528. @param protocol: The protocol of the connection.
  529. @type protocol: L{IProtocol}
  530. """
  531. # This should be in _resetFailedAttempts but the signature doesn't
  532. # match.
  533. self._failedAttempts = 0
  534. self._currentConnection = protocol._protocol
  535. self._unawait(self._currentConnection)
  536. @_machine.input()
  537. def _connectionFailed(self, f):
  538. """
  539. The current connection attempt failed.
  540. """
  541. @_machine.output()
  542. def _wait(self):
  543. """
  544. Schedule a retry attempt.
  545. """
  546. self._doWait()
  547. @_machine.output()
  548. def _ignoreAndWait(self, f):
  549. """
  550. Schedule a retry attempt, and ignore the Failure passed in.
  551. """
  552. return self._doWait()
  553. def _doWait(self):
  554. self._failedAttempts += 1
  555. delay = self._timeoutForAttempt(self._failedAttempts)
  556. self._log.info("Scheduling retry {attempt} to connect {endpoint} "
  557. "in {delay} seconds.", attempt=self._failedAttempts,
  558. endpoint=self._endpoint, delay=delay)
  559. self._retryCall = self._clock.callLater(delay, self._reconnect)
  560. @_machine.input()
  561. def _reconnect(self):
  562. """
  563. The wait between connection attempts is done.
  564. """
  565. @_machine.input()
  566. def _clientDisconnected(self):
  567. """
  568. The current connection has been disconnected.
  569. """
  570. @_machine.output()
  571. def _forgetConnection(self):
  572. """
  573. Forget the current connection.
  574. """
  575. del self._currentConnection
  576. @_machine.output()
  577. def _cancelConnectWaiters(self):
  578. """
  579. Notify all pending requests for a connection that no more connections
  580. are expected.
  581. """
  582. self._unawait(Failure(CancelledError()))
  583. @_machine.output()
  584. def _ignoreAndCancelConnectWaiters(self, f):
  585. """
  586. Notify all pending requests for a connection that no more connections
  587. are expected, after ignoring the Failure passed in.
  588. """
  589. self._unawait(Failure(CancelledError()))
  590. @_machine.output()
  591. def _finishStopping(self):
  592. """
  593. Notify all deferreds waiting on the service stopping.
  594. """
  595. self._doFinishStopping()
  596. @_machine.output()
  597. def _ignoreAndFinishStopping(self, f):
  598. """
  599. Notify all deferreds waiting on the service stopping, and ignore the
  600. Failure passed in.
  601. """
  602. self._doFinishStopping()
  603. def _doFinishStopping(self):
  604. self._stopWaiters, waiting = [], self._stopWaiters
  605. for w in waiting:
  606. w.callback(None)
  607. @_machine.input()
  608. def whenConnected(self, failAfterFailures=None):
  609. """
  610. Retrieve the currently-connected L{Protocol}, or the next one to
  611. connect.
  612. @param failAfterFailures: number of connection failures after which
  613. the Deferred will deliver a Failure (None means the Deferred will
  614. only fail if/when the service is stopped). Set this to 1 to make
  615. the very first connection failure signal an error. Use 2 to
  616. allow one failure but signal an error if the subsequent retry
  617. then fails.
  618. @type failAfterFailures: L{int} or None
  619. @return: a Deferred that fires with a protocol produced by the
  620. factory passed to C{__init__}
  621. @rtype: L{Deferred} that may:
  622. - fire with L{IProtocol}
  623. - fail with L{CancelledError} when the service is stopped
  624. - fail with e.g.
  625. L{DNSLookupError<twisted.internet.error.DNSLookupError>} or
  626. L{ConnectionRefusedError<twisted.internet.error.ConnectionRefusedError>}
  627. when the number of consecutive failed connection attempts
  628. equals the value of "failAfterFailures"
  629. """
  630. @_machine.output()
  631. def _currentConnection(self, failAfterFailures=None):
  632. """
  633. Return the currently connected protocol.
  634. @return: L{Deferred} that is fired with currently connected protocol.
  635. """
  636. return succeed(self._currentConnection)
  637. @_machine.output()
  638. def _noConnection(self, failAfterFailures=None):
  639. """
  640. Notify the caller that no connection is expected.
  641. @return: L{Deferred} that is fired with L{CancelledError}.
  642. """
  643. return fail(CancelledError())
  644. @_machine.output()
  645. def _awaitingConnection(self, failAfterFailures=None):
  646. """
  647. Return a deferred that will fire with the next connected protocol.
  648. @return: L{Deferred} that will fire with the next connected protocol.
  649. """
  650. result = Deferred()
  651. self._awaitingConnected.append((result, failAfterFailures))
  652. return result
  653. @_machine.output()
  654. def _deferredSucceededWithNone(self):
  655. """
  656. Return a deferred that has already fired with L{None}.
  657. @return: A L{Deferred} that has already fired with L{None}.
  658. """
  659. return succeed(None)
  660. def _unawait(self, value):
  661. """
  662. Fire all outstanding L{ClientService.whenConnected} L{Deferred}s.
  663. @param value: the value to fire the L{Deferred}s with.
  664. """
  665. self._awaitingConnected, waiting = [], self._awaitingConnected
  666. for (w, remaining) in waiting:
  667. w.callback(value)
  668. @_machine.output()
  669. def _deliverConnectionFailure(self, f):
  670. """
  671. Deliver connection failures to any L{ClientService.whenConnected}
  672. L{Deferred}s that have met their failAfterFailures threshold.
  673. @param f: the Failure to fire the L{Deferred}s with.
  674. """
  675. ready = []
  676. notReady = []
  677. for (w, remaining) in self._awaitingConnected:
  678. if remaining is None:
  679. notReady.append((w, remaining))
  680. elif remaining <= 1:
  681. ready.append(w)
  682. else:
  683. notReady.append((w, remaining-1))
  684. self._awaitingConnected = notReady
  685. for w in ready:
  686. w.callback(f)
  687. # State Transitions
  688. _init.upon(start, enter=_connecting,
  689. outputs=[_connect])
  690. _init.upon(stop, enter=_stopped,
  691. outputs=[_deferredSucceededWithNone],
  692. collector=_firstResult)
  693. _connecting.upon(start, enter=_connecting, outputs=[])
  694. # Note that this synchonously triggers _connectionFailed in the
  695. # _disconnecting state.
  696. _connecting.upon(stop, enter=_disconnecting,
  697. outputs=[_waitForStop, _stopConnecting],
  698. collector=_firstResult)
  699. _connecting.upon(_connectionMade, enter=_connected,
  700. outputs=[_notifyWaiters])
  701. _connecting.upon(_connectionFailed, enter=_waiting,
  702. outputs=[_ignoreAndWait, _deliverConnectionFailure])
  703. _waiting.upon(start, enter=_waiting,
  704. outputs=[])
  705. _waiting.upon(stop, enter=_stopped,
  706. outputs=[_waitForStop,
  707. _cancelConnectWaiters,
  708. _stopRetrying,
  709. _finishStopping],
  710. collector=_firstResult)
  711. _waiting.upon(_reconnect, enter=_connecting,
  712. outputs=[_connect])
  713. _connected.upon(start, enter=_connected,
  714. outputs=[])
  715. _connected.upon(stop, enter=_disconnecting,
  716. outputs=[_waitForStop, _disconnect],
  717. collector=_firstResult)
  718. _connected.upon(_clientDisconnected, enter=_waiting,
  719. outputs=[_forgetConnection, _wait])
  720. _disconnecting.upon(start, enter=_restarting,
  721. outputs=[_resetFailedAttempts])
  722. _disconnecting.upon(stop, enter=_disconnecting,
  723. outputs=[_waitForStop],
  724. collector=_firstResult)
  725. _disconnecting.upon(_clientDisconnected, enter=_stopped,
  726. outputs=[_cancelConnectWaiters,
  727. _finishStopping,
  728. _forgetConnection])
  729. # Note that this is triggered synchonously with the transition from
  730. # _connecting
  731. _disconnecting.upon(_connectionFailed, enter=_stopped,
  732. outputs=[_ignoreAndCancelConnectWaiters,
  733. _ignoreAndFinishStopping])
  734. _restarting.upon(start, enter=_restarting,
  735. outputs=[])
  736. _restarting.upon(stop, enter=_disconnecting,
  737. outputs=[_waitForStop],
  738. collector=_firstResult)
  739. _restarting.upon(_clientDisconnected, enter=_connecting,
  740. outputs=[_finishStopping, _connect])
  741. _stopped.upon(start, enter=_connecting,
  742. outputs=[_connect])
  743. _stopped.upon(stop, enter=_stopped,
  744. outputs=[_deferredSucceededWithNone],
  745. collector=_firstResult)
  746. _init.upon(whenConnected, enter=_init,
  747. outputs=[_awaitingConnection],
  748. collector=_firstResult)
  749. _connecting.upon(whenConnected, enter=_connecting,
  750. outputs=[_awaitingConnection],
  751. collector=_firstResult)
  752. _waiting.upon(whenConnected, enter=_waiting,
  753. outputs=[_awaitingConnection],
  754. collector=_firstResult)
  755. _connected.upon(whenConnected, enter=_connected,
  756. outputs=[_currentConnection],
  757. collector=_firstResult)
  758. _disconnecting.upon(whenConnected, enter=_disconnecting,
  759. outputs=[_awaitingConnection],
  760. collector=_firstResult)
  761. _restarting.upon(whenConnected, enter=_restarting,
  762. outputs=[_awaitingConnection],
  763. collector=_firstResult)
  764. _stopped.upon(whenConnected, enter=_stopped,
  765. outputs=[_noConnection],
  766. collector=_firstResult)
  767. class ClientService(service.Service, object):
  768. """
  769. A L{ClientService} maintains a single outgoing connection to a client
  770. endpoint, reconnecting after a configurable timeout when a connection
  771. fails, either before or after connecting.
  772. @since: 16.1.0
  773. """
  774. _log = Logger()
  775. def __init__(self, endpoint, factory, retryPolicy=None, clock=None):
  776. """
  777. @param endpoint: A L{stream client endpoint
  778. <interfaces.IStreamClientEndpoint>} provider which will be used to
  779. connect when the service starts.
  780. @param factory: A L{protocol factory <interfaces.IProtocolFactory>}
  781. which will be used to create clients for the endpoint.
  782. @param retryPolicy: A policy configuring how long L{ClientService} will
  783. wait between attempts to connect to C{endpoint}.
  784. @type retryPolicy: callable taking (the number of failed connection
  785. attempts made in a row (L{int})) and returning the number of
  786. seconds to wait before making another attempt.
  787. @param clock: The clock used to schedule reconnection. It's mainly
  788. useful to be parametrized in tests. If the factory is serialized,
  789. this attribute will not be serialized, and the default value (the
  790. reactor) will be restored when deserialized.
  791. @type clock: L{IReactorTime}
  792. """
  793. clock = _maybeGlobalReactor(clock)
  794. retryPolicy = _defaultPolicy if retryPolicy is None else retryPolicy
  795. self._machine = _ClientMachine(
  796. endpoint, factory, retryPolicy, clock,
  797. log=self._log,
  798. )
  799. def whenConnected(self, failAfterFailures=None):
  800. """
  801. Retrieve the currently-connected L{Protocol}, or the next one to
  802. connect.
  803. @param failAfterFailures: number of connection failures after which
  804. the Deferred will deliver a Failure (None means the Deferred will
  805. only fail if/when the service is stopped). Set this to 1 to make
  806. the very first connection failure signal an error. Use 2 to
  807. allow one failure but signal an error if the subsequent retry
  808. then fails.
  809. @type failAfterFailures: L{int} or None
  810. @return: a Deferred that fires with a protocol produced by the
  811. factory passed to C{__init__}
  812. @rtype: L{Deferred} that may:
  813. - fire with L{IProtocol}
  814. - fail with L{CancelledError} when the service is stopped
  815. - fail with e.g.
  816. L{DNSLookupError<twisted.internet.error.DNSLookupError>} or
  817. L{ConnectionRefusedError<twisted.internet.error.ConnectionRefusedError>}
  818. when the number of consecutive failed connection attempts
  819. equals the value of "failAfterFailures"
  820. """
  821. return self._machine.whenConnected(failAfterFailures)
  822. def startService(self):
  823. """
  824. Start this L{ClientService}, initiating the connection retry loop.
  825. """
  826. if self.running:
  827. self._log.warn("Duplicate ClientService.startService {log_source}")
  828. return
  829. super(ClientService, self).startService()
  830. self._machine.start()
  831. def stopService(self):
  832. """
  833. Stop attempting to reconnect and close any existing connections.
  834. @return: a L{Deferred} that fires when all outstanding connections are
  835. closed and all in-progress connection attempts halted.
  836. """
  837. super(ClientService, self).stopService()
  838. return self._machine.stop()
  839. __all__ = (['TimerService', 'CooperatorService', 'MulticastServer',
  840. 'StreamServerEndpointService', 'UDPServer',
  841. 'ClientService'] +
  842. [tran + side
  843. for tran in 'TCP UNIX SSL UNIXDatagram'.split()
  844. for side in 'Server Client'.split()])