mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-27 01:34:29 +03:00
Remove unused fields from the Sessions
Most of the stuff didn't actually need to be saved and only belong to the MTProtoState which is not a separate class from the sessions.
This commit is contained in:
parent
bb3a564500
commit
1247d050ab
|
@ -1,18 +1,9 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
import time
|
|
||||||
import struct
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
class Session(ABC):
|
class Session(ABC):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Session IDs can be random on every connection
|
# Session IDs can be random on every connection
|
||||||
self.id = struct.unpack('q', os.urandom(8))[0]
|
|
||||||
|
|
||||||
self._sequence = 0
|
|
||||||
self._last_msg_id = 0
|
|
||||||
self._time_offset = 0
|
|
||||||
self._salt = 0
|
|
||||||
self._report_errors = True
|
self._report_errors = True
|
||||||
self._flood_sleep_threshold = 60
|
self._flood_sleep_threshold = 60
|
||||||
|
|
||||||
|
@ -155,20 +146,6 @@ class Session(ABC):
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@property
|
|
||||||
def salt(self):
|
|
||||||
"""
|
|
||||||
Returns the current salt used when encrypting messages.
|
|
||||||
"""
|
|
||||||
return self._salt
|
|
||||||
|
|
||||||
@salt.setter
|
|
||||||
def salt(self, value):
|
|
||||||
"""
|
|
||||||
Updates the salt (integer) used when encrypting messages.
|
|
||||||
"""
|
|
||||||
self._salt = value
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def report_errors(self):
|
def report_errors(self):
|
||||||
"""
|
"""
|
||||||
|
@ -185,21 +162,6 @@ class Session(ABC):
|
||||||
"""
|
"""
|
||||||
self._report_errors = value
|
self._report_errors = value
|
||||||
|
|
||||||
@property
|
|
||||||
def time_offset(self):
|
|
||||||
"""
|
|
||||||
Time offset (in seconds) to be used
|
|
||||||
in case the local time is incorrect.
|
|
||||||
"""
|
|
||||||
return self._time_offset
|
|
||||||
|
|
||||||
@time_offset.setter
|
|
||||||
def time_offset(self, value):
|
|
||||||
"""
|
|
||||||
Updates the integer time offset in seconds.
|
|
||||||
"""
|
|
||||||
self._time_offset = value
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def flood_sleep_threshold(self):
|
def flood_sleep_threshold(self):
|
||||||
"""
|
"""
|
||||||
|
@ -214,55 +176,3 @@ class Session(ABC):
|
||||||
Sets the new time threshold (integer, float or timedelta).
|
Sets the new time threshold (integer, float or timedelta).
|
||||||
"""
|
"""
|
||||||
self._flood_sleep_threshold = value
|
self._flood_sleep_threshold = value
|
||||||
|
|
||||||
@property
|
|
||||||
def sequence(self):
|
|
||||||
"""
|
|
||||||
Current sequence number needed to generate messages.
|
|
||||||
"""
|
|
||||||
return self._sequence
|
|
||||||
|
|
||||||
@sequence.setter
|
|
||||||
def sequence(self, value):
|
|
||||||
"""
|
|
||||||
Updates the sequence number (integer) value.
|
|
||||||
"""
|
|
||||||
self._sequence = value
|
|
||||||
|
|
||||||
def get_new_msg_id(self):
|
|
||||||
"""
|
|
||||||
Generates a new unique message ID based on the current
|
|
||||||
time (in ms) since epoch, applying a known time offset.
|
|
||||||
"""
|
|
||||||
now = time.time() + self._time_offset
|
|
||||||
nanoseconds = int((now - int(now)) * 1e+9)
|
|
||||||
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
|
|
||||||
|
|
||||||
if self._last_msg_id >= new_msg_id:
|
|
||||||
new_msg_id = self._last_msg_id + 4
|
|
||||||
|
|
||||||
self._last_msg_id = new_msg_id
|
|
||||||
|
|
||||||
return new_msg_id
|
|
||||||
|
|
||||||
def update_time_offset(self, correct_msg_id):
|
|
||||||
"""
|
|
||||||
Updates the time offset to the correct
|
|
||||||
one given a known valid message ID.
|
|
||||||
"""
|
|
||||||
now = int(time.time())
|
|
||||||
correct = correct_msg_id >> 32
|
|
||||||
self._time_offset = correct - now
|
|
||||||
self._last_msg_id = 0
|
|
||||||
|
|
||||||
def generate_sequence(self, content_related):
|
|
||||||
"""
|
|
||||||
Generates the next sequence number depending on whether
|
|
||||||
it should be for a content-related query or not.
|
|
||||||
"""
|
|
||||||
if content_related:
|
|
||||||
result = self._sequence * 2 + 1
|
|
||||||
self._sequence += 1
|
|
||||||
return result
|
|
||||||
else:
|
|
||||||
return self._sequence * 2
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
from os.path import isfile as file_exists
|
from os.path import isfile as file_exists
|
||||||
from threading import Lock, RLock
|
|
||||||
|
|
||||||
from telethon.tl import types
|
from telethon.tl import types
|
||||||
from .memory import MemorySession, _SentFileType
|
from .memory import MemorySession, _SentFileType
|
||||||
|
@ -29,10 +28,6 @@ class SQLiteSession(MemorySession):
|
||||||
|
|
||||||
def __init__(self, session_id=None):
|
def __init__(self, session_id=None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
"""session_user_id should either be a string or another Session.
|
|
||||||
Note that if another session is given, only parameters like
|
|
||||||
those required to init a connection will be copied.
|
|
||||||
"""
|
|
||||||
# These values will NOT be saved
|
# These values will NOT be saved
|
||||||
self.filename = ':memory:'
|
self.filename = ':memory:'
|
||||||
self.save_entities = True
|
self.save_entities = True
|
||||||
|
@ -42,12 +37,8 @@ class SQLiteSession(MemorySession):
|
||||||
if not self.filename.endswith(EXTENSION):
|
if not self.filename.endswith(EXTENSION):
|
||||||
self.filename += EXTENSION
|
self.filename += EXTENSION
|
||||||
|
|
||||||
# Cross-thread safety
|
|
||||||
self._seq_no_lock = Lock()
|
|
||||||
self._msg_id_lock = Lock()
|
|
||||||
self._db_lock = RLock()
|
|
||||||
|
|
||||||
# Migrating from .json -> SQL
|
# Migrating from .json -> SQL
|
||||||
|
# TODO ^ Deprecate
|
||||||
entities = self._check_migrate_json()
|
entities = self._check_migrate_json()
|
||||||
|
|
||||||
self._conn = None
|
self._conn = None
|
||||||
|
@ -204,7 +195,6 @@ class SQLiteSession(MemorySession):
|
||||||
self._update_session_table()
|
self._update_session_table()
|
||||||
|
|
||||||
def _update_session_table(self):
|
def _update_session_table(self):
|
||||||
with self._db_lock:
|
|
||||||
c = self._cursor()
|
c = self._cursor()
|
||||||
# While we can save multiple rows into the sessions table
|
# While we can save multiple rows into the sessions table
|
||||||
# currently we only want to keep ONE as the tables don't
|
# currently we only want to keep ONE as the tables don't
|
||||||
|
@ -231,7 +221,6 @@ class SQLiteSession(MemorySession):
|
||||||
return types.updates.State(pts, qts, date, seq, unread_count=0)
|
return types.updates.State(pts, qts, date, seq, unread_count=0)
|
||||||
|
|
||||||
def set_update_state(self, entity_id, state):
|
def set_update_state(self, entity_id, state):
|
||||||
with self._db_lock:
|
|
||||||
c = self._cursor()
|
c = self._cursor()
|
||||||
c.execute('insert or replace into update_state values (?,?,?,?,?)',
|
c.execute('insert or replace into update_state values (?,?,?,?,?)',
|
||||||
(entity_id, state.pts, state.qts,
|
(entity_id, state.pts, state.qts,
|
||||||
|
@ -241,12 +230,10 @@ class SQLiteSession(MemorySession):
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Saves the current session object as session_user_id.session"""
|
"""Saves the current session object as session_user_id.session"""
|
||||||
with self._db_lock:
|
|
||||||
self._conn.commit()
|
self._conn.commit()
|
||||||
|
|
||||||
def _cursor(self):
|
def _cursor(self):
|
||||||
"""Asserts that the connection is open and returns a cursor"""
|
"""Asserts that the connection is open and returns a cursor"""
|
||||||
with self._db_lock:
|
|
||||||
if self._conn is None:
|
if self._conn is None:
|
||||||
self._conn = sqlite3.connect(self.filename,
|
self._conn = sqlite3.connect(self.filename,
|
||||||
check_same_thread=False)
|
check_same_thread=False)
|
||||||
|
@ -255,7 +242,6 @@ class SQLiteSession(MemorySession):
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Closes the connection unless we're working in-memory"""
|
"""Closes the connection unless we're working in-memory"""
|
||||||
if self.filename != ':memory:':
|
if self.filename != ':memory:':
|
||||||
with self._db_lock:
|
|
||||||
if self._conn is not None:
|
if self._conn is not None:
|
||||||
self._conn.close()
|
self._conn.close()
|
||||||
self._conn = None
|
self._conn = None
|
||||||
|
@ -293,7 +279,6 @@ class SQLiteSession(MemorySession):
|
||||||
if not rows:
|
if not rows:
|
||||||
return
|
return
|
||||||
|
|
||||||
with self._db_lock:
|
|
||||||
self._cursor().executemany(
|
self._cursor().executemany(
|
||||||
'insert or replace into entities values (?,?,?,?,?)', rows
|
'insert or replace into entities values (?,?,?,?,?)', rows
|
||||||
)
|
)
|
||||||
|
@ -346,7 +331,6 @@ class SQLiteSession(MemorySession):
|
||||||
if not isinstance(instance, (InputDocument, InputPhoto)):
|
if not isinstance(instance, (InputDocument, InputPhoto)):
|
||||||
raise TypeError('Cannot cache %s instance' % type(instance))
|
raise TypeError('Cannot cache %s instance' % type(instance))
|
||||||
|
|
||||||
with self._db_lock:
|
|
||||||
self._cursor().execute(
|
self._cursor().execute(
|
||||||
'insert or replace into sent_files values (?,?,?,?,?)', (
|
'insert or replace into sent_files values (?,?,?,?,?)', (
|
||||||
md5_digest, file_size,
|
md5_digest, file_size,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user