diff --git a/synapse/rest/client/v1/base.py b/synapse/rest/client/v1/base.py
index c2a8447860..c7aa0bbf59 100644
--- a/synapse/rest/client/v1/base.py
+++ b/synapse/rest/client/v1/base.py
@@ -18,7 +18,8 @@
from synapse.http.servlet import RestServlet
from synapse.api.urls import CLIENT_PREFIX
-from .transactions import HttpTransactionStore
+from synapse.rest.client.transactions import HttpTransactionCache
+
import re
import logging
@@ -59,4 +60,4 @@ class ClientV1RestServlet(RestServlet):
self.hs = hs
self.builder_factory = hs.get_event_builder_factory()
self.auth = hs.get_v1auth()
- self.txns = HttpTransactionStore()
+ self.txns = HttpTransactionCache(hs.get_clock())
diff --git a/synapse/rest/client/v1/room.py b/synapse/rest/client/v1/room.py
index 010fbc7c32..3fb1f2deb3 100644
--- a/synapse/rest/client/v1/room.py
+++ b/synapse/rest/client/v1/room.py
@@ -53,19 +53,10 @@ class RoomCreateRestServlet(ClientV1RestServlet):
client_path_patterns("/createRoom(?:/.*)?$"),
self.on_OPTIONS)
- @defer.inlineCallbacks
def on_PUT(self, request, txn_id):
- try:
- defer.returnValue(
- self.txns.get_client_transaction(request, txn_id)
- )
- except KeyError:
- pass
-
- response = yield self.on_POST(request)
-
- self.txns.store_client_transaction(request, txn_id, response)
- defer.returnValue(response)
+ return self.txns.fetch_or_execute_request(
+ request, self.on_POST, request
+ )
@defer.inlineCallbacks
def on_POST(self, request):
@@ -214,19 +205,10 @@ class RoomSendEventRestServlet(ClientV1RestServlet):
def on_GET(self, request, room_id, event_type, txn_id):
return (200, "Not implemented")
- @defer.inlineCallbacks
def on_PUT(self, request, room_id, event_type, txn_id):
- try:
- defer.returnValue(
- self.txns.get_client_transaction(request, txn_id)
- )
- except KeyError:
- pass
-
- response = yield self.on_POST(request, room_id, event_type, txn_id)
-
- self.txns.store_client_transaction(request, txn_id, response)
- defer.returnValue(response)
+ return self.txns.fetch_or_execute_request(
+ request, self.on_POST, request, room_id, event_type, txn_id
+ )
# TODO: Needs unit testing for room ID + alias joins
@@ -283,19 +265,10 @@ class JoinRoomAliasServlet(ClientV1RestServlet):
defer.returnValue((200, {"room_id": room_id}))
- @defer.inlineCallbacks
def on_PUT(self, request, room_identifier, txn_id):
- try:
- defer.returnValue(
- self.txns.get_client_transaction(request, txn_id)
- )
- except KeyError:
- pass
-
- response = yield self.on_POST(request, room_identifier, txn_id)
-
- self.txns.store_client_transaction(request, txn_id, response)
- defer.returnValue(response)
+ return self.txns.fetch_or_execute_request(
+ request, self.on_POST, request, room_identifier, txn_id
+ )
# TODO: Needs unit testing
@@ -537,22 +510,11 @@ class RoomForgetRestServlet(ClientV1RestServlet):
defer.returnValue((200, {}))
- @defer.inlineCallbacks
def on_PUT(self, request, room_id, txn_id):
- try:
- defer.returnValue(
- self.txns.get_client_transaction(request, txn_id)
- )
- except KeyError:
- pass
-
- response = yield self.on_POST(
- request, room_id, txn_id
+ return self.txns.fetch_or_execute_request(
+ request, self.on_POST, request, room_id, txn_id
)
- self.txns.store_client_transaction(request, txn_id, response)
- defer.returnValue(response)
-
# TODO: Needs unit testing
class RoomMembershipRestServlet(ClientV1RestServlet):
@@ -623,22 +585,11 @@ class RoomMembershipRestServlet(ClientV1RestServlet):
return False
return True
- @defer.inlineCallbacks
def on_PUT(self, request, room_id, membership_action, txn_id):
- try:
- defer.returnValue(
- self.txns.get_client_transaction(request, txn_id)
- )
- except KeyError:
- pass
-
- response = yield self.on_POST(
- request, room_id, membership_action, txn_id
+ return self.txns.fetch_or_execute_request(
+ request, self.on_POST, request, room_id, membership_action, txn_id
)
- self.txns.store_client_transaction(request, txn_id, response)
- defer.returnValue(response)
-
class RoomRedactEventRestServlet(ClientV1RestServlet):
def __init__(self, hs):
@@ -669,19 +620,10 @@ class RoomRedactEventRestServlet(ClientV1RestServlet):
defer.returnValue((200, {"event_id": event.event_id}))
- @defer.inlineCallbacks
def on_PUT(self, request, room_id, event_id, txn_id):
- try:
- defer.returnValue(
- self.txns.get_client_transaction(request, txn_id)
- )
- except KeyError:
- pass
-
- response = yield self.on_POST(request, room_id, event_id, txn_id)
-
- self.txns.store_client_transaction(request, txn_id, response)
- defer.returnValue(response)
+ return self.txns.fetch_or_execute_request(
+ request, self.on_POST, request, room_id, event_id, txn_id
+ )
class RoomTypingRestServlet(ClientV1RestServlet):
diff --git a/synapse/rest/client/v1/transactions.py b/synapse/rest/client/v1/transactions.py
deleted file mode 100644
index 2f2c9d0881..0000000000
--- a/synapse/rest/client/v1/transactions.py
+++ /dev/null
@@ -1,97 +0,0 @@
-# -*- coding: utf-8 -*-
-# Copyright 2014-2016 OpenMarket Ltd
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""This module contains logic for storing HTTP PUT transactions. This is used
-to ensure idempotency when performing PUTs using the REST API."""
-import logging
-
-from synapse.api.auth import get_access_token_from_request
-
-logger = logging.getLogger(__name__)
-
-
-# FIXME: elsewhere we use FooStore to indicate something in the storage layer...
-class HttpTransactionStore(object):
-
- def __init__(self):
- # { key : (txn_id, response) }
- self.transactions = {}
-
- def get_response(self, key, txn_id):
- """Retrieve a response for this request.
-
- Args:
- key (str): A transaction-independent key for this request. Usually
- this is a combination of the path (without the transaction id)
- and the user's access token.
- txn_id (str): The transaction ID for this request
- Returns:
- A tuple of (HTTP response code, response content) or None.
- """
- try:
- logger.debug("get_response TxnId: %s", txn_id)
- (last_txn_id, response) = self.transactions[key]
- if txn_id == last_txn_id:
- logger.info("get_response: Returning a response for %s", txn_id)
- return response
- except KeyError:
- pass
- return None
-
- def store_response(self, key, txn_id, response):
- """Stores an HTTP response tuple.
-
- Args:
- key (str): A transaction-independent key for this request. Usually
- this is a combination of the path (without the transaction id)
- and the user's access token.
- txn_id (str): The transaction ID for this request.
- response (tuple): A tuple of (HTTP response code, response content)
- """
- logger.debug("store_response TxnId: %s", txn_id)
- self.transactions[key] = (txn_id, response)
-
- def store_client_transaction(self, request, txn_id, response):
- """Stores the request/response pair of an HTTP transaction.
-
- Args:
- request (twisted.web.http.Request): The twisted HTTP request. This
- request must have the transaction ID as the last path segment.
- response (tuple): A tuple of (response code, response dict)
- txn_id (str): The transaction ID for this request.
- """
- self.store_response(self._get_key(request), txn_id, response)
-
- def get_client_transaction(self, request, txn_id):
- """Retrieves a stored response if there was one.
-
- Args:
- request (twisted.web.http.Request): The twisted HTTP request. This
- request must have the transaction ID as the last path segment.
- txn_id (str): The transaction ID for this request.
- Returns:
- The response tuple.
- Raises:
- KeyError if the transaction was not found.
- """
- response = self.get_response(self._get_key(request), txn_id)
- if response is None:
- raise KeyError("Transaction not found.")
- return response
-
- def _get_key(self, request):
- token = get_access_token_from_request(request)
- path_without_txn_id = request.path.rsplit("/", 1)[0]
- return path_without_txn_id + "/" + token
|