persistence.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  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 synapse.logging.utils import log_function
  21. logger = logging.getLogger(__name__)
  22. class TransactionActions(object):
  23. """ Defines persistence actions that relate to handling Transactions.
  24. """
  25. def __init__(self, datastore):
  26. self.store = datastore
  27. @log_function
  28. def have_responded(self, origin, transaction):
  29. """ Have we already responded to a transaction with the same id and
  30. origin?
  31. Returns:
  32. Deferred: Results in `None` if we have not previously responded to
  33. this transaction or a 2-tuple of `(int, dict)` representing the
  34. response code and response body.
  35. """
  36. if not transaction.transaction_id:
  37. raise RuntimeError("Cannot persist a transaction with no transaction_id")
  38. return self.store.get_received_txn_response(transaction.transaction_id, origin)
  39. @log_function
  40. def set_response(self, origin, transaction, code, response):
  41. """ Persist how we responded to a transaction.
  42. Returns:
  43. Deferred
  44. """
  45. if not transaction.transaction_id:
  46. raise RuntimeError("Cannot persist a transaction with no transaction_id")
  47. return self.store.set_received_txn_response(
  48. transaction.transaction_id, origin, code, response
  49. )