_deadline.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (c) "Neo4j"
  2. # Neo4j Sweden AB [https://neo4j.com]
  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. # https://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. from contextlib import contextmanager
  16. from time import monotonic
  17. class Deadline:
  18. def __init__(self, timeout):
  19. if timeout is None or timeout == float("inf"):
  20. self._deadline = float("inf")
  21. else:
  22. self._deadline = monotonic() + timeout
  23. self._original_timeout = timeout
  24. @property
  25. def original_timeout(self):
  26. return self._original_timeout
  27. def expired(self):
  28. return self.to_timeout() == 0
  29. def to_timeout(self):
  30. if self._deadline == float("inf"):
  31. return None
  32. timeout = self._deadline - monotonic()
  33. return max(0, timeout)
  34. def __eq__(self, other):
  35. if isinstance(other, Deadline):
  36. return self._deadline == other._deadline
  37. return NotImplemented
  38. def __gt__(self, other):
  39. if isinstance(other, Deadline):
  40. return self._deadline > other._deadline
  41. return NotImplemented
  42. def __ge__(self, other):
  43. if isinstance(other, Deadline):
  44. return self._deadline >= other._deadline
  45. return NotImplemented
  46. def __lt__(self, other):
  47. if isinstance(other, Deadline):
  48. return self._deadline < other._deadline
  49. return NotImplemented
  50. def __le__(self, other):
  51. if isinstance(other, Deadline):
  52. return self._deadline <= other._deadline
  53. return NotImplemented
  54. @classmethod
  55. def from_timeout_or_deadline(cls, timeout):
  56. if isinstance(timeout, cls):
  57. return timeout
  58. return cls(timeout)
  59. def __str__(self):
  60. return f"Deadline(timeout={self._original_timeout})"
  61. merge_deadlines = min
  62. def merge_deadlines_and_timeouts(*deadline):
  63. deadlines = map(Deadline.from_timeout_or_deadline, deadline)
  64. return merge_deadlines(deadlines)
  65. @contextmanager
  66. def connection_deadline(connection, deadline):
  67. original_deadline = connection.socket.get_deadline()
  68. if deadline is None and original_deadline is not None:
  69. # nothing to do here
  70. yield
  71. return
  72. deadline = merge_deadlines(
  73. d for d in (deadline, original_deadline) if d is not None
  74. )
  75. connection.socket.set_deadline(deadline)
  76. try:
  77. yield
  78. finally:
  79. connection.socket.set_deadline(original_deadline)