repid 2.2.2__tar.gz → 2.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. {repid-2.2.2 → repid-2.3.0}/PKG-INFO +2 -2
  2. repid-2.3.0/docs/advanced_user_guide/your_own_brokers.md +189 -0
  3. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/actors/execution.md +6 -6
  4. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/actors/routers.md +9 -0
  5. repid-2.3.0/docs/user_guide/workers/concurrency.md +212 -0
  6. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/workers/index.md +2 -2
  7. {repid-2.2.2 → repid-2.3.0}/pyproject.toml +1 -1
  8. repid-2.3.0/repid/_concurrency.py +185 -0
  9. repid-2.3.0/repid/_deadlines.py +128 -0
  10. repid-2.3.0/repid/_delivery.py +231 -0
  11. repid-2.3.0/repid/_intake.py +200 -0
  12. repid-2.3.0/repid/_runner.py +541 -0
  13. {repid-2.2.2 → repid-2.3.0}/repid/_worker.py +14 -6
  14. {repid-2.2.2 → repid-2.3.0}/repid/connections/__init__.py +10 -0
  15. repid-2.3.0/repid/connections/_submission.py +131 -0
  16. {repid-2.2.2 → repid-2.3.0}/repid/connections/abc.py +64 -6
  17. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/helpers.py +64 -41
  18. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/message_broker.py +25 -8
  19. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/links.py +12 -9
  20. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/managed.py +23 -42
  21. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/session.py +3 -12
  22. repid-2.3.0/repid/connections/amqp/subscriber.py +160 -0
  23. {repid-2.2.2 → repid-2.3.0}/repid/connections/in_memory/message_broker.py +34 -54
  24. repid-2.3.0/repid/connections/in_memory/utils.py +55 -0
  25. repid-2.3.0/repid/connections/kafka/message.py +170 -0
  26. {repid-2.2.2 → repid-2.3.0}/repid/connections/kafka/message_broker.py +34 -20
  27. {repid-2.2.2 → repid-2.3.0}/repid/connections/kafka/protocols.py +22 -9
  28. repid-2.3.0/repid/connections/kafka/subscriber.py +338 -0
  29. repid-2.3.0/repid/connections/nats/message_broker.py +660 -0
  30. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/message_broker.py +33 -15
  31. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/__init__.py +0 -1
  32. repid-2.3.0/repid/connections/pubsub/protocol/_helpers.py +24 -0
  33. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/received_message.py +75 -39
  34. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/subscriber.py +103 -114
  35. {repid-2.2.2 → repid-2.3.0}/repid/connections/redis/message_broker.py +473 -300
  36. {repid-2.2.2 → repid-2.3.0}/repid/connections/sqs/message.py +86 -57
  37. {repid-2.2.2 → repid-2.3.0}/repid/connections/sqs/message_broker.py +17 -12
  38. repid-2.3.0/repid/connections/sqs/subscriber.py +312 -0
  39. {repid-2.2.2 → repid-2.3.0}/repid/data/__init__.py +1 -0
  40. {repid-2.2.2 → repid-2.3.0}/repid/data/channel.py +7 -1
  41. repid-2.3.0/repid/data/concurrency.py +30 -0
  42. {repid-2.2.2 → repid-2.3.0}/repid/main.py +17 -3
  43. {repid-2.2.2 → repid-2.3.0}/repid/router.py +24 -3
  44. {repid-2.2.2 → repid-2.3.0}/repid/test_client.py +3 -7
  45. repid-2.3.0/tests/intake.py +94 -0
  46. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_default_flow.py +10 -4
  47. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_kafka_specific.py +23 -26
  48. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_nats_specific.py +147 -104
  49. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_rabbitmq_specific.py +23 -10
  50. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_redis_specific.py +144 -28
  51. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_sqs_specific.py +185 -432
  52. repid-2.3.0/tests/integration/test_worker.py +264 -0
  53. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_connection.py +31 -26
  54. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_links.py +57 -61
  55. repid-2.3.0/tests/unit/amqp/test_logging.py +21 -0
  56. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_managed_pools.py +29 -12
  57. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_message_broker.py +572 -206
  58. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_reconnect.py +0 -11
  59. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_session.py +142 -184
  60. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/utils.py +1 -0
  61. {repid-2.2.2 → repid-2.3.0}/tests/unit/in_memory/test_message_broker.py +245 -119
  62. repid-2.3.0/tests/unit/in_memory/test_utils.py +36 -0
  63. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_channel.py +35 -40
  64. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_credentials.py +36 -25
  65. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_received_message.py +115 -35
  66. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_subscriber.py +306 -207
  67. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_subscriber_execution.py +19 -56
  68. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/test_message_broker.py +195 -72
  69. {repid-2.2.2 → repid-2.3.0}/tests/unit/redis/test_message_broker.py +1027 -459
  70. {repid-2.2.2 → repid-2.3.0}/tests/unit/redis/test_redis_keep_alive.py +30 -12
  71. repid-2.3.0/tests/unit/sqs/test_message.py +360 -0
  72. repid-2.3.0/tests/unit/sqs/test_subscriber_concurrency.py +578 -0
  73. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_channel.py +12 -3
  74. repid-2.3.0/tests/unit/test_concurrency.py +1565 -0
  75. repid-2.3.0/tests/unit/test_deadlines.py +145 -0
  76. repid-2.3.0/tests/unit/test_intake.py +364 -0
  77. repid-2.3.0/tests/unit/test_kafka_connection.py +927 -0
  78. repid-2.3.0/tests/unit/test_nats_connection.py +781 -0
  79. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_runner.py +455 -157
  80. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_test_client.py +12 -0
  81. {repid-2.2.2 → repid-2.3.0}/uv.lock +188 -185
  82. repid-2.2.2/docs/advanced_user_guide/your_own_brokers.md +0 -128
  83. repid-2.2.2/docs/user_guide/workers/concurrency.md +0 -31
  84. repid-2.2.2/repid/_runner.py +0 -410
  85. repid-2.2.2/repid/connections/amqp/subscriber.py +0 -173
  86. repid-2.2.2/repid/connections/in_memory/utils.py +0 -22
  87. repid-2.2.2/repid/connections/kafka/message.py +0 -168
  88. repid-2.2.2/repid/connections/kafka/subscriber.py +0 -168
  89. repid-2.2.2/repid/connections/nats/message_broker.py +0 -497
  90. repid-2.2.2/repid/connections/pubsub/protocol/_helpers.py +0 -27
  91. repid-2.2.2/repid/connections/sqs/subscriber.py +0 -333
  92. repid-2.2.2/tests/integration/test_worker.py +0 -140
  93. repid-2.2.2/tests/unit/amqp/test_logging.py +0 -15
  94. repid-2.2.2/tests/unit/in_memory/test_utils.py +0 -14
  95. repid-2.2.2/tests/unit/sqs/test_message.py +0 -164
  96. repid-2.2.2/tests/unit/test_kafka_connection.py +0 -67
  97. repid-2.2.2/tests/unit/test_nats_connection.py +0 -96
  98. {repid-2.2.2 → repid-2.3.0}/.devcontainer/devcontainer.json +0 -0
  99. {repid-2.2.2 → repid-2.3.0}/.github/CODE_OF_CONDUCT.md +0 -0
  100. {repid-2.2.2 → repid-2.3.0}/.github/FUNDING.yml +0 -0
  101. {repid-2.2.2 → repid-2.3.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  102. {repid-2.2.2 → repid-2.3.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  103. {repid-2.2.2 → repid-2.3.0}/.github/ISSUE_TEMPLATE/other.md +0 -0
  104. {repid-2.2.2 → repid-2.3.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
  105. {repid-2.2.2 → repid-2.3.0}/.github/dependabot.yml +0 -0
  106. {repid-2.2.2 → repid-2.3.0}/.github/workflows/actions.lock +0 -0
  107. {repid-2.2.2 → repid-2.3.0}/.github/workflows/codeql.yml +0 -0
  108. {repid-2.2.2 → repid-2.3.0}/.github/workflows/dependency-review.yml +0 -0
  109. {repid-2.2.2 → repid-2.3.0}/.github/workflows/docs.yml +0 -0
  110. {repid-2.2.2 → repid-2.3.0}/.github/workflows/prek.yml +0 -0
  111. {repid-2.2.2 → repid-2.3.0}/.github/workflows/release.yaml +0 -0
  112. {repid-2.2.2 → repid-2.3.0}/.github/workflows/tests.yaml +0 -0
  113. {repid-2.2.2 → repid-2.3.0}/.github/workflows/typecheck.yml +0 -0
  114. {repid-2.2.2 → repid-2.3.0}/.github/workflows/workflow-lint.yml +0 -0
  115. {repid-2.2.2 → repid-2.3.0}/.gitignore +0 -0
  116. {repid-2.2.2 → repid-2.3.0}/.pre-commit-config.yaml +0 -0
  117. {repid-2.2.2 → repid-2.3.0}/LICENSE +0 -0
  118. {repid-2.2.2 → repid-2.3.0}/Makefile +0 -0
  119. {repid-2.2.2 → repid-2.3.0}/README.md +0 -0
  120. {repid-2.2.2 → repid-2.3.0}/SECURITY.md +0 -0
  121. {repid-2.2.2 → repid-2.3.0}/docs/advanced_user_guide/your_own_converter.md +0 -0
  122. {repid-2.2.2 → repid-2.3.0}/docs/advanced_user_guide/your_own_serializer.md +0 -0
  123. {repid-2.2.2 → repid-2.3.0}/docs/benchmarks.md +0 -0
  124. {repid-2.2.2 → repid-2.3.0}/docs/benchmarks_img/.gitignore +0 -0
  125. {repid-2.2.2 → repid-2.3.0}/docs/benchmarks_img/chart.svg +0 -0
  126. {repid-2.2.2 → repid-2.3.0}/docs/contributing.md +0 -0
  127. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/chaining_jobs.md +0 -0
  128. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/large_payloads.md +0 -0
  129. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/middlewares.md +0 -0
  130. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/opentelemetry_middleware.md +0 -0
  131. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/rabbitmq_retries.md +0 -0
  132. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/sentry_middleware.md +0 -0
  133. {repid-2.2.2 → repid-2.3.0}/docs/cookbook/testing.md +0 -0
  134. {repid-2.2.2 → repid-2.3.0}/docs/features.md +0 -0
  135. {repid-2.2.2 → repid-2.3.0}/docs/getting_started/glossary.md +0 -0
  136. {repid-2.2.2 → repid-2.3.0}/docs/getting_started/quickstart.md +0 -0
  137. {repid-2.2.2 → repid-2.3.0}/docs/getting_started/supported_brokers.md +0 -0
  138. {repid-2.2.2 → repid-2.3.0}/docs/index.md +0 -0
  139. {repid-2.2.2 → repid-2.3.0}/docs/integrations/asyncapi.md +0 -0
  140. {repid-2.2.2 → repid-2.3.0}/docs/integrations/fastapi_and_pydantic.md +0 -0
  141. {repid-2.2.2 → repid-2.3.0}/docs/overrides/partials/integrations/analytics/custom.html +0 -0
  142. {repid-2.2.2 → repid-2.3.0}/docs/playground.md +0 -0
  143. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/actors/confirmation_and_errors.md +0 -0
  144. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/actors/index.md +0 -0
  145. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/actors/parsing.md +0 -0
  146. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/actors/routing.md +0 -0
  147. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/dependency_injection.md +0 -0
  148. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/message_registry.md +0 -0
  149. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/messages.md +0 -0
  150. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/raw_message_and_eager_response.md +0 -0
  151. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/server_registry.md +0 -0
  152. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/server_specific_parameters.md +0 -0
  153. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/workers/built_in_servers.md +0 -0
  154. {repid-2.2.2 → repid-2.3.0}/docs/user_guide/workers/lifecycle.md +0 -0
  155. {repid-2.2.2 → repid-2.3.0}/repid/__init__.py +0 -0
  156. {repid-2.2.2 → repid-2.3.0}/repid/_utils/__init__.py +0 -0
  157. {repid-2.2.2 → repid-2.3.0}/repid/_utils/_typing.py +0 -0
  158. {repid-2.2.2 → repid-2.3.0}/repid/_utils/asyncify_.py +0 -0
  159. {repid-2.2.2 → repid-2.3.0}/repid/_utils/is_installed.py +0 -0
  160. {repid-2.2.2 → repid-2.3.0}/repid/_utils/json_encoder.py +0 -0
  161. {repid-2.2.2 → repid-2.3.0}/repid/_utils/not_set.py +0 -0
  162. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/__init__.py +0 -0
  163. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/generator.py +0 -0
  164. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/__init__.py +0 -0
  165. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/asyncapi.py +0 -0
  166. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/channels.py +0 -0
  167. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/__init__.py +0 -0
  168. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/bindings.py +0 -0
  169. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/correlation_id.py +0 -0
  170. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/external_docs.py +0 -0
  171. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/message.py +0 -0
  172. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/reference.py +0 -0
  173. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/common/tag.py +0 -0
  174. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/components.py +0 -0
  175. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/info.py +0 -0
  176. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/operations.py +0 -0
  177. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi/models/servers.py +0 -0
  178. {repid-2.2.2 → repid-2.3.0}/repid/asyncapi_server.py +0 -0
  179. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/__init__.py +0 -0
  180. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/LICENSE.txt +0 -0
  181. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/_decode.py +0 -0
  182. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/_encode.py +0 -0
  183. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/amqptypes.py +0 -0
  184. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/constants.py +0 -0
  185. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/endpoints.py +0 -0
  186. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/message.py +0 -0
  187. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/outcomes.py +0 -0
  188. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/_uamqp/performatives.py +0 -0
  189. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/__init__.py +0 -0
  190. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/connection.py +0 -0
  191. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/events.py +0 -0
  192. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/reconnect.py +0 -0
  193. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/states.py +0 -0
  194. {repid-2.2.2 → repid-2.3.0}/repid/connections/amqp/protocol/transport.py +0 -0
  195. {repid-2.2.2 → repid-2.3.0}/repid/connections/in_memory/__init__.py +0 -0
  196. {repid-2.2.2 → repid-2.3.0}/repid/connections/kafka/__init__.py +0 -0
  197. {repid-2.2.2 → repid-2.3.0}/repid/connections/nats/__init__.py +0 -0
  198. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/__init__.py +0 -0
  199. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/helpers.py +0 -0
  200. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/channel.py +0 -0
  201. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/client.py +0 -0
  202. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/control_batcher.py +0 -0
  203. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/credentials.py +0 -0
  204. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/proto.py +0 -0
  205. {repid-2.2.2 → repid-2.3.0}/repid/connections/pubsub/protocol/resilience.py +0 -0
  206. {repid-2.2.2 → repid-2.3.0}/repid/connections/redis/__init__.py +0 -0
  207. {repid-2.2.2 → repid-2.3.0}/repid/connections/sqs/__init__.py +0 -0
  208. {repid-2.2.2 → repid-2.3.0}/repid/connections/sqs/constants.py +0 -0
  209. {repid-2.2.2 → repid-2.3.0}/repid/converter.py +0 -0
  210. {repid-2.2.2 → repid-2.3.0}/repid/data/actor.py +0 -0
  211. {repid-2.2.2 → repid-2.3.0}/repid/data/contact.py +0 -0
  212. {repid-2.2.2 → repid-2.3.0}/repid/data/converter_input_schema.py +0 -0
  213. {repid-2.2.2 → repid-2.3.0}/repid/data/external_docs.py +0 -0
  214. {repid-2.2.2 → repid-2.3.0}/repid/data/license.py +0 -0
  215. {repid-2.2.2 → repid-2.3.0}/repid/data/message.py +0 -0
  216. {repid-2.2.2 → repid-2.3.0}/repid/data/message_schema.py +0 -0
  217. {repid-2.2.2 → repid-2.3.0}/repid/data/operation.py +0 -0
  218. {repid-2.2.2 → repid-2.3.0}/repid/data/runner_info.py +0 -0
  219. {repid-2.2.2 → repid-2.3.0}/repid/data/tag.py +0 -0
  220. {repid-2.2.2 → repid-2.3.0}/repid/dependencies/__init__.py +0 -0
  221. {repid-2.2.2 → repid-2.3.0}/repid/dependencies/_utils.py +0 -0
  222. {repid-2.2.2 → repid-2.3.0}/repid/dependencies/depends.py +0 -0
  223. {repid-2.2.2 → repid-2.3.0}/repid/dependencies/full_payload.py +0 -0
  224. {repid-2.2.2 → repid-2.3.0}/repid/dependencies/header_dependency.py +0 -0
  225. {repid-2.2.2 → repid-2.3.0}/repid/dependencies/message_dependency.py +0 -0
  226. {repid-2.2.2 → repid-2.3.0}/repid/health_check_server.py +0 -0
  227. {repid-2.2.2 → repid-2.3.0}/repid/logger.py +0 -0
  228. {repid-2.2.2 → repid-2.3.0}/repid/message_registry.py +0 -0
  229. {repid-2.2.2 → repid-2.3.0}/repid/middlewares.py +0 -0
  230. {repid-2.2.2 → repid-2.3.0}/repid/py.typed +0 -0
  231. {repid-2.2.2 → repid-2.3.0}/repid/serializer.py +0 -0
  232. {repid-2.2.2 → repid-2.3.0}/repid/server_registry.py +0 -0
  233. {repid-2.2.2 → repid-2.3.0}/tests/__init__.py +0 -0
  234. {repid-2.2.2 → repid-2.3.0}/tests/integration/__init__.py +0 -0
  235. {repid-2.2.2 → repid-2.3.0}/tests/integration/asyncapi/package-lock.json +0 -0
  236. {repid-2.2.2 → repid-2.3.0}/tests/integration/asyncapi/package.json +0 -0
  237. {repid-2.2.2 → repid-2.3.0}/tests/integration/brokers.py +0 -0
  238. {repid-2.2.2 → repid-2.3.0}/tests/integration/conftest.py +0 -0
  239. {repid-2.2.2 → repid-2.3.0}/tests/integration/pubsub_proto_helpers.py +0 -0
  240. {repid-2.2.2 → repid-2.3.0}/tests/integration/rabbitmq_definitions.json +0 -0
  241. {repid-2.2.2 → repid-2.3.0}/tests/integration/test_asyncapi_compatibility.py +0 -0
  242. {repid-2.2.2 → repid-2.3.0}/tests/unit/__init__.py +0 -0
  243. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/__init__.py +0 -0
  244. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/conftest.py +0 -0
  245. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_amqp_transport.py +0 -0
  246. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_encoding_decoding.py +0 -0
  247. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_event_emitter.py +0 -0
  248. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_frame_buffer.py +0 -0
  249. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_managed.py +0 -0
  250. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_message.py +0 -0
  251. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_metrics.py +0 -0
  252. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_state_machine.py +0 -0
  253. {repid-2.2.2 → repid-2.3.0}/tests/unit/amqp/test_uamqp_types.py +0 -0
  254. {repid-2.2.2 → repid-2.3.0}/tests/unit/conftest.py +0 -0
  255. {repid-2.2.2 → repid-2.3.0}/tests/unit/in_memory/__init__.py +0 -0
  256. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/__init__.py +0 -0
  257. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_client.py +0 -0
  258. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_control_batcher.py +0 -0
  259. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_proto.py +0 -0
  260. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/protocol/test_resilience.py +0 -0
  261. {repid-2.2.2 → repid-2.3.0}/tests/unit/pubsub/test_helpers.py +0 -0
  262. {repid-2.2.2 → repid-2.3.0}/tests/unit/redis/__init__.py +0 -0
  263. {repid-2.2.2 → repid-2.3.0}/tests/unit/redis/test_connection.py +0 -0
  264. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_asyncapi_generator.py +0 -0
  265. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_converter.py +0 -0
  266. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_dependencies.py +0 -0
  267. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_health_check_server.py +0 -0
  268. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_main.py +0 -0
  269. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_middleware.py +0 -0
  270. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_router.py +0 -0
  271. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_runner_manual_explicit.py +0 -0
  272. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_serializer.py +0 -0
  273. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_server_registry.py +0 -0
  274. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_utils.py +0 -0
  275. {repid-2.2.2 → repid-2.3.0}/tests/unit/test_worker.py +0 -0
  276. {repid-2.2.2 → repid-2.3.0}/zensical.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: repid
3
- Version: 2.2.2
3
+ Version: 2.3.0
4
4
  Summary: Repid is a simple, fast, and extensible async task queue framework, with built-in AsyncAPI 3.0 schema generation.
5
5
  Project-URL: documentation, https://repid.aleksul.space
6
6
  Project-URL: funding, https://github.com/sponsors/aleksul
@@ -33,7 +33,7 @@ Requires-Dist: grpcio<2.0.0,>=1.76.0; extra == 'pubsub'
33
33
  Provides-Extra: pydantic
34
34
  Requires-Dist: pydantic<3.0.0,>=2.0.0; extra == 'pydantic'
35
35
  Provides-Extra: redis
36
- Requires-Dist: redis<8.0.0,>=7.0.0; extra == 'redis'
36
+ Requires-Dist: redis<9.0.0,>=7.0.0; extra == 'redis'
37
37
  Provides-Extra: sqs
38
38
  Requires-Dist: aiobotocore<4.0.0,>=3.0.0; extra == 'sqs'
39
39
  Description-Content-Type: text/markdown
@@ -0,0 +1,189 @@
1
+ # Your own brokers
2
+
3
+ Repid's architecture allows you to easily plug in your own message brokers.
4
+ To create a custom broker, you need to implement a class that adheres to the `ServerT` protocol,
5
+ defined in `repid.connections.abc`.
6
+
7
+ ## The `ServerT` Protocol
8
+
9
+ At its core, a server (broker implementation) must handle the connection lifecycle,
10
+ message publishing, and message consumption (subscribing).
11
+
12
+ Here is a simplified overview of what you need to implement:
13
+
14
+ ```python
15
+ import asyncio
16
+ from contextlib import AbstractAsyncContextManager
17
+ from typing import Mapping, Sequence, Any
18
+ from repid.connections.abc import (
19
+ ServerT,
20
+ SubscriberT,
21
+ SentMessageT,
22
+ Intake,
23
+ CapabilitiesT,
24
+ )
25
+
26
+
27
+ class MyCustomServer:
28
+ # 1. Server Metadata Properties for AsyncAPI
29
+ @property
30
+ def host(self) -> str:
31
+ return "my-broker-host"
32
+
33
+ @property
34
+ def protocol(self) -> str:
35
+ return "my-custom-protocol"
36
+
37
+ # (other properties like title, summary, tags, variables, etc. can return None or empty defaults)
38
+
39
+ # 2. Capabilities
40
+ @property
41
+ def capabilities(self) -> CapabilitiesT:
42
+ return {
43
+ "supports_native_reply": False,
44
+ "supports_lightweight_pause": False,
45
+ "supports_keep_alive": False,
46
+ }
47
+
48
+ # 3. Connection Lifecycle
49
+ @property
50
+ def is_connected(self) -> bool:
51
+ # Return True if the connection to the broker is active
52
+ ...
53
+
54
+ async def connect(self) -> None:
55
+ # Establish the connection
56
+ ...
57
+
58
+ async def disconnect(self) -> None:
59
+ # Teardown the connection
60
+ ...
61
+
62
+ def connection(self) -> AbstractAsyncContextManager[ServerT]:
63
+ # Return an async context manager for the connection
64
+ ...
65
+
66
+ # 4. Message Publishing
67
+ async def publish(
68
+ self,
69
+ *,
70
+ channel: str,
71
+ message: SentMessageT,
72
+ server_specific_parameters: dict[str, Any] | None = None,
73
+ ) -> None:
74
+ # Send the payload to the broker on the specified channel
75
+ ...
76
+
77
+ # 5. Message Consumption
78
+ async def subscribe(self, *, intake: Intake) -> SubscriberT:
79
+ # Start consuming from the channels in `intake.channels`, handing each delivery
80
+ # to its channel's intake. Return an object that implements `SubscriberT`
81
+ # (can manage pauses, resumes, closures).
82
+ ...
83
+ ```
84
+
85
+ ### Intake
86
+
87
+ `subscribe` receives the worker's `Intake`. It tells your subscriber how much work the worker
88
+ can take. `intake.channels` maps each channel to consume to a `ChannelIntake`.
89
+
90
+ Both the intake and each channel intake have three properties:
91
+
92
+ - `limits`: the most messages and payload bytes the worker runs at once. Copy it into native
93
+ settings such as link credit or unacknowledged-message limits.
94
+ - `demand`: the free capacity right now, as `Demand(messages=..., payload_bytes=...)`. `None`
95
+ means unlimited. Use it to size fetches.
96
+ - `ready`: a future that is done while there is room for another message.
97
+
98
+ A channel's values already include the worker's limit. A broker that fetches for all channels
99
+ at once can size that fetch from `intake` itself.
100
+
101
+ ### Submitting messages
102
+
103
+ Hand each received message to its channel with `channel.submit(message)`. The call is
104
+ synchronous, and from then on the worker owns the message: it runs the actor and settles the
105
+ message. Submit only while `ready` is done.
106
+
107
+ A pull-based reader looks like this:
108
+
109
+ ```python
110
+ async def read(channel: ChannelIntake) -> None:
111
+ while True:
112
+ ready = channel.ready
113
+ if not ready.done():
114
+ await ready
115
+ continue
116
+ room = channel.demand.messages
117
+ for message in await fetch(max_messages=room or DEFAULT_BATCH):
118
+ channel.submit(message)
119
+ ```
120
+
121
+ A push-based broker sets its window from `channel.limits` and submits messages as they arrive.
122
+
123
+ ### Prefetching (optional)
124
+
125
+ Your subscriber can fetch a few messages beyond `demand` and submit them as soon as `ready`
126
+ completes, so freed slots fill without a broker round trip. Make the amount a server constructor
127
+ argument such as `prefetch`. Until you submit them, held messages are your subscriber's: renew
128
+ their leases, and return them to the broker in `close()`.
129
+
130
+ ### Keep-alive (optional)
131
+
132
+ If your broker uses leases, set `supports_keep_alive` and implement `keep_alive()`. Repid calls
133
+ it every `keep_alive_interval` seconds until it's done with the message. Return `False` once
134
+ renewal is pointless (the message is settled or its lease expired), otherwise `None`.
135
+
136
+ Repid starts renewing when you submit the message. To renew a message your subscriber holds
137
+ before that, use `repid.LeaseRenewal` and submit inside `handoff()`, so the worker takes over the
138
+ renewal:
139
+
140
+ ```python
141
+ renewal = LeaseRenewal(message)
142
+ renewal.renew_at(message.keep_alive_interval)
143
+ ...
144
+ with renewal.handoff():
145
+ channel.submit(message)
146
+ renewal.stop_if_unclaimed()
147
+ ```
148
+
149
+ ## Creating a Subscriber
150
+
151
+ `subscribe` returns an object compatible with the `SubscriberT` protocol. It represents the
152
+ active listening loop and must have these members:
153
+
154
+ ```python
155
+ class MyCustomSubscriber:
156
+ @property
157
+ def is_active(self) -> bool:
158
+ # True if actively consuming
159
+ ...
160
+
161
+ @property
162
+ def task(self) -> asyncio.Task:
163
+ # The background asyncio Task running the consumer loop
164
+ ...
165
+
166
+ async def pause(self) -> None:
167
+ # Pause consumption temporarily
168
+ ...
169
+
170
+ async def resume(self) -> None:
171
+ # Resume consumption
172
+ ...
173
+
174
+ async def close(self) -> None:
175
+ # Shut down the subscriber
176
+ ...
177
+ ```
178
+
179
+ ## Received Messages
180
+
181
+ The messages you submit must be
182
+ instances compatible with `ReceivedMessageT` protocol. These objects wrap the payload, headers,
183
+ reply metadata (`reply_to`), and methods to act on a message (`ack`, `nack`, `reject`, `reply`).
184
+
185
+ If your broker does not provide native request/reply
186
+ semantics, implement `reply(...)` to raise `NotImplementedError`.
187
+
188
+ By implementing these protocols, your custom broker will integrate natively with the rest
189
+ of Repid's architecture, including routers, workers, and middlewares.
@@ -24,20 +24,20 @@ async def fetch_slow_api():
24
24
  ## Keep-alive
25
25
 
26
26
  For long-running actors, Repid automatically tells the broker that the message
27
- is still being worked on (assuming the broker supports it). This keeps the message from
28
- timing out and being redelivered while it's still being processed.
27
+ is still being worked on (if the broker supports it), so it isn't redelivered while it is still
28
+ being processed.
29
29
 
30
- By default, Repid uses the broker's recommended interval for sending keep-alive signals.
31
- You can configure or disable this behavior per-actor:
30
+ Repid uses the broker's recommended interval by default. You can change it or turn it off per
31
+ actor:
32
32
 
33
33
  ```python
34
- # Disable keep-alive entirely for this actor
34
+ # Disable keep-alive for this actor
35
35
  @router.actor(keep_alive=False)
36
36
  async def my_actor():
37
37
  pass
38
38
 
39
39
 
40
- # Override the keep-alive interval to ping the broker every 15 seconds
40
+ # Send a keep-alive every 15 seconds
41
41
  @router.actor(keep_alive=15.0)
42
42
  async def my_long_running_actor():
43
43
  pass
@@ -67,3 +67,12 @@ app.include_router(parent_router)
67
67
  When dealing with middlewares during nesting, they are **concatenated**,
68
68
  not overwritten. The parent router's middlewares will run *before*
69
69
  the child router's middlewares!
70
+
71
+ ## Router Materialization
72
+
73
+ Repid stores included routers and actor definitions lazily.
74
+ Therefore, including the same router in the same parent multiple times makes no difference.
75
+
76
+ For execution, the routers and actors are materialized when creating a worker or a `TestClient`.
77
+ Thus, any changes made afterward have no effect -
78
+ if necessary, create a new one to materialize changed routers and actors.
@@ -0,0 +1,212 @@
1
+ # Concurrency Limits
2
+
3
+ Concurrency limits keep a worker from taking more work than it can handle.
4
+ For example, you can use them to protect memory, database pools, external services, etc.
5
+
6
+ Configure message count and payload bytes with `Concurrency`:
7
+
8
+ ```python
9
+ from repid import Concurrency
10
+
11
+ await app.run_worker(
12
+ concurrency=Concurrency(
13
+ messages=200,
14
+ payload_bytes=64 * 1024**2,
15
+ ),
16
+ )
17
+ ```
18
+
19
+ ## Default worker limit
20
+
21
+ A worker admits up to 1,000 messages by default:
22
+
23
+ ```python
24
+ await app.run_worker() # same as Concurrency(messages=1000)
25
+ ```
26
+
27
+ To remove this limit, pass an empty `Concurrency` object:
28
+
29
+ ```python
30
+ await app.run_worker(concurrency=Concurrency())
31
+ ```
32
+
33
+ There is no default payload byte cap.
34
+
35
+ Any `Concurrency` you pass replaces the default as a whole, and a field you leave out is
36
+ unlimited. A worker configured with only `payload_bytes` therefore has no message limit, and
37
+ small or empty payloads can still start an unbounded number of actors. Set both fields to
38
+ bound both:
39
+
40
+ ```python
41
+ await app.run_worker(
42
+ concurrency=Concurrency(messages=1000, payload_bytes=64 * 1024**2),
43
+ )
44
+ ```
45
+
46
+ The `tasks_limit` worker argument is deprecated. Use `concurrency=Concurrency(messages=...)`
47
+ to set the same limit.
48
+
49
+ Each field accepts a positive integer or `None` for unlimited.
50
+
51
+ Routers and actors have no default limits.
52
+
53
+ ??? Note "Why is the default 1,000 messages?"
54
+ In tests, the processing overhead of receiving and scheduling about 1,000 to 2,000 no-op
55
+ messages can saturate one CPU core. This number is based on Repid's
56
+ message-consumption overhead, does not include any work done by your actors.
57
+
58
+ When a worker takes more messages than its CPU can process, it can also take work away from
59
+ less busy workers, thus the whole system becomes less efficient.
60
+ The best value depends on your CPU, broker, and actors.
61
+ Measure your workload and adjust the limit.
62
+
63
+ ## Channel limits
64
+
65
+ Declare an additional budget on a channel:
66
+
67
+ ```python
68
+ from repid import Channel, Concurrency, Router
69
+
70
+ images = Channel(
71
+ address="images",
72
+ concurrency=Concurrency(
73
+ messages=8,
74
+ payload_bytes=32 * 1024**2,
75
+ ),
76
+ )
77
+ router = Router(channel=images)
78
+
79
+ await app.run_worker(
80
+ concurrency=Concurrency(
81
+ messages=100,
82
+ payload_bytes=128 * 1024**2,
83
+ ),
84
+ )
85
+ ```
86
+
87
+ The worker limit applies to all channels combined. A message counts toward the worker limit and
88
+ any limit set for its channel. All channels draw from the same pool of worker capacity.
89
+
90
+ === "With a channel limit"
91
+
92
+ The worker waits until both budgets can admit the message.
93
+
94
+ ```mermaid
95
+ flowchart TD
96
+ routed["Message routed to a channel"] --> worker{"Room in the shared worker budget?"}
97
+ worker -->|Yes| channel{"Room in the images channel budget?"}
98
+ worker -->|No| wait["Wait for capacity"]
99
+ channel -->|No| wait
100
+ wait -->|Retry| worker
101
+ channel -->|Yes| reserve["Reserve worker and channel capacity together"]
102
+ reserve --> process["Process the message"]
103
+ ```
104
+
105
+ === "Without a channel limit"
106
+
107
+ The worker checks only its own budget.
108
+
109
+ ```mermaid
110
+ flowchart TD
111
+ routed["Message routed to a channel"] --> capacity{"Room in the worker budget?"}
112
+ capacity -->|Yes| process["Reserve worker capacity and process the message"]
113
+ capacity -->|No| wait["Wait for capacity"]
114
+ wait --> capacity
115
+ ```
116
+
117
+ ### Sharing a channel limit across actors
118
+
119
+ Both actors below use the same `images` address, so they share one 8-message limit.
120
+
121
+ ```python
122
+ from repid import Channel, Concurrency, Router
123
+
124
+ router = Router()
125
+
126
+
127
+ @router.actor(
128
+ channel=Channel(
129
+ address="images",
130
+ concurrency=Concurrency(messages=8),
131
+ ),
132
+ )
133
+ async def resize_image() -> None: ...
134
+
135
+
136
+ @router.actor(
137
+ channel=Channel(
138
+ address="images",
139
+ concurrency=Concurrency(messages=8),
140
+ ),
141
+ )
142
+ async def make_thumbnail() -> None: ...
143
+ ```
144
+
145
+ If 6 messages are running in `resize_image` and 2 in `make_thumbnail`, the next `images`
146
+ message waits for a slot.
147
+
148
+ If you explicitly set concurrency limits for the same channel address more than once,
149
+ the settings must match exactly. Repid will raise an error when it materializes the router
150
+ if the settings differ.
151
+ A limit declared on a router's default channel also applies when actors in other included
152
+ routers use the same address.
153
+
154
+ You can also declare the limit once and reference the channel by its address:
155
+
156
+ ```python
157
+ from repid import Channel, Concurrency, Router
158
+
159
+ router = Router()
160
+
161
+
162
+ @router.actor(
163
+ channel=Channel(
164
+ address="images",
165
+ concurrency=Concurrency(messages=8),
166
+ ),
167
+ )
168
+ async def resize_image() -> None: ...
169
+
170
+
171
+ @router.actor(channel="images") # (1)
172
+ async def make_thumbnail() -> None: ...
173
+ ```
174
+
175
+ 1. Implicitly references earlier declaration of the channel with Concurrency of 8 messages.
176
+
177
+ ## What counts toward a limit
178
+
179
+ - `messages` counts the messages the worker is processing.
180
+ - `payload_bytes` adds up the size of their serialized payloads.
181
+
182
+ A message counts from the moment it starts until the worker has finished with it, including the
183
+ final acknowledgment. A message larger than a byte limit runs alone once that limit is free.
184
+
185
+ ## Prefetch
186
+
187
+ Subscribers fetch only as many messages as the worker has room for. With `prefetch=N`, they
188
+ fetch up to N extra messages per channel and keep them ready, so a slot freed by a finished actor
189
+ fills without waiting for the broker.
190
+
191
+ ```python
192
+ from repid.connections import RedisServer
193
+
194
+ server = RedisServer("redis://localhost:6379/0", prefetch=10)
195
+ ```
196
+
197
+ | Broker | `prefetch` default |
198
+ | --- | --- |
199
+ | Redis Streams | `batch_size`, up to the channel's message limit |
200
+ | SQS | `batch_size`, up to the channel's message limit |
201
+ | AMQP 1.0, Google Pub/Sub, Kafka, NATS JetStream | `0` |
202
+ | In-memory | not available |
203
+
204
+ Prefetched messages are reserved for this worker and don't count toward `payload_bytes`. Keep
205
+ `prefetch` small when actors are slow or many workers share a queue, and raise it when actors are
206
+ fast and broker round trips are the bottleneck.
207
+
208
+ ### NATS JetStream
209
+
210
+ Repid sets the consumer's `max_ack_pending` to the channel's message limit plus `prefetch`, but
211
+ only when it creates the consumer. All workers share that window, and Repid never changes an
212
+ existing consumer. To change it later, update the consumer on the NATS server.
@@ -44,8 +44,8 @@ if __name__ == "__main__":
44
44
  In distributed orchestrated systems (like Kubernetes or Docker Swarm), it is a common practice to
45
45
  periodically restart worker processes to prevent long-term memory leaks or zombie connections.
46
46
 
47
- You can configure the worker to gracefully exit after processing a certain number of messages (by
48
- default, it runs indefinitely):
47
+ You can configure the worker to gracefully exit after processing a certain number of messages.
48
+ The default, `messages_limit=None`, lets the worker run indefinitely:
49
49
 
50
50
  ```python
51
51
  # Worker will exit successfully after processing exactly 1000 messages
@@ -58,7 +58,7 @@ pubsub = [
58
58
  "grpcio>=1.76.0,<2.0.0"
59
59
  ]
60
60
  pydantic = ["pydantic>=2.0.0,<3.0.0"]
61
- redis = ["redis>=7.0.0,<8.0.0"]
61
+ redis = ["redis>=7.0.0,<9.0.0"]
62
62
  sqs = ["aiobotocore>=3.0.0,<4.0.0"]
63
63
 
64
64
  [project.urls]
@@ -0,0 +1,185 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ from collections import OrderedDict
5
+ from collections.abc import Mapping
6
+ from math import inf
7
+
8
+ from repid.data.concurrency import Concurrency
9
+
10
+ _DEFAULT_WORKER_CONCURRENCY = Concurrency(messages=1000)
11
+
12
+
13
+ def _tighter_limit(worker_limit: int | None, channel_limit: int | None) -> int | None:
14
+ if worker_limit is None:
15
+ return channel_limit
16
+ if channel_limit is None:
17
+ return worker_limit
18
+ return min(worker_limit, channel_limit)
19
+
20
+
21
+ class _Scope:
22
+ __slots__ = ("byte_limit", "limits", "message_limit", "messages", "payload_bytes")
23
+
24
+ def __init__(self, limits: Concurrency) -> None:
25
+ self.limits = limits
26
+ self.message_limit = inf if limits.messages is None else limits.messages
27
+ self.byte_limit = inf if limits.payload_bytes is None else limits.payload_bytes
28
+ self.messages = 0
29
+ self.payload_bytes = 0
30
+
31
+ def fits(self, size: int) -> bool:
32
+ # An empty scope can admit a single oversized delivery.
33
+ return self.messages < self.message_limit and (
34
+ self.messages == 0 or self.payload_bytes + size <= self.byte_limit
35
+ )
36
+
37
+ def charge(self, size: int) -> None:
38
+ self.messages += 1
39
+ self.payload_bytes += size
40
+
41
+ def release(self, size: int) -> None:
42
+ self.messages -= 1
43
+ self.payload_bytes -= size
44
+
45
+
46
+ _Waiter = asyncio.Future[bool]
47
+ _WaitQueue = OrderedDict[_Waiter, int]
48
+
49
+
50
+ class _Budget:
51
+ """Atomically check and charge worker and channel budgets on the worker's event loop."""
52
+
53
+ __slots__ = (
54
+ "_candidates",
55
+ "_waiters",
56
+ "channels",
57
+ "closed",
58
+ "worker",
59
+ )
60
+
61
+ def __init__(
62
+ self,
63
+ worker: Concurrency,
64
+ channels: Mapping[str, Concurrency] | None = None,
65
+ ) -> None:
66
+ self.worker = _Scope(worker)
67
+ # Only channels with their own limit get a scope; others share the worker's.
68
+ self.channels: dict[str, _Scope] = {
69
+ name: _Scope(limits)
70
+ for name, limits in (channels or {}).items()
71
+ if limits.messages is not None or limits.payload_bytes is not None
72
+ }
73
+ self.closed = False
74
+ self._waiters: dict[str, _WaitQueue] = {}
75
+ # Channels rotate here until their head hits a local limit. The waiting
76
+ # map retains parked queues until a release or head change reactivates them.
77
+ self._candidates: OrderedDict[str, _WaitQueue] = OrderedDict()
78
+
79
+ def limits(self, channel: str) -> Concurrency:
80
+ """Return the tighter of the worker and channel limits for a channel."""
81
+ worker = self.worker.limits
82
+ if (scope := self.channels.get(channel)) is None:
83
+ return worker
84
+ return Concurrency(
85
+ messages=_tighter_limit(worker.messages, scope.limits.messages),
86
+ payload_bytes=_tighter_limit(worker.payload_bytes, scope.limits.payload_bytes),
87
+ )
88
+
89
+ def close(self) -> None:
90
+ self.closed = True
91
+ for queue in self._waiters.values():
92
+ for waiter in queue:
93
+ if not waiter.done():
94
+ waiter.set_result(False)
95
+ self._waiters.clear()
96
+ self._candidates.clear()
97
+
98
+ def try_acquire(self, channel: str, size: int) -> bool:
99
+ if self.closed or (self._waiters and (self._candidates or channel in self._waiters)):
100
+ return False
101
+ scope = self.channels.get(channel)
102
+ if not self.worker.fits(size) or (scope is not None and not scope.fits(size)):
103
+ return False
104
+ self._charge(channel, size)
105
+ return True
106
+
107
+ async def acquire(self, channel: str, size: int) -> bool:
108
+ if self.try_acquire(channel, size) or self.closed:
109
+ return not self.closed
110
+
111
+ waiter: _Waiter = asyncio.get_running_loop().create_future()
112
+ queue = self._waiters.get(channel)
113
+ if queue is None:
114
+ self._waiters[channel] = queue = OrderedDict(((waiter, size),))
115
+ self._candidates[channel] = queue
116
+ self._admit_waiters()
117
+ else:
118
+ queue[waiter] = size
119
+ try:
120
+ granted = await waiter
121
+ except asyncio.CancelledError:
122
+ if waiter.cancelled():
123
+ if self._waiters.get(channel) is queue:
124
+ was_head = next(iter(queue)) is waiter
125
+ queue.pop(waiter, None)
126
+ if was_head:
127
+ self._head_changed(channel, queue)
128
+ self._admit_waiters()
129
+ elif waiter.result():
130
+ self.release(channel, size)
131
+ raise
132
+ if self.closed and granted:
133
+ self.release(channel, size)
134
+ return False
135
+ return granted
136
+
137
+ def release(self, channel: str, size: int) -> None:
138
+ self.worker.release(size)
139
+ if (scope := self.channels.get(channel)) is not None:
140
+ scope.release(size)
141
+ if self._waiters:
142
+ if (queue := self._waiters.get(channel)) is not None:
143
+ self._candidates[channel] = queue
144
+ self._admit_waiters()
145
+
146
+ def _charge(self, channel: str, size: int) -> None:
147
+ self.worker.charge(size)
148
+ if (scope := self.channels.get(channel)) is not None:
149
+ scope.charge(size)
150
+
151
+ def _head_changed(self, channel: str, queue: _WaitQueue) -> None:
152
+ if queue:
153
+ self._candidates[channel] = queue
154
+ else:
155
+ del self._waiters[channel]
156
+ self._candidates.pop(channel, None)
157
+
158
+ def _admit_waiters(self) -> None:
159
+ worker = self.worker
160
+ candidates = self._candidates
161
+ while candidates and worker.fits(0):
162
+ channel, queue = next(iter(candidates.items()))
163
+ waiter, size = next(iter(queue.items()))
164
+ if waiter.cancelled():
165
+ queue.popitem(last=False)
166
+ self._head_changed(channel, queue)
167
+ continue
168
+
169
+ scope = self.channels.get(channel)
170
+ if scope is not None and not scope.fits(size):
171
+ # A local limit can only change when this channel releases capacity.
172
+ del candidates[channel]
173
+ continue
174
+ if not worker.fits(size):
175
+ # Let the shared budget drain for the next eligible channel.
176
+ return
177
+
178
+ self._charge(channel, size)
179
+ queue.popitem(last=False)
180
+ if queue:
181
+ candidates.move_to_end(channel)
182
+ else:
183
+ del self._waiters[channel]
184
+ del candidates[channel]
185
+ waiter.set_result(True)