mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-23 18:37:26 +00:00
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:
parent
13094f1d8b
commit
ae028ab4b6
@ -17,29 +17,19 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
import time
|
||||||
from time import perf_counter
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MsgId:
|
class MsgId:
|
||||||
reference_clock = 0
|
|
||||||
last_time = 0
|
last_time = 0
|
||||||
msg_id_offset = 0
|
offset = 0
|
||||||
server_time = 0
|
|
||||||
|
|
||||||
def __new__(cls) -> int:
|
def __new__(cls) -> int:
|
||||||
now = perf_counter() - cls.reference_clock + cls.server_time
|
now = time.time()
|
||||||
cls.msg_id_offset = cls.msg_id_offset + 4 if now == cls.last_time else 0
|
cls.offset = (cls.offset + 4) if now == cls.last_time else 0
|
||||||
msg_id = int(now * 2 ** 32) + cls.msg_id_offset
|
msg_id = int(now * 2 ** 32) + cls.offset
|
||||||
cls.last_time = now
|
cls.last_time = now
|
||||||
|
|
||||||
return msg_id
|
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")
|
|
||||||
|
@ -203,9 +203,6 @@ class Session:
|
|||||||
log.debug(data)
|
log.debug(data)
|
||||||
|
|
||||||
for msg in messages:
|
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.seq_no % 2 != 0:
|
||||||
if msg.msg_id in self.pending_acks:
|
if msg.msg_id in self.pending_acks:
|
||||||
continue
|
continue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user