You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

66 lines
2.3 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ This module contains all the persistence actions done by the federation
  15. package.
  16. These actions are mostly only used by the :py:mod:`.replication` module.
  17. """
  18. import logging
  19. from typing import Optional, Tuple
  20. from synapse.federation.units import Transaction
  21. from synapse.logging.utils import log_function
  22. from synapse.types import JsonDict
  23. logger = logging.getLogger(__name__)
  24. class TransactionActions:
  25. """Defines persistence actions that relate to handling Transactions."""
  26. def __init__(self, datastore):
  27. self.store = datastore
  28. @log_function
  29. async def have_responded(
  30. self, origin: str, transaction: Transaction
  31. ) -> Optional[Tuple[int, JsonDict]]:
  32. """Have we already responded to a transaction with the same id and
  33. origin?
  34. Returns:
  35. `None` if we have not previously responded to this transaction or a
  36. 2-tuple of `(int, dict)` representing the response code and response body.
  37. """
  38. transaction_id = transaction.transaction_id # type: ignore
  39. if not transaction_id:
  40. raise RuntimeError("Cannot persist a transaction with no transaction_id")
  41. return await self.store.get_received_txn_response(transaction_id, origin)
  42. @log_function
  43. async def set_response(
  44. self, origin: str, transaction: Transaction, code: int, response: JsonDict
  45. ) -> None:
  46. """Persist how we responded to a transaction."""
  47. transaction_id = transaction.transaction_id # type: ignore
  48. if not transaction_id:
  49. raise RuntimeError("Cannot persist a transaction with no transaction_id")
  50. await self.store.set_received_txn_response(
  51. transaction_id, origin, code, response
  52. )