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.
 
 
 
 
 
 

65 lines
2.3 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2021 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """ This module contains all the persistence actions done by the federation
  16. package.
  17. These actions are mostly only used by the :py:mod:`.replication` module.
  18. """
  19. import logging
  20. from typing import Optional, Tuple
  21. from synapse.federation.units import Transaction
  22. from synapse.storage.databases.main import DataStore
  23. from synapse.types import JsonDict
  24. logger = logging.getLogger(__name__)
  25. class TransactionActions:
  26. """Defines persistence actions that relate to handling Transactions."""
  27. def __init__(self, datastore: DataStore):
  28. self.store = datastore
  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
  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. async def set_response(
  43. self, origin: str, transaction: Transaction, code: int, response: JsonDict
  44. ) -> None:
  45. """Persist how we responded to a transaction."""
  46. transaction_id = transaction.transaction_id
  47. if not transaction_id:
  48. raise RuntimeError("Cannot persist a transaction with no transaction_id")
  49. await self.store.set_received_txn_response(
  50. transaction_id, origin, code, response
  51. )