Switch back to local system time synchronization

perf_counter will stop counting when the system goes to sleep, causing
the generation of invalid message ids after waking up which in turn put
the client into a never ending reconnecting loop due to check mismatches
caused by the time not being synced anymore. It's also unclear whether
perf_counter stays in sync during long runs.
This commit is contained in:
Dan 2022-12-25 14:55:40 +01:00
parent 13094f1d8b
commit ae028ab4b6
2 changed files with 5 additions and 18 deletions

View File

@ -17,29 +17,19 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import logging
from datetime import datetime
from time import perf_counter
import time
log = logging.getLogger(__name__)
class MsgId:
reference_clock = 0
last_time = 0
msg_id_offset = 0
server_time = 0
offset = 0
def __new__(cls) -> int:
now = perf_counter() - cls.reference_clock + cls.server_time
cls.msg_id_offset = cls.msg_id_offset + 4 if now == cls.last_time else 0
msg_id = int(now * 2 ** 32) + cls.msg_id_offset
now = time.time()
cls.offset = (cls.offset + 4) if now == cls.last_time else 0
msg_id = int(now * 2 ** 32) + cls.offset
cls.last_time = now
return msg_id
@classmethod
def set_server_time(cls, server_time: int):
if not cls.server_time:
cls.reference_clock = perf_counter()
cls.server_time = server_time
log.info(f"Time synced: {datetime.utcfromtimestamp(server_time)} UTC")

View File

@ -203,9 +203,6 @@ class Session:
log.debug(data)
for msg in messages:
if msg.seq_no == 0:
MsgId.set_server_time(msg.msg_id / (2 ** 32))
if msg.seq_no % 2 != 0:
if msg.msg_id in self.pending_acks:
continue