Appointment updates only do not decrease slots if not necessary

- For a given appointment, checks if it is an update and computes the difference of sizes if so.
- Additional slots are only filled if the new version is bigger. Slots are freed if the update is smaller.
- Adds get_appoiment_summary to get information in memory information of an appointment (so the API can check if a request is an update)
	- The API computes the uuid and requests it to the tower.
	- Size field has been added to all in memory appointments
This commit is contained in:
Sergi Delgado Segura
2020-03-27 18:16:25 +01:00
parent 02fd2e5149
commit e924b57efc
2 changed files with 47 additions and 6 deletions

View File

@@ -169,19 +169,41 @@ class API:
appointment = self.inspector.inspect(request_data.get("appointment")) appointment = self.inspector.inspect(request_data.get("appointment"))
user_pk = self.gatekeeper.identify_user(appointment.serialize(), request_data.get("signature")) user_pk = self.gatekeeper.identify_user(appointment.serialize(), request_data.get("signature"))
# An appointment will fill 1 slot per ENCRYPTED_BLOB_MAX_SIZE_HEX block. # Check if the appointment is an update. Updates will return a summary.
# Temporarily taking out slots to avoid abusing this via race conditions. appointment_uuid = hash_160("{}{}".format(appointment.locator, user_pk))
# DISCUSS: It may be worth using signals here to avoid race conditions anyway. appointment_summary = self.watcher.get_appointment_summary(appointment_uuid)
# For updates we only reserve the slot difference provided the new one is bigger.
if appointment_summary:
size_diff = len(appointment.encrypted_blob.data) - appointment_summary.get("size")
slot_diff = ceil(size_diff / ENCRYPTED_BLOB_MAX_SIZE_HEX)
required_slots = slot_diff if slot_diff > 0 else 0
# For regular appointments 1 slot is reserved per ENCRYPTED_BLOB_MAX_SIZE_HEX block.
else:
slot_diff = 0
required_slots = ceil(len(appointment.encrypted_blob.data) / ENCRYPTED_BLOB_MAX_SIZE_HEX) required_slots = ceil(len(appointment.encrypted_blob.data) / ENCRYPTED_BLOB_MAX_SIZE_HEX)
# Slots are reserved before adding the appointments to prevent race conditions.
# DISCUSS: It may be worth using signals here to avoid race conditions anyway.
self.gatekeeper.fill_slots(user_pk, required_slots) self.gatekeeper.fill_slots(user_pk, required_slots)
appointment_added, signature = self.watcher.add_appointment(appointment, user_pk) appointment_added, signature = self.watcher.add_appointment(appointment, user_pk)
if appointment_added: if appointment_added:
rcode = HTTP_OK rcode = HTTP_OK
response = {"locator": appointment.locator, "signature": signature} response = {
"locator": appointment.locator,
"signature": signature,
"available_slots": self.gatekeeper.registered_users[user_pk],
}
# If the appointment is added and the update is smaller than the original, the difference is given back.
if slot_diff < 0:
self.gatekeeper.free_slots(slot_diff)
else: else:
# Adding back the slots since they were not used # If the appointment is not added the reserved slots are given back
self.gatekeeper.free_slots(user_pk, required_slots) self.gatekeeper.free_slots(user_pk, required_slots)
rcode = HTTP_SERVICE_UNAVAILABLE rcode = HTTP_SERVICE_UNAVAILABLE
response = {"error": "appointment rejected"} response = {"error": "appointment rejected"}

View File

@@ -76,6 +76,21 @@ class Watcher:
return watcher_thread return watcher_thread
def get_appointment_summary(self, uuid):
"""
Returns the summary of an appointment. The summary consists of the data kept in memory:
locator, end_time, and size.
Args:
uuid (:obj:`str`): a 16-byte hex string identifying the appointment.
Returns:
:obj:`dict` or :obj:`None`: a dictionary with the appointment summary, or None if the appointment is not
found.
"""
return self.appointments.get(uuid)
def add_appointment(self, appointment, user_pk): def add_appointment(self, appointment, user_pk):
""" """
Adds a new appointment to the ``appointments`` dictionary if ``max_appointments`` has not been reached. Adds a new appointment to the ``appointments`` dictionary if ``max_appointments`` has not been reached.
@@ -112,7 +127,11 @@ class Watcher:
# anything about the user from this point on (no need to store user_pk in the database). # anything about the user from this point on (no need to store user_pk in the database).
# If an appointment is requested by the user the uuid can be recomputed and queried straightaway (no maps). # If an appointment is requested by the user the uuid can be recomputed and queried straightaway (no maps).
uuid = hash_160("{}{}".format(appointment.locator, user_pk)) uuid = hash_160("{}{}".format(appointment.locator, user_pk))
self.appointments[uuid] = {"locator": appointment.locator, "end_time": appointment.end_time} self.appointments[uuid] = {
"locator": appointment.locator,
"end_time": appointment.end_time,
"size": len(appointment.encrypted_blob.data),
}
if appointment.locator in self.locator_uuid_map: if appointment.locator in self.locator_uuid_map:
# If the uuid is already in the map it means this is an update. # If the uuid is already in the map it means this is an update.