persistence.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 twisted.internet import defer
  21. from synapse.util.logutils import log_function
  22. logger = logging.getLogger(__name__)
  23. class TransactionActions(object):
  24. """ Defines persistence actions that relate to handling Transactions.
  25. """
  26. def __init__(self, datastore):
  27. self.store = datastore
  28. @log_function
  29. def have_responded(self, transaction):
  30. """ Have we already responded to a transaction with the same id and
  31. origin?
  32. Returns:
  33. Deferred: Results in `None` if we have not previously responded to
  34. this transaction or a 2-tuple of `(int, dict)` representing the
  35. response code and response body.
  36. """
  37. if not transaction.transaction_id:
  38. raise RuntimeError("Cannot persist a transaction with no "
  39. "transaction_id")
  40. return self.store.get_received_txn_response(
  41. transaction.transaction_id, transaction.origin
  42. )
  43. @log_function
  44. def set_response(self, transaction, code, response):
  45. """ Persist how we responded to a transaction.
  46. Returns:
  47. Deferred
  48. """
  49. if not transaction.transaction_id:
  50. raise RuntimeError("Cannot persist a transaction with no "
  51. "transaction_id")
  52. return self.store.set_received_txn_response(
  53. transaction.transaction_id,
  54. transaction.origin,
  55. code,
  56. response,
  57. )
  58. @defer.inlineCallbacks
  59. @log_function
  60. def prepare_to_send(self, transaction):
  61. """ Persists the `Transaction` we are about to send and works out the
  62. correct value for the `prev_ids` key.
  63. Returns:
  64. Deferred
  65. """
  66. transaction.prev_ids = yield self.store.prep_send_transaction(
  67. transaction.transaction_id,
  68. transaction.destination,
  69. transaction.origin_server_ts,
  70. )
  71. @log_function
  72. def delivered(self, transaction, response_code, response_dict):
  73. """ Marks the given `Transaction` as having been successfully
  74. delivered to the remote homeserver, and what the response was.
  75. Returns:
  76. Deferred
  77. """
  78. return self.store.delivered_txn(
  79. transaction.transaction_id,
  80. transaction.destination,
  81. response_code,
  82. response_dict,
  83. )