connection.py 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. """Core connection objects"""
  2. # disable too-many-lines
  3. # pylint: disable=C0302
  4. import abc
  5. import ast
  6. import copy
  7. import functools
  8. import logging
  9. import math
  10. import numbers
  11. import platform
  12. import ssl
  13. import pika.callback
  14. import pika.channel
  15. import pika.compat
  16. import pika.credentials
  17. import pika.exceptions as exceptions
  18. import pika.frame as frame
  19. import pika.heartbeat
  20. import pika.spec as spec
  21. import pika.validators as validators
  22. from pika.compat import (
  23. xrange,
  24. url_unquote,
  25. dictkeys,
  26. dict_itervalues,
  27. dict_iteritems)
  28. PRODUCT = "Pika Python Client Library"
  29. LOGGER = logging.getLogger(__name__)
  30. class Parameters(object): # pylint: disable=R0902
  31. """Base connection parameters class definition
  32. """
  33. # Declare slots to protect against accidental assignment of an invalid
  34. # attribute
  35. __slots__ = ('_blocked_connection_timeout', '_channel_max',
  36. '_client_properties', '_connection_attempts', '_credentials',
  37. '_frame_max', '_heartbeat', '_host', '_locale', '_port',
  38. '_retry_delay', '_socket_timeout', '_stack_timeout',
  39. '_ssl_options', '_virtual_host', '_tcp_options')
  40. DEFAULT_USERNAME = 'guest'
  41. DEFAULT_PASSWORD = 'guest'
  42. DEFAULT_BLOCKED_CONNECTION_TIMEOUT = None
  43. DEFAULT_CHANNEL_MAX = pika.channel.MAX_CHANNELS
  44. DEFAULT_CLIENT_PROPERTIES = None
  45. DEFAULT_CREDENTIALS = pika.credentials.PlainCredentials(
  46. DEFAULT_USERNAME, DEFAULT_PASSWORD)
  47. DEFAULT_CONNECTION_ATTEMPTS = 1
  48. DEFAULT_FRAME_MAX = spec.FRAME_MAX_SIZE
  49. DEFAULT_HEARTBEAT_TIMEOUT = None # None accepts server's proposal
  50. DEFAULT_HOST = 'localhost'
  51. DEFAULT_LOCALE = 'en_US'
  52. DEFAULT_PORT = 5672
  53. DEFAULT_RETRY_DELAY = 2.0
  54. DEFAULT_SOCKET_TIMEOUT = 10.0 # socket.connect() timeout
  55. DEFAULT_STACK_TIMEOUT = 15.0 # full-stack TCP/[SSl]/AMQP bring-up timeout
  56. DEFAULT_SSL = False
  57. DEFAULT_SSL_OPTIONS = None
  58. DEFAULT_SSL_PORT = 5671
  59. DEFAULT_VIRTUAL_HOST = '/'
  60. DEFAULT_TCP_OPTIONS = None
  61. def __init__(self):
  62. # If not None, blocked_connection_timeout is the timeout, in seconds,
  63. # for the connection to remain blocked; if the timeout expires, the
  64. # connection will be torn down, triggering the connection's
  65. # on_close_callback
  66. self._blocked_connection_timeout = None
  67. self.blocked_connection_timeout = (
  68. self.DEFAULT_BLOCKED_CONNECTION_TIMEOUT)
  69. self._channel_max = None
  70. self.channel_max = self.DEFAULT_CHANNEL_MAX
  71. self._client_properties = None
  72. self.client_properties = self.DEFAULT_CLIENT_PROPERTIES
  73. self._connection_attempts = None
  74. self.connection_attempts = self.DEFAULT_CONNECTION_ATTEMPTS
  75. self._credentials = None
  76. self.credentials = self.DEFAULT_CREDENTIALS
  77. self._frame_max = None
  78. self.frame_max = self.DEFAULT_FRAME_MAX
  79. self._heartbeat = None
  80. self.heartbeat = self.DEFAULT_HEARTBEAT_TIMEOUT
  81. self._host = None
  82. self.host = self.DEFAULT_HOST
  83. self._locale = None
  84. self.locale = self.DEFAULT_LOCALE
  85. self._port = None
  86. self.port = self.DEFAULT_PORT
  87. self._retry_delay = None
  88. self.retry_delay = self.DEFAULT_RETRY_DELAY
  89. self._socket_timeout = None
  90. self.socket_timeout = self.DEFAULT_SOCKET_TIMEOUT
  91. self._stack_timeout = None
  92. self.stack_timeout = self.DEFAULT_STACK_TIMEOUT
  93. self._ssl_options = None
  94. self.ssl_options = self.DEFAULT_SSL_OPTIONS
  95. self._virtual_host = None
  96. self.virtual_host = self.DEFAULT_VIRTUAL_HOST
  97. self._tcp_options = None
  98. self.tcp_options = self.DEFAULT_TCP_OPTIONS
  99. def __repr__(self):
  100. """Represent the info about the instance.
  101. :rtype: str
  102. """
  103. return ('<%s host=%s port=%s virtual_host=%s ssl=%s>' %
  104. (self.__class__.__name__, self.host, self.port,
  105. self.virtual_host, bool(self.ssl_options)))
  106. def __eq__(self, other):
  107. if isinstance(other, Parameters):
  108. return self._host == other._host and self._port == other._port # pylint: disable=W0212
  109. return NotImplemented
  110. def __ne__(self, other):
  111. result = self.__eq__(other)
  112. if result is not NotImplemented:
  113. return not result
  114. return NotImplemented
  115. @property
  116. def blocked_connection_timeout(self):
  117. """
  118. :returns: blocked connection timeout. Defaults to
  119. `DEFAULT_BLOCKED_CONNECTION_TIMEOUT`.
  120. :rtype: float|None
  121. """
  122. return self._blocked_connection_timeout
  123. @blocked_connection_timeout.setter
  124. def blocked_connection_timeout(self, value):
  125. """
  126. :param value: If not None, blocked_connection_timeout is the timeout, in
  127. seconds, for the connection to remain blocked; if the timeout
  128. expires, the connection will be torn down, triggering the
  129. connection's on_close_callback
  130. """
  131. if value is not None:
  132. if not isinstance(value, numbers.Real):
  133. raise TypeError('blocked_connection_timeout must be a Real '
  134. 'number, but got %r' % (value,))
  135. if value < 0:
  136. raise ValueError('blocked_connection_timeout must be >= 0, but '
  137. 'got %r' % (value,))
  138. self._blocked_connection_timeout = value
  139. @property
  140. def channel_max(self):
  141. """
  142. :returns: max preferred number of channels. Defaults to
  143. `DEFAULT_CHANNEL_MAX`.
  144. :rtype: int
  145. """
  146. return self._channel_max
  147. @channel_max.setter
  148. def channel_max(self, value):
  149. """
  150. :param int value: max preferred number of channels, between 1 and
  151. `channel.MAX_CHANNELS`, inclusive
  152. """
  153. if not isinstance(value, numbers.Integral):
  154. raise TypeError('channel_max must be an int, but got %r' % (value,))
  155. if value < 1 or value > pika.channel.MAX_CHANNELS:
  156. raise ValueError('channel_max must be <= %i and > 0, but got %r' %
  157. (pika.channel.MAX_CHANNELS, value))
  158. self._channel_max = value
  159. @property
  160. def client_properties(self):
  161. """
  162. :returns: client properties used to override the fields in the default
  163. client poperties reported to RabbitMQ via `Connection.StartOk`
  164. method. Defaults to `DEFAULT_CLIENT_PROPERTIES`.
  165. :rtype: dict|None
  166. """
  167. return self._client_properties
  168. @client_properties.setter
  169. def client_properties(self, value):
  170. """
  171. :param value: None or dict of client properties used to override the
  172. fields in the default client poperties reported to RabbitMQ via
  173. `Connection.StartOk` method.
  174. """
  175. if not isinstance(value, (
  176. dict,
  177. type(None),
  178. )):
  179. raise TypeError('client_properties must be dict or None, '
  180. 'but got %r' % (value,))
  181. # Copy the mutable object to avoid accidental side-effects
  182. self._client_properties = copy.deepcopy(value)
  183. @property
  184. def connection_attempts(self):
  185. """
  186. :returns: number of socket connection attempts. Defaults to
  187. `DEFAULT_CONNECTION_ATTEMPTS`. See also `retry_delay`.
  188. :rtype: int
  189. """
  190. return self._connection_attempts
  191. @connection_attempts.setter
  192. def connection_attempts(self, value):
  193. """
  194. :param int value: number of socket connection attempts of at least 1.
  195. See also `retry_delay`.
  196. """
  197. if not isinstance(value, numbers.Integral):
  198. raise TypeError('connection_attempts must be an int')
  199. if value < 1:
  200. raise ValueError(
  201. 'connection_attempts must be > 0, but got %r' % (value,))
  202. self._connection_attempts = value
  203. @property
  204. def credentials(self):
  205. """
  206. :rtype: one of the classes from `pika.credentials.VALID_TYPES`. Defaults
  207. to `DEFAULT_CREDENTIALS`.
  208. """
  209. return self._credentials
  210. @credentials.setter
  211. def credentials(self, value):
  212. """
  213. :param value: authentication credential object of one of the classes
  214. from `pika.credentials.VALID_TYPES`
  215. """
  216. if not isinstance(value, tuple(pika.credentials.VALID_TYPES)):
  217. raise TypeError('credentials must be an object of type: %r, but '
  218. 'got %r' % (pika.credentials.VALID_TYPES, value))
  219. # Copy the mutable object to avoid accidental side-effects
  220. self._credentials = copy.deepcopy(value)
  221. @property
  222. def frame_max(self):
  223. """
  224. :returns: desired maximum AMQP frame size to use. Defaults to
  225. `DEFAULT_FRAME_MAX`.
  226. :rtype: int
  227. """
  228. return self._frame_max
  229. @frame_max.setter
  230. def frame_max(self, value):
  231. """
  232. :param int value: desired maximum AMQP frame size to use between
  233. `spec.FRAME_MIN_SIZE` and `spec.FRAME_MAX_SIZE`, inclusive
  234. """
  235. if not isinstance(value, numbers.Integral):
  236. raise TypeError('frame_max must be an int, but got %r' % (value,))
  237. if value < spec.FRAME_MIN_SIZE:
  238. raise ValueError('Min AMQP 0.9.1 Frame Size is %i, but got %r' % (
  239. spec.FRAME_MIN_SIZE,
  240. value,
  241. ))
  242. elif value > spec.FRAME_MAX_SIZE:
  243. raise ValueError('Max AMQP 0.9.1 Frame Size is %i, but got %r' % (
  244. spec.FRAME_MAX_SIZE,
  245. value,
  246. ))
  247. self._frame_max = value
  248. @property
  249. def heartbeat(self):
  250. """
  251. :returns: AMQP connection heartbeat timeout value for negotiation during
  252. connection tuning or callable which is invoked during connection tuning.
  253. None to accept broker's value. 0 turns heartbeat off. Defaults to
  254. `DEFAULT_HEARTBEAT_TIMEOUT`.
  255. :rtype: int|callable|None
  256. """
  257. return self._heartbeat
  258. @heartbeat.setter
  259. def heartbeat(self, value):
  260. """
  261. :param int|None|callable value: Controls AMQP heartbeat timeout negotiation
  262. during connection tuning. An integer value always overrides the value
  263. proposed by broker. Use 0 to deactivate heartbeats and None to always
  264. accept the broker's proposal. If a callable is given, it will be called
  265. with the connection instance and the heartbeat timeout proposed by broker
  266. as its arguments. The callback should return a non-negative integer that
  267. will be used to override the broker's proposal.
  268. """
  269. if value is not None:
  270. if not isinstance(value, numbers.Integral) and not callable(value):
  271. raise TypeError(
  272. 'heartbeat must be an int or a callable function, but got %r'
  273. % (value,))
  274. if not callable(value) and value < 0:
  275. raise ValueError('heartbeat must >= 0, but got %r' % (value,))
  276. self._heartbeat = value
  277. @property
  278. def host(self):
  279. """
  280. :returns: hostname or ip address of broker. Defaults to `DEFAULT_HOST`.
  281. :rtype: str
  282. """
  283. return self._host
  284. @host.setter
  285. def host(self, value):
  286. """
  287. :param str value: hostname or ip address of broker
  288. """
  289. validators.require_string(value, 'host')
  290. self._host = value
  291. @property
  292. def locale(self):
  293. """
  294. :returns: locale value to pass to broker; e.g., 'en_US'. Defaults to
  295. `DEFAULT_LOCALE`.
  296. :rtype: str
  297. """
  298. return self._locale
  299. @locale.setter
  300. def locale(self, value):
  301. """
  302. :param str value: locale value to pass to broker; e.g., "en_US"
  303. """
  304. validators.require_string(value, 'locale')
  305. self._locale = value
  306. @property
  307. def port(self):
  308. """
  309. :returns: port number of broker's listening socket. Defaults to
  310. `DEFAULT_PORT`.
  311. :rtype: int
  312. """
  313. return self._port
  314. @port.setter
  315. def port(self, value):
  316. """
  317. :param int value: port number of broker's listening socket
  318. """
  319. try:
  320. self._port = int(value)
  321. except (TypeError, ValueError):
  322. raise TypeError('port must be an int, but got %r' % (value,))
  323. @property
  324. def retry_delay(self):
  325. """
  326. :returns: interval between socket connection attempts; see also
  327. `connection_attempts`. Defaults to `DEFAULT_RETRY_DELAY`.
  328. :rtype: float
  329. """
  330. return self._retry_delay
  331. @retry_delay.setter
  332. def retry_delay(self, value):
  333. """
  334. :param int | float value: interval between socket connection attempts;
  335. see also `connection_attempts`.
  336. """
  337. if not isinstance(value, numbers.Real):
  338. raise TypeError(
  339. 'retry_delay must be a float or int, but got %r' % (value,))
  340. self._retry_delay = value
  341. @property
  342. def socket_timeout(self):
  343. """
  344. :returns: socket connect timeout in seconds. Defaults to
  345. `DEFAULT_SOCKET_TIMEOUT`. The value None disables this timeout.
  346. :rtype: float|None
  347. """
  348. return self._socket_timeout
  349. @socket_timeout.setter
  350. def socket_timeout(self, value):
  351. """
  352. :param int | float | None value: positive socket connect timeout in
  353. seconds. None to disable this timeout.
  354. """
  355. if value is not None:
  356. if not isinstance(value, numbers.Real):
  357. raise TypeError('socket_timeout must be a float or int, '
  358. 'but got %r' % (value,))
  359. if value <= 0:
  360. raise ValueError(
  361. 'socket_timeout must be > 0, but got %r' % (value,))
  362. value = float(value)
  363. self._socket_timeout = value
  364. @property
  365. def stack_timeout(self):
  366. """
  367. :returns: full protocol stack TCP/[SSL]/AMQP bring-up timeout in
  368. seconds. Defaults to `DEFAULT_STACK_TIMEOUT`. The value None
  369. disables this timeout.
  370. :rtype: float
  371. """
  372. return self._stack_timeout
  373. @stack_timeout.setter
  374. def stack_timeout(self, value):
  375. """
  376. :param int | float | None value: positive full protocol stack
  377. TCP/[SSL]/AMQP bring-up timeout in seconds. It's recommended to set
  378. this value higher than `socket_timeout`. None to disable this
  379. timeout.
  380. """
  381. if value is not None:
  382. if not isinstance(value, numbers.Real):
  383. raise TypeError('stack_timeout must be a float or int, '
  384. 'but got %r' % (value,))
  385. if value <= 0:
  386. raise ValueError(
  387. 'stack_timeout must be > 0, but got %r' % (value,))
  388. value = float(value)
  389. self._stack_timeout = value
  390. @property
  391. def ssl_options(self):
  392. """
  393. :returns: None for plaintext or `pika.SSLOptions` instance for SSL/TLS.
  394. :rtype: `pika.SSLOptions`|None
  395. """
  396. return self._ssl_options
  397. @ssl_options.setter
  398. def ssl_options(self, value):
  399. """
  400. :param `pika.SSLOptions`|None value: None for plaintext or
  401. `pika.SSLOptions` instance for SSL/TLS. Defaults to None.
  402. """
  403. if not isinstance(value, (SSLOptions, type(None))):
  404. raise TypeError(
  405. 'ssl_options must be None or SSLOptions but got %r' % (value,))
  406. self._ssl_options = value
  407. @property
  408. def virtual_host(self):
  409. """
  410. :returns: rabbitmq virtual host name. Defaults to
  411. `DEFAULT_VIRTUAL_HOST`.
  412. :rtype: str
  413. """
  414. return self._virtual_host
  415. @virtual_host.setter
  416. def virtual_host(self, value):
  417. """
  418. :param str value: rabbitmq virtual host name
  419. """
  420. validators.require_string(value, 'virtual_host')
  421. self._virtual_host = value
  422. @property
  423. def tcp_options(self):
  424. """
  425. :returns: None or a dict of options to pass to the underlying socket
  426. :rtype: dict|None
  427. """
  428. return self._tcp_options
  429. @tcp_options.setter
  430. def tcp_options(self, value):
  431. """
  432. :param dict|None value: None or a dict of options to pass to the underlying
  433. socket. Currently supported are TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT
  434. and TCP_USER_TIMEOUT. Availability of these may depend on your platform.
  435. """
  436. if not isinstance(value, (dict, type(None))):
  437. raise TypeError(
  438. 'tcp_options must be a dict or None, but got %r' % (value,))
  439. self._tcp_options = value
  440. class ConnectionParameters(Parameters):
  441. """Connection parameters object that is passed into the connection adapter
  442. upon construction.
  443. """
  444. # Protect against accidental assignment of an invalid attribute
  445. __slots__ = ()
  446. class _DEFAULT(object):
  447. """Designates default parameter value; internal use"""
  448. def __init__( # pylint: disable=R0913,R0914
  449. self,
  450. host=_DEFAULT,
  451. port=_DEFAULT,
  452. virtual_host=_DEFAULT,
  453. credentials=_DEFAULT,
  454. channel_max=_DEFAULT,
  455. frame_max=_DEFAULT,
  456. heartbeat=_DEFAULT,
  457. ssl_options=_DEFAULT,
  458. connection_attempts=_DEFAULT,
  459. retry_delay=_DEFAULT,
  460. socket_timeout=_DEFAULT,
  461. stack_timeout=_DEFAULT,
  462. locale=_DEFAULT,
  463. blocked_connection_timeout=_DEFAULT,
  464. client_properties=_DEFAULT,
  465. tcp_options=_DEFAULT,
  466. **kwargs):
  467. """Create a new ConnectionParameters instance. See `Parameters` for
  468. default values.
  469. :param str host: Hostname or IP Address to connect to
  470. :param int port: TCP port to connect to
  471. :param str virtual_host: RabbitMQ virtual host to use
  472. :param pika.credentials.Credentials credentials: auth credentials
  473. :param int channel_max: Maximum number of channels to allow
  474. :param int frame_max: The maximum byte size for an AMQP frame
  475. :param int|None|callable heartbeat: Controls AMQP heartbeat timeout negotiation
  476. during connection tuning. An integer value always overrides the value
  477. proposed by broker. Use 0 to deactivate heartbeats and None to always
  478. accept the broker's proposal. If a callable is given, it will be called
  479. with the connection instance and the heartbeat timeout proposed by broker
  480. as its arguments. The callback should return a non-negative integer that
  481. will be used to override the broker's proposal.
  482. :param `pika.SSLOptions`|None ssl_options: None for plaintext or
  483. `pika.SSLOptions` instance for SSL/TLS. Defaults to None.
  484. :param int connection_attempts: Maximum number of retry attempts
  485. :param int|float retry_delay: Time to wait in seconds, before the next
  486. :param int|float socket_timeout: Positive socket connect timeout in
  487. seconds.
  488. :param int|float stack_timeout: Positive full protocol stack
  489. (TCP/[SSL]/AMQP) bring-up timeout in seconds. It's recommended to
  490. set this value higher than `socket_timeout`.
  491. :param str locale: Set the locale value
  492. :param int|float|None blocked_connection_timeout: If not None,
  493. the value is a non-negative timeout, in seconds, for the
  494. connection to remain blocked (triggered by Connection.Blocked from
  495. broker); if the timeout expires before connection becomes unblocked,
  496. the connection will be torn down, triggering the adapter-specific
  497. mechanism for informing client app about the closed connection:
  498. passing `ConnectionBlockedTimeout` exception to on_close_callback
  499. in asynchronous adapters or raising it in `BlockingConnection`.
  500. :param client_properties: None or dict of client properties used to
  501. override the fields in the default client properties reported to
  502. RabbitMQ via `Connection.StartOk` method.
  503. :param tcp_options: None or a dict of TCP options to set for socket
  504. """
  505. super(ConnectionParameters, self).__init__()
  506. if blocked_connection_timeout is not self._DEFAULT:
  507. self.blocked_connection_timeout = blocked_connection_timeout
  508. if channel_max is not self._DEFAULT:
  509. self.channel_max = channel_max
  510. if client_properties is not self._DEFAULT:
  511. self.client_properties = client_properties
  512. if connection_attempts is not self._DEFAULT:
  513. self.connection_attempts = connection_attempts
  514. if credentials is not self._DEFAULT:
  515. self.credentials = credentials
  516. if frame_max is not self._DEFAULT:
  517. self.frame_max = frame_max
  518. if heartbeat is not self._DEFAULT:
  519. self.heartbeat = heartbeat
  520. if host is not self._DEFAULT:
  521. self.host = host
  522. if locale is not self._DEFAULT:
  523. self.locale = locale
  524. if retry_delay is not self._DEFAULT:
  525. self.retry_delay = retry_delay
  526. if socket_timeout is not self._DEFAULT:
  527. self.socket_timeout = socket_timeout
  528. if stack_timeout is not self._DEFAULT:
  529. self.stack_timeout = stack_timeout
  530. if ssl_options is not self._DEFAULT:
  531. self.ssl_options = ssl_options
  532. # Set port after SSL status is known
  533. if port is not self._DEFAULT:
  534. self.port = port
  535. else:
  536. self.port = self.DEFAULT_SSL_PORT if self.ssl_options else self.DEFAULT_PORT
  537. if virtual_host is not self._DEFAULT:
  538. self.virtual_host = virtual_host
  539. if tcp_options is not self._DEFAULT:
  540. self.tcp_options = tcp_options
  541. if kwargs:
  542. raise TypeError('unexpected kwargs: %r' % (kwargs,))
  543. class URLParameters(Parameters):
  544. """Connect to RabbitMQ via an AMQP URL in the format::
  545. amqp://username:password@host:port/<virtual_host>[?query-string]
  546. Ensure that the virtual host is URI encoded when specified. For example if
  547. you are using the default "/" virtual host, the value should be `%2f`.
  548. See `Parameters` for default values.
  549. Valid query string values are:
  550. - channel_max:
  551. Override the default maximum channel count value
  552. - client_properties:
  553. dict of client properties used to override the fields in the default
  554. client properties reported to RabbitMQ via `Connection.StartOk`
  555. method
  556. - connection_attempts:
  557. Specify how many times pika should try and reconnect before it gives up
  558. - frame_max:
  559. Override the default maximum frame size for communication
  560. - heartbeat:
  561. Desired connection heartbeat timeout for negotiation. If not present
  562. the broker's value is accepted. 0 turns heartbeat off.
  563. - locale:
  564. Override the default `en_US` locale value
  565. - ssl_options:
  566. None for plaintext; for SSL: dict of public ssl context-related
  567. arguments that may be passed to :meth:`ssl.SSLSocket` as kwargs,
  568. except `sock`, `server_side`,`do_handshake_on_connect`, `family`,
  569. `type`, `proto`, `fileno`.
  570. - retry_delay:
  571. The number of seconds to sleep before attempting to connect on
  572. connection failure.
  573. - socket_timeout:
  574. Socket connect timeout value in seconds (float or int)
  575. - stack_timeout:
  576. Positive full protocol stack (TCP/[SSL]/AMQP) bring-up timeout in
  577. seconds. It's recommended to set this value higher than
  578. `socket_timeout`.
  579. - blocked_connection_timeout:
  580. Set the timeout, in seconds, that the connection may remain blocked
  581. (triggered by Connection.Blocked from broker); if the timeout
  582. expires before connection becomes unblocked, the connection will be
  583. torn down, triggering the connection's on_close_callback
  584. - tcp_options:
  585. Set the tcp options for the underlying socket.
  586. :param str url: The AMQP URL to connect to
  587. """
  588. # Protect against accidental assignment of an invalid attribute
  589. __slots__ = ('_all_url_query_values',)
  590. # The name of the private function for parsing and setting a given URL query
  591. # arg is constructed by catenating the query arg's name to this prefix
  592. _SETTER_PREFIX = '_set_url_'
  593. def __init__(self, url):
  594. """Create a new URLParameters instance.
  595. :param str url: The URL value
  596. """
  597. super(URLParameters, self).__init__()
  598. self._all_url_query_values = None
  599. # Handle the Protocol scheme
  600. #
  601. # Fix up scheme amqp(s) to http(s) so urlparse won't barf on python
  602. # prior to 2.7. On Python 2.6.9,
  603. # `urlparse('amqp://127.0.0.1/%2f?socket_timeout=1')` produces an
  604. # incorrect path='/%2f?socket_timeout=1'
  605. if url[0:4].lower() == 'amqp':
  606. url = 'http' + url[4:]
  607. parts = pika.compat.urlparse(url)
  608. if parts.scheme == 'https':
  609. # Create default context which will get overridden by the
  610. # ssl_options URL arg, if any
  611. self.ssl_options = pika.SSLOptions(
  612. context=ssl.create_default_context())
  613. elif parts.scheme == 'http':
  614. self.ssl_options = None
  615. elif parts.scheme:
  616. raise ValueError('Unexpected URL scheme %r; supported scheme '
  617. 'values: amqp, amqps' % (parts.scheme,))
  618. if parts.hostname is not None:
  619. self.host = parts.hostname
  620. # Take care of port after SSL status is known
  621. if parts.port is not None:
  622. self.port = parts.port
  623. else:
  624. self.port = (self.DEFAULT_SSL_PORT
  625. if self.ssl_options else self.DEFAULT_PORT)
  626. if parts.username is not None:
  627. self.credentials = pika.credentials.PlainCredentials(
  628. url_unquote(parts.username), url_unquote(parts.password))
  629. # Get the Virtual Host
  630. if len(parts.path) > 1:
  631. self.virtual_host = url_unquote(parts.path.split('/')[1])
  632. # Handle query string values, validating and assigning them
  633. self._all_url_query_values = pika.compat.url_parse_qs(parts.query)
  634. for name, value in dict_iteritems(self._all_url_query_values):
  635. try:
  636. set_value = getattr(self, self._SETTER_PREFIX + name)
  637. except AttributeError:
  638. raise ValueError('Unknown URL parameter: %r' % (name,))
  639. try:
  640. (value,) = value
  641. except ValueError:
  642. raise ValueError(
  643. 'Expected exactly one value for URL parameter '
  644. '%s, but got %i values: %s' % (name, len(value), value))
  645. set_value(value)
  646. def _set_url_blocked_connection_timeout(self, value):
  647. """Deserialize and apply the corresponding query string arg"""
  648. try:
  649. blocked_connection_timeout = float(value)
  650. except ValueError as exc:
  651. raise ValueError(
  652. 'Invalid blocked_connection_timeout value %r: %r' % (
  653. value,
  654. exc,
  655. ))
  656. self.blocked_connection_timeout = blocked_connection_timeout
  657. def _set_url_channel_max(self, value):
  658. """Deserialize and apply the corresponding query string arg"""
  659. try:
  660. channel_max = int(value)
  661. except ValueError as exc:
  662. raise ValueError('Invalid channel_max value %r: %r' % (
  663. value,
  664. exc,
  665. ))
  666. self.channel_max = channel_max
  667. def _set_url_client_properties(self, value):
  668. """Deserialize and apply the corresponding query string arg"""
  669. self.client_properties = ast.literal_eval(value)
  670. def _set_url_connection_attempts(self, value):
  671. """Deserialize and apply the corresponding query string arg"""
  672. try:
  673. connection_attempts = int(value)
  674. except ValueError as exc:
  675. raise ValueError('Invalid connection_attempts value %r: %r' % (
  676. value,
  677. exc,
  678. ))
  679. self.connection_attempts = connection_attempts
  680. def _set_url_frame_max(self, value):
  681. """Deserialize and apply the corresponding query string arg"""
  682. try:
  683. frame_max = int(value)
  684. except ValueError as exc:
  685. raise ValueError('Invalid frame_max value %r: %r' % (
  686. value,
  687. exc,
  688. ))
  689. self.frame_max = frame_max
  690. def _set_url_heartbeat(self, value):
  691. """Deserialize and apply the corresponding query string arg"""
  692. try:
  693. heartbeat_timeout = int(value)
  694. except ValueError as exc:
  695. raise ValueError('Invalid heartbeat value %r: %r' % (
  696. value,
  697. exc,
  698. ))
  699. self.heartbeat = heartbeat_timeout
  700. def _set_url_locale(self, value):
  701. """Deserialize and apply the corresponding query string arg"""
  702. self.locale = value
  703. def _set_url_retry_delay(self, value):
  704. """Deserialize and apply the corresponding query string arg"""
  705. try:
  706. retry_delay = float(value)
  707. except ValueError as exc:
  708. raise ValueError('Invalid retry_delay value %r: %r' % (
  709. value,
  710. exc,
  711. ))
  712. self.retry_delay = retry_delay
  713. def _set_url_socket_timeout(self, value):
  714. """Deserialize and apply the corresponding query string arg"""
  715. try:
  716. socket_timeout = float(value)
  717. except ValueError as exc:
  718. raise ValueError('Invalid socket_timeout value %r: %r' % (
  719. value,
  720. exc,
  721. ))
  722. self.socket_timeout = socket_timeout
  723. def _set_url_stack_timeout(self, value):
  724. """Deserialize and apply the corresponding query string arg"""
  725. try:
  726. stack_timeout = float(value)
  727. except ValueError as exc:
  728. raise ValueError('Invalid stack_timeout value %r: %r' % (
  729. value,
  730. exc,
  731. ))
  732. self.stack_timeout = stack_timeout
  733. def _set_url_ssl_options(self, value):
  734. """Deserialize and apply the corresponding query string arg
  735. """
  736. opts = ast.literal_eval(value)
  737. if opts is None:
  738. if self.ssl_options is not None:
  739. raise ValueError(
  740. 'Specified ssl_options=None URL arg is inconsistent with '
  741. 'the specified https URL scheme.')
  742. else:
  743. # Older versions of Pika would take the opts dict and pass it
  744. # directly as kwargs to the deprecated ssl.wrap_socket method.
  745. # Here, we take the valid options and translate them into args
  746. # for various SSLContext methods.
  747. #
  748. # https://docs.python.org/3/library/ssl.html#ssl.wrap_socket
  749. #
  750. # SSLContext.load_verify_locations(cafile=None, capath=None, cadata=None)
  751. try:
  752. opt_protocol = ssl.PROTOCOL_TLS
  753. except AttributeError:
  754. opt_protocol = ssl.PROTOCOL_TLSv1
  755. if 'protocol' in opts:
  756. opt_protocol = opts['protocol']
  757. cxt = ssl.SSLContext(protocol=opt_protocol)
  758. opt_cafile = opts.get('ca_certs') or opts.get('cafile')
  759. opt_capath = opts.get('ca_path') or opts.get('capath')
  760. opt_cadata = opts.get('ca_data') or opts.get('cadata')
  761. cxt.load_verify_locations(opt_cafile, opt_capath, opt_cadata)
  762. # SSLContext.load_cert_chain(certfile, keyfile=None, password=None)
  763. if 'certfile' in opts:
  764. opt_certfile = opts['certfile']
  765. opt_keyfile = opts.get('keyfile')
  766. opt_password = opts.get('password')
  767. cxt.load_cert_chain(opt_certfile, opt_keyfile, opt_password)
  768. if 'ciphers' in opts:
  769. opt_ciphers = opts['ciphers']
  770. cxt.set_ciphers(opt_ciphers)
  771. server_hostname = opts.get('server_hostname')
  772. self.ssl_options = pika.SSLOptions(
  773. context=cxt, server_hostname=server_hostname)
  774. def _set_url_tcp_options(self, value):
  775. """Deserialize and apply the corresponding query string arg"""
  776. self.tcp_options = ast.literal_eval(value)
  777. class SSLOptions(object):
  778. """Class used to provide parameters for optional fine grained control of SSL
  779. socket wrapping.
  780. """
  781. # Protect against accidental assignment of an invalid attribute
  782. __slots__ = ('context', 'server_hostname')
  783. def __init__(self, context, server_hostname=None):
  784. """
  785. :param ssl.SSLContext context: SSLContext instance
  786. :param str|None server_hostname: SSLContext.wrap_socket, used to enable
  787. SNI
  788. """
  789. if not isinstance(context, ssl.SSLContext):
  790. raise TypeError(
  791. 'context must be of ssl.SSLContext type, but got {!r}'.format(
  792. context))
  793. self.context = context
  794. self.server_hostname = server_hostname
  795. class Connection(pika.compat.AbstractBase):
  796. """This is the core class that implements communication with RabbitMQ. This
  797. class should not be invoked directly but rather through the use of an
  798. adapter such as SelectConnection or BlockingConnection.
  799. """
  800. # Disable pylint messages concerning "method could be a funciton"
  801. # pylint: disable=R0201
  802. ON_CONNECTION_CLOSED = '_on_connection_closed'
  803. ON_CONNECTION_ERROR = '_on_connection_error'
  804. ON_CONNECTION_OPEN_OK = '_on_connection_open_ok'
  805. CONNECTION_CLOSED = 0
  806. CONNECTION_INIT = 1
  807. CONNECTION_PROTOCOL = 2
  808. CONNECTION_START = 3
  809. CONNECTION_TUNE = 4
  810. CONNECTION_OPEN = 5
  811. CONNECTION_CLOSING = 6 # client-initiated close in progress
  812. _STATE_NAMES = {
  813. CONNECTION_CLOSED: 'CLOSED',
  814. CONNECTION_INIT: 'INIT',
  815. CONNECTION_PROTOCOL: 'PROTOCOL',
  816. CONNECTION_START: 'START',
  817. CONNECTION_TUNE: 'TUNE',
  818. CONNECTION_OPEN: 'OPEN',
  819. CONNECTION_CLOSING: 'CLOSING'
  820. }
  821. def __init__(self,
  822. parameters=None,
  823. on_open_callback=None,
  824. on_open_error_callback=None,
  825. on_close_callback=None,
  826. internal_connection_workflow=True):
  827. """Connection initialization expects an object that has implemented the
  828. Parameters class and a callback function to notify when we have
  829. successfully connected to the AMQP Broker.
  830. Available Parameters classes are the ConnectionParameters class and
  831. URLParameters class.
  832. :param pika.connection.Parameters parameters: Read-only connection
  833. parameters.
  834. :param callable on_open_callback: Called when the connection is opened:
  835. on_open_callback(connection)
  836. :param None | method on_open_error_callback: Called if the connection
  837. can't be established or connection establishment is interrupted by
  838. `Connection.close()`: on_open_error_callback(Connection, exception).
  839. :param None | method on_close_callback: Called when a previously fully
  840. open connection is closed:
  841. `on_close_callback(Connection, exception)`, where `exception` is
  842. either an instance of `exceptions.ConnectionClosed` if closed by
  843. user or broker or exception of another type that describes the cause
  844. of connection failure.
  845. :param bool internal_connection_workflow: True for autonomous connection
  846. establishment which is default; False for externally-managed
  847. connection workflow via the `create_connection()` factory.
  848. """
  849. self.connection_state = self.CONNECTION_CLOSED
  850. # Determines whether we invoke the on_open_error_callback or
  851. # on_close_callback. So that we don't lose track when state transitions
  852. # to CONNECTION_CLOSING as the result of Connection.close() call during
  853. # opening.
  854. self._opened = False
  855. # Value to pass to on_open_error_callback or on_close_callback when
  856. # connection fails to be established or becomes closed
  857. self._error = None # type: Exception
  858. # Used to hold timer if configured for Connection.Blocked timeout
  859. self._blocked_conn_timer = None
  860. self._heartbeat_checker = None
  861. # Set our configuration options
  862. if parameters is not None:
  863. # NOTE: Work around inability to copy ssl.SSLContext contained in
  864. # our SSLOptions; ssl.SSLContext fails to implement __getnewargs__
  865. saved_ssl_options = parameters.ssl_options
  866. parameters.ssl_options = None
  867. try:
  868. self.params = copy.deepcopy(parameters)
  869. self.params.ssl_options = saved_ssl_options
  870. finally:
  871. parameters.ssl_options = saved_ssl_options
  872. else:
  873. self.params = ConnectionParameters()
  874. self._internal_connection_workflow = internal_connection_workflow
  875. # Define our callback dictionary
  876. self.callbacks = pika.callback.CallbackManager()
  877. # Attributes that will be properly initialized by _init_connection_state
  878. # and/or during connection handshake.
  879. self.server_capabilities = None
  880. self.server_properties = None
  881. self._body_max_length = None
  882. self.known_hosts = None
  883. self._frame_buffer = None
  884. self._channels = None
  885. self._init_connection_state()
  886. # Add the on connection error callback
  887. self.callbacks.add(
  888. 0, self.ON_CONNECTION_ERROR, on_open_error_callback or
  889. self._default_on_connection_error, False)
  890. # On connection callback
  891. if on_open_callback:
  892. self.add_on_open_callback(on_open_callback)
  893. # On connection callback
  894. if on_close_callback:
  895. self.add_on_close_callback(on_close_callback)
  896. self._set_connection_state(self.CONNECTION_INIT)
  897. if self._internal_connection_workflow:
  898. # Kick off full-stack connection establishment. It will complete
  899. # asynchronously.
  900. self._adapter_connect_stream()
  901. else:
  902. # Externally-managed connection workflow will proceed asynchronously
  903. # using adapter-specific mechanism
  904. LOGGER.debug('Using external connection workflow.')
  905. def _init_connection_state(self):
  906. """Initialize or reset all of the internal state variables for a given
  907. connection. On disconnect or reconnect all of the state needs to
  908. be wiped.
  909. """
  910. # TODO: probably don't need the state recovery logic since we don't
  911. # test re-connection sufficiently (if at all), and users should
  912. # just create a new instance of Connection when needed.
  913. # So, just merge the pertinent logic into the constructor.
  914. # Connection state
  915. self._set_connection_state(self.CONNECTION_CLOSED)
  916. # Negotiated server properties
  917. self.server_properties = None
  918. # Inbound buffer for decoding frames
  919. self._frame_buffer = bytes()
  920. # Dict of open channels
  921. self._channels = dict()
  922. # Data used for Heartbeat checking and back-pressure detection
  923. self.bytes_sent = 0
  924. self.bytes_received = 0
  925. self.frames_sent = 0
  926. self.frames_received = 0
  927. self._heartbeat_checker = None
  928. # When closing, holds reason why
  929. self._error = None
  930. # Our starting point once connected, first frame received
  931. self._add_connection_start_callback()
  932. # Add a callback handler for the Broker telling us to disconnect.
  933. # NOTE: As of RabbitMQ 3.6.0, RabbitMQ broker may send Connection.Close
  934. # to signal error during connection setup (and wait a longish time
  935. # before closing the TCP/IP stream). Earlier RabbitMQ versions
  936. # simply closed the TCP/IP stream.
  937. self.callbacks.add(0, spec.Connection.Close,
  938. self._on_connection_close_from_broker)
  939. if self.params.blocked_connection_timeout is not None:
  940. if self._blocked_conn_timer is not None:
  941. # Blocked connection timer was active when teardown was
  942. # initiated
  943. self._adapter_remove_timeout(self._blocked_conn_timer)
  944. self._blocked_conn_timer = None
  945. self.add_on_connection_blocked_callback(self._on_connection_blocked)
  946. self.add_on_connection_unblocked_callback(
  947. self._on_connection_unblocked)
  948. def add_on_close_callback(self, callback):
  949. """Add a callback notification when the connection has closed. The
  950. callback will be passed the connection and an exception instance. The
  951. exception will either be an instance of `exceptions.ConnectionClosed` if
  952. a fully-open connection was closed by user or broker or exception of
  953. another type that describes the cause of connection closure/failure.
  954. :param callable callback: Callback to call on close, having the signature:
  955. callback(pika.connection.Connection, exception)
  956. """
  957. validators.require_callback(callback)
  958. self.callbacks.add(0, self.ON_CONNECTION_CLOSED, callback, False)
  959. def add_on_connection_blocked_callback(self, callback):
  960. """RabbitMQ AMQP extension - Add a callback to be notified when the
  961. connection gets blocked (`Connection.Blocked` received from RabbitMQ)
  962. due to the broker running low on resources (memory or disk). In this
  963. state RabbitMQ suspends processing incoming data until the connection
  964. is unblocked, so it's a good idea for publishers receiving this
  965. notification to suspend publishing until the connection becomes
  966. unblocked.
  967. See also `Connection.add_on_connection_unblocked_callback()`
  968. See also `ConnectionParameters.blocked_connection_timeout`.
  969. :param callable callback: Callback to call on `Connection.Blocked`,
  970. having the signature `callback(connection, pika.frame.Method)`,
  971. where the method frame's `method` member is of type
  972. `pika.spec.Connection.Blocked`
  973. """
  974. validators.require_callback(callback)
  975. self.callbacks.add(
  976. 0,
  977. spec.Connection.Blocked,
  978. functools.partial(callback, self),
  979. one_shot=False)
  980. def add_on_connection_unblocked_callback(self, callback):
  981. """RabbitMQ AMQP extension - Add a callback to be notified when the
  982. connection gets unblocked (`Connection.Unblocked` frame is received from
  983. RabbitMQ) letting publishers know it's ok to start publishing again.
  984. :param callable callback: Callback to call on
  985. `Connection.Unblocked`, having the signature
  986. `callback(connection, pika.frame.Method)`, where the method frame's
  987. `method` member is of type `pika.spec.Connection.Unblocked`
  988. """
  989. validators.require_callback(callback)
  990. self.callbacks.add(
  991. 0,
  992. spec.Connection.Unblocked,
  993. functools.partial(callback, self),
  994. one_shot=False)
  995. def add_on_open_callback(self, callback):
  996. """Add a callback notification when the connection has opened. The
  997. callback will be passed the connection instance as its only arg.
  998. :param callable callback: Callback to call when open
  999. """
  1000. validators.require_callback(callback)
  1001. self.callbacks.add(0, self.ON_CONNECTION_OPEN_OK, callback, False)
  1002. def add_on_open_error_callback(self, callback, remove_default=True):
  1003. """Add a callback notification when the connection can not be opened.
  1004. The callback method should accept the connection instance that could not
  1005. connect, and either a string or an exception as its second arg.
  1006. :param callable callback: Callback to call when can't connect, having
  1007. the signature _(Connection, Exception)
  1008. :param bool remove_default: Remove default exception raising callback
  1009. """
  1010. validators.require_callback(callback)
  1011. if remove_default:
  1012. self.callbacks.remove(0, self.ON_CONNECTION_ERROR,
  1013. self._default_on_connection_error)
  1014. self.callbacks.add(0, self.ON_CONNECTION_ERROR, callback, False)
  1015. def channel(self, channel_number=None, on_open_callback=None):
  1016. """Create a new channel with the next available channel number or pass
  1017. in a channel number to use. Must be non-zero if you would like to
  1018. specify but it is recommended that you let Pika manage the channel
  1019. numbers.
  1020. :param int channel_number: The channel number to use, defaults to the
  1021. next available.
  1022. :param callable on_open_callback: The callback when the channel is
  1023. opened. The callback will be invoked with the `Channel` instance
  1024. as its only argument.
  1025. :rtype: pika.channel.Channel
  1026. """
  1027. if not self.is_open:
  1028. raise exceptions.ConnectionWrongStateError(
  1029. 'Channel allocation requires an open connection: %s' % self)
  1030. if not channel_number:
  1031. channel_number = self._next_channel_number()
  1032. self._channels[channel_number] = self._create_channel(
  1033. channel_number, on_open_callback)
  1034. self._add_channel_callbacks(channel_number)
  1035. self._channels[channel_number].open()
  1036. return self._channels[channel_number]
  1037. def close(self, reply_code=200, reply_text='Normal shutdown'):
  1038. """Disconnect from RabbitMQ. If there are any open channels, it will
  1039. attempt to close them prior to fully disconnecting. Channels which
  1040. have active consumers will attempt to send a Basic.Cancel to RabbitMQ
  1041. to cleanly stop the delivery of messages prior to closing the channel.
  1042. :param int reply_code: The code number for the close
  1043. :param str reply_text: The text reason for the close
  1044. :raises pika.exceptions.ConnectionWrongStateError: if connection is
  1045. closed or closing.
  1046. """
  1047. if self.is_closing or self.is_closed:
  1048. msg = ('Illegal close({}, {!r}) request on {} because it '
  1049. 'was called while connection state={}.'.format(
  1050. reply_code, reply_text, self,
  1051. self._STATE_NAMES[self.connection_state]))
  1052. LOGGER.error(msg)
  1053. raise exceptions.ConnectionWrongStateError(msg)
  1054. # NOTE The connection is either in opening or open state
  1055. # Initiate graceful closing of channels that are OPEN or OPENING
  1056. if self._channels:
  1057. self._close_channels(reply_code, reply_text)
  1058. prev_state = self.connection_state
  1059. # Transition to closing
  1060. self._set_connection_state(self.CONNECTION_CLOSING)
  1061. LOGGER.info("Closing connection (%s): %r", reply_code, reply_text)
  1062. if not self._opened:
  1063. # It was opening, but not fully open yet, so we won't attempt
  1064. # graceful AMQP Connection.Close.
  1065. LOGGER.info('Connection.close() is terminating stream and '
  1066. 'bypassing graceful AMQP close, since AMQP is still '
  1067. 'opening.')
  1068. error = exceptions.ConnectionOpenAborted(
  1069. 'Connection.close() called before connection '
  1070. 'finished opening: prev_state={} ({}): {!r}'.format(
  1071. self._STATE_NAMES[prev_state], reply_code, reply_text))
  1072. self._terminate_stream(error)
  1073. else:
  1074. self._error = exceptions.ConnectionClosedByClient(
  1075. reply_code, reply_text)
  1076. # If there are channels that haven't finished closing yet, then
  1077. # _on_close_ready will finally be called from _on_channel_cleanup once
  1078. # all channels have been closed
  1079. if not self._channels:
  1080. # We can initiate graceful closing of the connection right away,
  1081. # since no more channels remain
  1082. self._on_close_ready()
  1083. else:
  1084. LOGGER.info(
  1085. 'Connection.close is waiting for %d channels to close: %s',
  1086. len(self._channels), self)
  1087. #
  1088. # Connection state properties
  1089. #
  1090. @property
  1091. def is_closed(self):
  1092. """
  1093. Returns a boolean reporting the current connection state.
  1094. """
  1095. return self.connection_state == self.CONNECTION_CLOSED
  1096. @property
  1097. def is_closing(self):
  1098. """
  1099. Returns True if connection is in the process of closing due to
  1100. client-initiated `close` request, but closing is not yet complete.
  1101. """
  1102. return self.connection_state == self.CONNECTION_CLOSING
  1103. @property
  1104. def is_open(self):
  1105. """
  1106. Returns a boolean reporting the current connection state.
  1107. """
  1108. return self.connection_state == self.CONNECTION_OPEN
  1109. #
  1110. # Properties that reflect server capabilities for the current connection
  1111. #
  1112. @property
  1113. def basic_nack(self):
  1114. """Specifies if the server supports basic.nack on the active connection.
  1115. :rtype: bool
  1116. """
  1117. return self.server_capabilities.get('basic.nack', False)
  1118. @property
  1119. def consumer_cancel_notify(self):
  1120. """Specifies if the server supports consumer cancel notification on the
  1121. active connection.
  1122. :rtype: bool
  1123. """
  1124. return self.server_capabilities.get('consumer_cancel_notify', False)
  1125. @property
  1126. def exchange_exchange_bindings(self):
  1127. """Specifies if the active connection supports exchange to exchange
  1128. bindings.
  1129. :rtype: bool
  1130. """
  1131. return self.server_capabilities.get('exchange_exchange_bindings', False)
  1132. @property
  1133. def publisher_confirms(self):
  1134. """Specifies if the active connection can use publisher confirmations.
  1135. :rtype: bool
  1136. """
  1137. return self.server_capabilities.get('publisher_confirms', False)
  1138. @abc.abstractmethod
  1139. def _adapter_call_later(self, delay, callback):
  1140. """Adapters should override to call the callback after the
  1141. specified number of seconds have elapsed, using a timer, or a
  1142. thread, or similar.
  1143. :param float|int delay: The number of seconds to wait to call callback
  1144. :param callable callback: The callback will be called without args.
  1145. :returns: Handle that can be passed to `_adapter_remove_timeout()` to
  1146. cancel the callback.
  1147. :rtype: object
  1148. """
  1149. raise NotImplementedError
  1150. @abc.abstractmethod
  1151. def _adapter_remove_timeout(self, timeout_id):
  1152. """Adapters should override: Remove a timeout
  1153. :param opaque timeout_id: The timeout handle to remove
  1154. """
  1155. raise NotImplementedError
  1156. @abc.abstractmethod
  1157. def _adapter_add_callback_threadsafe(self, callback):
  1158. """Requests a call to the given function as soon as possible in the
  1159. context of this connection's IOLoop thread.
  1160. NOTE: This is the only thread-safe method offered by the connection. All
  1161. other manipulations of the connection must be performed from the
  1162. connection's thread.
  1163. :param callable callback: The callback method; must be callable.
  1164. """
  1165. raise NotImplementedError
  1166. #
  1167. # Internal methods for managing the communication process
  1168. #
  1169. @abc.abstractmethod
  1170. def _adapter_connect_stream(self):
  1171. """Subclasses should override to initiate stream connection
  1172. workflow asynchronously. Upon failed or aborted completion, they must
  1173. invoke `Connection._on_stream_terminated()`.
  1174. NOTE: On success, the stack will be up already, so there is no
  1175. corresponding callback.
  1176. """
  1177. raise NotImplementedError
  1178. @abc.abstractmethod
  1179. def _adapter_disconnect_stream(self):
  1180. """Asynchronously bring down the streaming transport layer and invoke
  1181. `Connection._on_stream_terminated()` asynchronously when complete.
  1182. :raises: NotImplementedError
  1183. """
  1184. raise NotImplementedError
  1185. @abc.abstractmethod
  1186. def _adapter_emit_data(self, data):
  1187. """Take ownership of data and send it to AMQP server as soon as
  1188. possible.
  1189. Subclasses must override this
  1190. :param bytes data:
  1191. """
  1192. raise NotImplementedError
  1193. def _add_channel_callbacks(self, channel_number):
  1194. """Add the appropriate callbacks for the specified channel number.
  1195. :param int channel_number: The channel number for the callbacks
  1196. """
  1197. # pylint: disable=W0212
  1198. # This permits us to garbage-collect our reference to the channel
  1199. # regardless of whether it was closed by client or broker, and do so
  1200. # after all channel-close callbacks.
  1201. self._channels[channel_number]._add_on_cleanup_callback(
  1202. self._on_channel_cleanup)
  1203. def _add_connection_start_callback(self):
  1204. """Add a callback for when a Connection.Start frame is received from
  1205. the broker.
  1206. """
  1207. self.callbacks.add(0, spec.Connection.Start, self._on_connection_start)
  1208. def _add_connection_tune_callback(self):
  1209. """Add a callback for when a Connection.Tune frame is received."""
  1210. self.callbacks.add(0, spec.Connection.Tune, self._on_connection_tune)
  1211. def _check_for_protocol_mismatch(self, value):
  1212. """Invoked when starting a connection to make sure it's a supported
  1213. protocol.
  1214. :param pika.frame.Method value: The frame to check
  1215. :raises: ProtocolVersionMismatch
  1216. """
  1217. if ((value.method.version_major, value.method.version_minor) !=
  1218. spec.PROTOCOL_VERSION[0:2]):
  1219. raise exceptions.ProtocolVersionMismatch(frame.ProtocolHeader(),
  1220. value)
  1221. @property
  1222. def _client_properties(self):
  1223. """Return the client properties dictionary.
  1224. :rtype: dict
  1225. """
  1226. properties = {
  1227. 'product': PRODUCT,
  1228. 'platform': 'Python %s' % platform.python_version(),
  1229. 'capabilities': {
  1230. 'authentication_failure_close': True,
  1231. 'basic.nack': True,
  1232. 'connection.blocked': True,
  1233. 'consumer_cancel_notify': True,
  1234. 'publisher_confirms': True
  1235. },
  1236. 'information': 'See http://pika.rtfd.org',
  1237. 'version': pika.__version__
  1238. }
  1239. if self.params.client_properties:
  1240. properties.update(self.params.client_properties)
  1241. return properties
  1242. def _close_channels(self, reply_code, reply_text):
  1243. """Initiate graceful closing of channels that are in OPEN or OPENING
  1244. states, passing reply_code and reply_text.
  1245. :param int reply_code: The code for why the channels are being closed
  1246. :param str reply_text: The text reason for why the channels are closing
  1247. """
  1248. assert self.is_open, str(self)
  1249. for channel_number in dictkeys(self._channels):
  1250. chan = self._channels[channel_number]
  1251. if not (chan.is_closing or chan.is_closed):
  1252. chan.close(reply_code, reply_text)
  1253. def _create_channel(self, channel_number, on_open_callback):
  1254. """Create a new channel using the specified channel number and calling
  1255. back the method specified by on_open_callback
  1256. :param int channel_number: The channel number to use
  1257. :param callable on_open_callback: The callback when the channel is
  1258. opened. The callback will be invoked with the `Channel` instance
  1259. as its only argument.
  1260. """
  1261. LOGGER.debug('Creating channel %s', channel_number)
  1262. return pika.channel.Channel(self, channel_number, on_open_callback)
  1263. def _create_heartbeat_checker(self):
  1264. """Create a heartbeat checker instance if there is a heartbeat interval
  1265. set.
  1266. :rtype: pika.heartbeat.Heartbeat|None
  1267. """
  1268. if self.params.heartbeat is not None and self.params.heartbeat > 0:
  1269. LOGGER.debug('Creating a HeartbeatChecker: %r',
  1270. self.params.heartbeat)
  1271. return pika.heartbeat.HeartbeatChecker(self, self.params.heartbeat)
  1272. return None
  1273. def _remove_heartbeat(self):
  1274. """Stop the heartbeat checker if it exists
  1275. """
  1276. if self._heartbeat_checker:
  1277. self._heartbeat_checker.stop()
  1278. self._heartbeat_checker = None
  1279. def _deliver_frame_to_channel(self, value):
  1280. """Deliver the frame to the channel specified in the frame.
  1281. :param pika.frame.Method value: The frame to deliver
  1282. """
  1283. if not value.channel_number in self._channels:
  1284. # This should never happen and would constitute breach of the
  1285. # protocol
  1286. LOGGER.critical(
  1287. 'Received %s frame for unregistered channel %i on %s',
  1288. value.NAME, value.channel_number, self)
  1289. return
  1290. # pylint: disable=W0212
  1291. self._channels[value.channel_number]._handle_content_frame(value)
  1292. def _ensure_closed(self):
  1293. """If the connection is not closed, close it."""
  1294. if self.is_open:
  1295. self.close()
  1296. def _get_body_frame_max_length(self):
  1297. """Calculate the maximum amount of bytes that can be in a body frame.
  1298. :rtype: int
  1299. """
  1300. return (self.params.frame_max - spec.FRAME_HEADER_SIZE -
  1301. spec.FRAME_END_SIZE)
  1302. def _get_credentials(self, method_frame):
  1303. """Get credentials for authentication.
  1304. :param pika.frame.MethodFrame method_frame: The Connection.Start frame
  1305. :rtype: tuple(str, str)
  1306. """
  1307. (auth_type,
  1308. response) = self.params.credentials.response_for(method_frame.method)
  1309. if not auth_type:
  1310. raise exceptions.AuthenticationError(self.params.credentials.TYPE)
  1311. self.params.credentials.erase_credentials()
  1312. return auth_type, response
  1313. def _has_pending_callbacks(self, value):
  1314. """Return true if there are any callbacks pending for the specified
  1315. frame.
  1316. :param pika.frame.Method value: The frame to check
  1317. :rtype: bool
  1318. """
  1319. return self.callbacks.pending(value.channel_number, value.method)
  1320. def _is_method_frame(self, value):
  1321. """Returns true if the frame is a method frame.
  1322. :param pika.frame.Frame value: The frame to evaluate
  1323. :rtype: bool
  1324. """
  1325. return isinstance(value, frame.Method)
  1326. def _is_protocol_header_frame(self, value):
  1327. """Returns True if it's a protocol header frame.
  1328. :rtype: bool
  1329. """
  1330. return isinstance(value, frame.ProtocolHeader)
  1331. def _next_channel_number(self):
  1332. """Return the next available channel number or raise an exception.
  1333. :rtype: int
  1334. """
  1335. limit = self.params.channel_max or pika.channel.MAX_CHANNELS
  1336. if len(self._channels) >= limit:
  1337. raise exceptions.NoFreeChannels()
  1338. for num in xrange(1, len(self._channels) + 1):
  1339. if num not in self._channels:
  1340. return num
  1341. return len(self._channels) + 1
  1342. def _on_channel_cleanup(self, channel):
  1343. """Remove the channel from the dict of channels when Channel.CloseOk is
  1344. sent. If connection is closing and no more channels remain, proceed to
  1345. `_on_close_ready`.
  1346. :param pika.channel.Channel channel: channel instance
  1347. """
  1348. try:
  1349. del self._channels[channel.channel_number]
  1350. LOGGER.debug('Removed channel %s', channel.channel_number)
  1351. except KeyError:
  1352. LOGGER.error('Channel %r not in channels', channel.channel_number)
  1353. if self.is_closing:
  1354. if not self._channels:
  1355. # Initiate graceful closing of the connection
  1356. self._on_close_ready()
  1357. else:
  1358. # Once Connection enters CLOSING state, all remaining channels
  1359. # should also be in CLOSING state. Deviation from this would
  1360. # prevent Connection from completing its closing procedure.
  1361. channels_not_in_closing_state = [
  1362. chan for chan in dict_itervalues(self._channels)
  1363. if not chan.is_closing
  1364. ]
  1365. if channels_not_in_closing_state:
  1366. LOGGER.critical(
  1367. 'Connection in CLOSING state has non-CLOSING '
  1368. 'channels: %r', channels_not_in_closing_state)
  1369. def _on_close_ready(self):
  1370. """Called when the Connection is in a state that it can close after
  1371. a close has been requested by client. This happens after all of the
  1372. channels are closed that were open when the close request was made.
  1373. """
  1374. if self.is_closed:
  1375. LOGGER.warning('_on_close_ready invoked when already closed')
  1376. return
  1377. # NOTE: Assuming self._error is instance of exceptions.ConnectionClosed
  1378. self._send_connection_close(self._error.reply_code,
  1379. self._error.reply_text)
  1380. def _on_stream_connected(self):
  1381. """Invoked when the socket is connected and it's time to start speaking
  1382. AMQP with the broker.
  1383. """
  1384. self._set_connection_state(self.CONNECTION_PROTOCOL)
  1385. # Start the communication with the RabbitMQ Broker
  1386. self._send_frame(frame.ProtocolHeader())
  1387. def _on_blocked_connection_timeout(self):
  1388. """ Called when the "connection blocked timeout" expires. When this
  1389. happens, we tear down the connection
  1390. """
  1391. self._blocked_conn_timer = None
  1392. self._terminate_stream(
  1393. exceptions.ConnectionBlockedTimeout(
  1394. 'Blocked connection timeout expired.'))
  1395. def _on_connection_blocked(self, _connection, method_frame):
  1396. """Handle Connection.Blocked notification from RabbitMQ broker
  1397. :param pika.frame.Method method_frame: method frame having `method`
  1398. member of type `pika.spec.Connection.Blocked`
  1399. """
  1400. LOGGER.warning('Received %s from broker', method_frame)
  1401. if self._blocked_conn_timer is not None:
  1402. # RabbitMQ is not supposed to repeat Connection.Blocked, but it
  1403. # doesn't hurt to be careful
  1404. LOGGER.warning(
  1405. '_blocked_conn_timer %s already set when '
  1406. '_on_connection_blocked is called', self._blocked_conn_timer)
  1407. else:
  1408. self._blocked_conn_timer = self._adapter_call_later(
  1409. self.params.blocked_connection_timeout,
  1410. self._on_blocked_connection_timeout)
  1411. def _on_connection_unblocked(self, _connection, method_frame):
  1412. """Handle Connection.Unblocked notification from RabbitMQ broker
  1413. :param pika.frame.Method method_frame: method frame having `method`
  1414. member of type `pika.spec.Connection.Blocked`
  1415. """
  1416. LOGGER.info('Received %s from broker', method_frame)
  1417. if self._blocked_conn_timer is None:
  1418. # RabbitMQ is supposed to pair Connection.Blocked/Unblocked, but it
  1419. # doesn't hurt to be careful
  1420. LOGGER.warning('_blocked_conn_timer was not active when '
  1421. '_on_connection_unblocked called')
  1422. else:
  1423. self._adapter_remove_timeout(self._blocked_conn_timer)
  1424. self._blocked_conn_timer = None
  1425. def _on_connection_close_from_broker(self, method_frame):
  1426. """Called when the connection is closed remotely via Connection.Close
  1427. frame from broker.
  1428. :param pika.frame.Method method_frame: The Connection.Close frame
  1429. """
  1430. LOGGER.debug('_on_connection_close_from_broker: frame=%s', method_frame)
  1431. self._terminate_stream(
  1432. exceptions.ConnectionClosedByBroker(method_frame.method.reply_code,
  1433. method_frame.method.reply_text))
  1434. def _on_connection_close_ok(self, method_frame):
  1435. """Called when Connection.CloseOk is received from remote.
  1436. :param pika.frame.Method method_frame: The Connection.CloseOk frame
  1437. """
  1438. LOGGER.debug('_on_connection_close_ok: frame=%s', method_frame)
  1439. self._terminate_stream(None)
  1440. def _default_on_connection_error(self, _connection_unused, error):
  1441. """Default behavior when the connecting connection cannot connect and
  1442. user didn't supply own `on_connection_error` callback.
  1443. :raises: the given error
  1444. """
  1445. raise error
  1446. def _on_connection_open_ok(self, method_frame):
  1447. """
  1448. This is called once we have tuned the connection with the server and
  1449. called the Connection.Open on the server and it has replied with
  1450. Connection.Ok.
  1451. """
  1452. self._opened = True
  1453. self.known_hosts = method_frame.method.known_hosts
  1454. # We're now connected at the AMQP level
  1455. self._set_connection_state(self.CONNECTION_OPEN)
  1456. # Call our initial callback that we're open
  1457. self.callbacks.process(0, self.ON_CONNECTION_OPEN_OK, self, self)
  1458. def _on_connection_start(self, method_frame):
  1459. """This is called as a callback once we have received a Connection.Start
  1460. from the server.
  1461. :param pika.frame.Method method_frame: The frame received
  1462. :raises: UnexpectedFrameError
  1463. """
  1464. self._set_connection_state(self.CONNECTION_START)
  1465. try:
  1466. if self._is_protocol_header_frame(method_frame):
  1467. raise exceptions.UnexpectedFrameError(method_frame)
  1468. self._check_for_protocol_mismatch(method_frame)
  1469. self._set_server_information(method_frame)
  1470. self._add_connection_tune_callback()
  1471. self._send_connection_start_ok(*self._get_credentials(method_frame))
  1472. except Exception as error: # pylint: disable=W0703
  1473. LOGGER.exception('Error processing Connection.Start.')
  1474. self._terminate_stream(error)
  1475. @staticmethod
  1476. def _negotiate_integer_value(client_value, server_value):
  1477. """Negotiates two values. If either of them is 0 or None,
  1478. returns the other one. If both are positive integers, returns the
  1479. smallest one.
  1480. :param int client_value: The client value
  1481. :param int server_value: The server value
  1482. :rtype: int
  1483. """
  1484. if client_value is None:
  1485. client_value = 0
  1486. if server_value is None:
  1487. server_value = 0
  1488. # this is consistent with how Java client and Bunny
  1489. # perform negotiation, see pika/pika#874
  1490. if client_value == 0 or server_value == 0:
  1491. val = max(client_value, server_value)
  1492. else:
  1493. val = min(client_value, server_value)
  1494. return val
  1495. @staticmethod
  1496. def _tune_heartbeat_timeout(client_value, server_value):
  1497. """ Determine heartbeat timeout per AMQP 0-9-1 rules
  1498. Per https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf,
  1499. > Both peers negotiate the limits to the lowest agreed value as follows:
  1500. > - The server MUST tell the client what limits it proposes.
  1501. > - The client responds and **MAY reduce those limits** for its
  1502. connection
  1503. If the client specifies a value, it always takes precedence.
  1504. :param client_value: None to accept server_value; otherwise, an integral
  1505. number in seconds; 0 (zero) to disable heartbeat.
  1506. :param server_value: integral value of the heartbeat timeout proposed by
  1507. broker; 0 (zero) to disable heartbeat.
  1508. :returns: the value of the heartbeat timeout to use and return to broker
  1509. :rtype: int
  1510. """
  1511. if client_value is None:
  1512. # Accept server's limit
  1513. timeout = server_value
  1514. else:
  1515. timeout = client_value
  1516. return timeout
  1517. def _on_connection_tune(self, method_frame):
  1518. """Once the Broker sends back a Connection.Tune, we will set our tuning
  1519. variables that have been returned to us and kick off the Heartbeat
  1520. monitor if required, send our TuneOk and then the Connection. Open rpc
  1521. call on channel 0.
  1522. :param pika.frame.Method method_frame: The frame received
  1523. """
  1524. self._set_connection_state(self.CONNECTION_TUNE)
  1525. # Get our max channels, frames and heartbeat interval
  1526. self.params.channel_max = Connection._negotiate_integer_value(
  1527. self.params.channel_max, method_frame.method.channel_max)
  1528. self.params.frame_max = Connection._negotiate_integer_value(
  1529. self.params.frame_max, method_frame.method.frame_max)
  1530. if callable(self.params.heartbeat):
  1531. ret_heartbeat = self.params.heartbeat(self,
  1532. method_frame.method.heartbeat)
  1533. if ret_heartbeat is None or callable(ret_heartbeat):
  1534. # Enforce callback-specific restrictions on callback's return value
  1535. raise TypeError('heartbeat callback must not return None '
  1536. 'or callable, but got %r' % (ret_heartbeat,))
  1537. # Leave it to hearbeat setter deal with the rest of the validation
  1538. self.params.heartbeat = ret_heartbeat
  1539. # Negotiate heatbeat timeout
  1540. self.params.heartbeat = self._tune_heartbeat_timeout(
  1541. client_value=self.params.heartbeat,
  1542. server_value=method_frame.method.heartbeat)
  1543. # Calculate the maximum pieces for body frames
  1544. self._body_max_length = self._get_body_frame_max_length()
  1545. # Create a new heartbeat checker if needed
  1546. self._heartbeat_checker = self._create_heartbeat_checker()
  1547. # Send the TuneOk response with what we've agreed upon
  1548. self._send_connection_tune_ok()
  1549. # Send the Connection.Open RPC call for the vhost
  1550. self._send_connection_open()
  1551. def _on_data_available(self, data_in):
  1552. """This is called by our Adapter, passing in the data from the socket.
  1553. As long as we have buffer try and map out frame data.
  1554. :param str data_in: The data that is available to read
  1555. """
  1556. self._frame_buffer += data_in
  1557. while self._frame_buffer:
  1558. consumed_count, frame_value = self._read_frame()
  1559. if not frame_value:
  1560. return
  1561. self._trim_frame_buffer(consumed_count)
  1562. self._process_frame(frame_value)
  1563. def _terminate_stream(self, error):
  1564. """Deactivate heartbeat instance if activated already, and initiate
  1565. termination of the stream (TCP) connection asynchronously.
  1566. When connection terminates, the appropriate user callback will be
  1567. invoked with the given error: "on open error" or "on connection closed".
  1568. :param Exception | None error: exception instance describing the reason
  1569. for termination; None for normal closing, such as upon receipt of
  1570. Connection.CloseOk.
  1571. """
  1572. assert isinstance(error, (type(None), Exception)), \
  1573. 'error arg is neither None nor instance of Exception: {!r}.'.format(
  1574. error)
  1575. if error is not None:
  1576. # Save the exception for user callback once the stream closes
  1577. self._error = error
  1578. else:
  1579. assert self._error is not None, (
  1580. '_terminate_stream() expected self._error to be set when '
  1581. 'passed None error arg.')
  1582. # So it won't mess with the stack
  1583. self._remove_heartbeat()
  1584. # Begin disconnection of stream or termination of connection workflow
  1585. self._adapter_disconnect_stream()
  1586. def _on_stream_terminated(self, error):
  1587. """Handle termination of stack (including TCP layer) or failure to
  1588. establish the stack. Notify registered ON_CONNECTION_ERROR or
  1589. ON_CONNECTION_CLOSED callbacks, depending on whether the connection
  1590. was opening or open.
  1591. :param Exception | None error: None means that the transport was aborted
  1592. internally and exception in `self._error` represents the cause.
  1593. Otherwise it's an exception object that describes the unexpected
  1594. loss of connection.
  1595. """
  1596. LOGGER.info(
  1597. 'AMQP stack terminated, failed to connect, or aborted: '
  1598. 'error-arg=%r; pending-error=%r', error, self._error)
  1599. if error is not None:
  1600. if self._error is not None:
  1601. LOGGER.debug(
  1602. '_on_stream_terminated(): overriding '
  1603. 'pending-error=%r with %r', self._error, error)
  1604. self._error = error
  1605. else:
  1606. assert self._error is not None, (
  1607. '_on_stream_terminated() expected self._error to be populated '
  1608. 'with reason for terminating stack.')
  1609. # Stop the heartbeat checker if it exists
  1610. self._remove_heartbeat()
  1611. # Remove connection management callbacks
  1612. self._remove_callbacks(0,
  1613. [spec.Connection.Close, spec.Connection.Start])
  1614. if self.params.blocked_connection_timeout is not None:
  1615. self._remove_callbacks(
  1616. 0, [spec.Connection.Blocked, spec.Connection.Unblocked])
  1617. if not self._opened and isinstance(self._error,
  1618. exceptions.StreamLostError):
  1619. # Heuristically deduce error based on connection state
  1620. if self.connection_state == self.CONNECTION_PROTOCOL:
  1621. LOGGER.error('Probably incompatible Protocol Versions')
  1622. self._error = exceptions.IncompatibleProtocolError(
  1623. repr(self._error))
  1624. elif self.connection_state == self.CONNECTION_START:
  1625. LOGGER.error(
  1626. 'Connection closed while authenticating indicating a '
  1627. 'probable authentication error')
  1628. self._error = exceptions.ProbableAuthenticationError(
  1629. repr(self._error))
  1630. elif self.connection_state == self.CONNECTION_TUNE:
  1631. LOGGER.error('Connection closed while tuning the connection '
  1632. 'indicating a probable permission error when '
  1633. 'accessing a virtual host')
  1634. self._error = exceptions.ProbableAccessDeniedError(
  1635. repr(self._error))
  1636. elif self.connection_state not in [
  1637. self.CONNECTION_OPEN, self.CONNECTION_CLOSED,
  1638. self.CONNECTION_CLOSING
  1639. ]:
  1640. LOGGER.warning('Unexpected connection state on disconnect: %i',
  1641. self.connection_state)
  1642. # Transition to closed state
  1643. self._set_connection_state(self.CONNECTION_CLOSED)
  1644. # Inform our channel proxies, if any are still around
  1645. for channel in dictkeys(self._channels):
  1646. if channel not in self._channels:
  1647. continue
  1648. # pylint: disable=W0212
  1649. self._channels[channel]._on_close_meta(self._error)
  1650. # Inform interested parties
  1651. if not self._opened:
  1652. LOGGER.info('Connection setup terminated due to %r', self._error)
  1653. self.callbacks.process(0, self.ON_CONNECTION_ERROR, self, self,
  1654. self._error)
  1655. else:
  1656. LOGGER.info('Stack terminated due to %r', self._error)
  1657. self.callbacks.process(0, self.ON_CONNECTION_CLOSED, self, self,
  1658. self._error)
  1659. # Reset connection properties
  1660. self._init_connection_state()
  1661. def _process_callbacks(self, frame_value):
  1662. """Process the callbacks for the frame if the frame is a method frame
  1663. and if it has any callbacks pending.
  1664. :param pika.frame.Method frame_value: The frame to process
  1665. :rtype: bool
  1666. """
  1667. if (self._is_method_frame(frame_value) and
  1668. self._has_pending_callbacks(frame_value)):
  1669. self.callbacks.process(
  1670. frame_value.channel_number, # Prefix
  1671. frame_value.method, # Key
  1672. self, # Caller
  1673. frame_value) # Args
  1674. return True
  1675. return False
  1676. def _process_frame(self, frame_value):
  1677. """Process an inbound frame from the socket.
  1678. :param pika.frame.Frame|pika.frame.Method frame_value: The frame to
  1679. process
  1680. """
  1681. # Will receive a frame type of -1 if protocol version mismatch
  1682. if frame_value.frame_type < 0:
  1683. return
  1684. # Keep track of how many frames have been read
  1685. self.frames_received += 1
  1686. # Process any callbacks, if True, exit method
  1687. if self._process_callbacks(frame_value):
  1688. return
  1689. # If a heartbeat is received, update the checker
  1690. if isinstance(frame_value, frame.Heartbeat):
  1691. if self._heartbeat_checker:
  1692. self._heartbeat_checker.received()
  1693. else:
  1694. LOGGER.warning('Received heartbeat frame without a heartbeat '
  1695. 'checker')
  1696. # If the frame has a channel number beyond the base channel, deliver it
  1697. elif frame_value.channel_number > 0:
  1698. self._deliver_frame_to_channel(frame_value)
  1699. def _read_frame(self):
  1700. """Try and read from the frame buffer and decode a frame.
  1701. :rtype tuple: (int, pika.frame.Frame)
  1702. """
  1703. return frame.decode_frame(self._frame_buffer)
  1704. def _remove_callbacks(self, channel_number, method_classes):
  1705. """Remove the callbacks for the specified channel number and list of
  1706. method frames.
  1707. :param int channel_number: The channel number to remove the callback on
  1708. :param sequence method_classes: The method classes (derived from
  1709. `pika.amqp_object.Method`) for the callbacks
  1710. """
  1711. for method_cls in method_classes:
  1712. self.callbacks.remove(str(channel_number), method_cls)
  1713. def _rpc(self,
  1714. channel_number,
  1715. method,
  1716. callback=None,
  1717. acceptable_replies=None):
  1718. """Make an RPC call for the given callback, channel number and method.
  1719. acceptable_replies lists out what responses we'll process from the
  1720. server with the specified callback.
  1721. :param int channel_number: The channel number for the RPC call
  1722. :param pika.amqp_object.Method method: The method frame to call
  1723. :param callable callback: The callback for the RPC response
  1724. :param list acceptable_replies: The replies this RPC call expects
  1725. """
  1726. # Validate that acceptable_replies is a list or None
  1727. if acceptable_replies and not isinstance(acceptable_replies, list):
  1728. raise TypeError('acceptable_replies should be list or None')
  1729. # Validate the callback is callable
  1730. if callback is not None:
  1731. validators.require_callback(callback)
  1732. for reply in acceptable_replies:
  1733. self.callbacks.add(channel_number, reply, callback)
  1734. # Send the rpc call to RabbitMQ
  1735. self._send_method(channel_number, method)
  1736. def _send_connection_close(self, reply_code, reply_text):
  1737. """Send a Connection.Close method frame.
  1738. :param int reply_code: The reason for the close
  1739. :param str reply_text: The text reason for the close
  1740. """
  1741. self._rpc(0, spec.Connection.Close(reply_code, reply_text, 0, 0),
  1742. self._on_connection_close_ok, [spec.Connection.CloseOk])
  1743. def _send_connection_open(self):
  1744. """Send a Connection.Open frame"""
  1745. self._rpc(0, spec.Connection.Open(
  1746. self.params.virtual_host, insist=True), self._on_connection_open_ok,
  1747. [spec.Connection.OpenOk])
  1748. def _send_connection_start_ok(self, authentication_type, response):
  1749. """Send a Connection.StartOk frame
  1750. :param str authentication_type: The auth type value
  1751. :param str response: The encoded value to send
  1752. """
  1753. self._send_method(
  1754. 0,
  1755. spec.Connection.StartOk(self._client_properties,
  1756. authentication_type, response,
  1757. self.params.locale))
  1758. def _send_connection_tune_ok(self):
  1759. """Send a Connection.TuneOk frame"""
  1760. self._send_method(
  1761. 0,
  1762. spec.Connection.TuneOk(self.params.channel_max,
  1763. self.params.frame_max,
  1764. self.params.heartbeat))
  1765. def _send_frame(self, frame_value):
  1766. """This appends the fully generated frame to send to the broker to the
  1767. output buffer which will be then sent via the connection adapter.
  1768. :param pika.frame.Frame|pika.frame.ProtocolHeader frame_value: The
  1769. frame to write
  1770. :raises: exceptions.ConnectionClosed
  1771. """
  1772. if self.is_closed:
  1773. LOGGER.error('Attempted to send frame when closed')
  1774. raise exceptions.ConnectionWrongStateError(
  1775. 'Attempted to send a frame on closed connection.')
  1776. marshaled_frame = frame_value.marshal()
  1777. self._output_marshaled_frames([marshaled_frame])
  1778. def _send_method(self, channel_number, method, content=None):
  1779. """Constructs a RPC method frame and then sends it to the broker.
  1780. :param int channel_number: The channel number for the frame
  1781. :param pika.amqp_object.Method method: The method to send
  1782. :param tuple content: If set, is a content frame, is tuple of
  1783. properties and body.
  1784. """
  1785. if content:
  1786. self._send_message(channel_number, method, content)
  1787. else:
  1788. self._send_frame(frame.Method(channel_number, method))
  1789. def _send_message(self, channel_number, method_frame, content):
  1790. """Publish a message.
  1791. :param int channel_number: The channel number for the frame
  1792. :param pika.object.Method method_frame: The method frame to send
  1793. :param tuple content: A content frame, which is tuple of properties and
  1794. body.
  1795. """
  1796. length = len(content[1])
  1797. marshaled_body_frames = []
  1798. # Note: we construct the Method, Header and Content objects, marshal them
  1799. # *then* output in case the marshaling operation throws an exception
  1800. frame_method = frame.Method(channel_number, method_frame)
  1801. frame_header = frame.Header(channel_number, length, content[0])
  1802. marshaled_body_frames.append(frame_method.marshal())
  1803. marshaled_body_frames.append(frame_header.marshal())
  1804. if content[1]:
  1805. chunks = int(math.ceil(float(length) / self._body_max_length))
  1806. for chunk in xrange(0, chunks):
  1807. start = chunk * self._body_max_length
  1808. end = start + self._body_max_length
  1809. if end > length:
  1810. end = length
  1811. frame_body = frame.Body(channel_number, content[1][start:end])
  1812. marshaled_body_frames.append(frame_body.marshal())
  1813. self._output_marshaled_frames(marshaled_body_frames)
  1814. def _set_connection_state(self, connection_state):
  1815. """Set the connection state.
  1816. :param int connection_state: The connection state to set
  1817. """
  1818. LOGGER.debug('New Connection state: %s (prev=%s)',
  1819. self._STATE_NAMES[connection_state],
  1820. self._STATE_NAMES[self.connection_state])
  1821. self.connection_state = connection_state
  1822. def _set_server_information(self, method_frame):
  1823. """Set the server properties and capabilities
  1824. :param spec.connection.Start method_frame: The Connection.Start frame
  1825. """
  1826. self.server_properties = method_frame.method.server_properties
  1827. self.server_capabilities = self.server_properties.get(
  1828. 'capabilities', dict())
  1829. if hasattr(self.server_properties, 'capabilities'):
  1830. del self.server_properties['capabilities']
  1831. def _trim_frame_buffer(self, byte_count):
  1832. """Trim the leading N bytes off the frame buffer and increment the
  1833. counter that keeps track of how many bytes have been read/used from the
  1834. socket.
  1835. :param int byte_count: The number of bytes consumed
  1836. """
  1837. self._frame_buffer = self._frame_buffer[byte_count:]
  1838. self.bytes_received += byte_count
  1839. def _output_marshaled_frames(self, marshaled_frames):
  1840. """Output list of marshaled frames to buffer and update stats
  1841. :param list marshaled_frames: A list of frames marshaled to bytes
  1842. """
  1843. for marshaled_frame in marshaled_frames:
  1844. self.bytes_sent += len(marshaled_frame)
  1845. self.frames_sent += 1
  1846. self._adapter_emit_data(marshaled_frame)