mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-10 19:46:36 +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
|
||||
import time
|
||||
import struct
|
||||
import os
|
||||
|
||||
|
||||
class Session(ABC):
|
||||
def __init__(self):
|
||||
# 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._flood_sleep_threshold = 60
|
||||
|
||||
|
@ -155,20 +146,6 @@ class Session(ABC):
|
|||
"""
|
||||
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
|
||||
def report_errors(self):
|
||||
"""
|
||||
|
@ -185,21 +162,6 @@ class Session(ABC):
|
|||
"""
|
||||
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
|
||||
def flood_sleep_threshold(self):
|
||||
"""
|
||||
|
@ -214,55 +176,3 @@ class Session(ABC):
|
|||
Sets the new time threshold (integer, float or timedelta).
|
||||
"""
|
||||
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
|
||||
from base64 import b64decode
|
||||
from os.path import isfile as file_exists
|
||||
from threading import Lock, RLock
|
||||
|
||||
from telethon.tl import types
|
||||
from .memory import MemorySession, _SentFileType
|
||||
|
@ -29,10 +28,6 @@ class SQLiteSession(MemorySession):
|
|||
|
||||
def __init__(self, session_id=None):
|
||||
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
|
||||
self.filename = ':memory:'
|
||||
self.save_entities = True
|
||||
|
@ -42,12 +37,8 @@ class SQLiteSession(MemorySession):
|
|||
if not self.filename.endswith(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
|
||||
# TODO ^ Deprecate
|
||||
entities = self._check_migrate_json()
|
||||
|
||||
self._conn = None
|
||||
|
@ -204,21 +195,20 @@ class SQLiteSession(MemorySession):
|
|||
self._update_session_table()
|
||||
|
||||
def _update_session_table(self):
|
||||
with self._db_lock:
|
||||
c = self._cursor()
|
||||
# While we can save multiple rows into the sessions table
|
||||
# currently we only want to keep ONE as the tables don't
|
||||
# tell us which auth_key's are usable and will work. Needs
|
||||
# some more work before being able to save auth_key's for
|
||||
# multiple DCs. Probably done differently.
|
||||
c.execute('delete from sessions')
|
||||
c.execute('insert or replace into sessions values (?,?,?,?)', (
|
||||
self._dc_id,
|
||||
self._server_address,
|
||||
self._port,
|
||||
self._auth_key.key if self._auth_key else b''
|
||||
))
|
||||
c.close()
|
||||
c = self._cursor()
|
||||
# While we can save multiple rows into the sessions table
|
||||
# currently we only want to keep ONE as the tables don't
|
||||
# tell us which auth_key's are usable and will work. Needs
|
||||
# some more work before being able to save auth_key's for
|
||||
# multiple DCs. Probably done differently.
|
||||
c.execute('delete from sessions')
|
||||
c.execute('insert or replace into sessions values (?,?,?,?)', (
|
||||
self._dc_id,
|
||||
self._server_address,
|
||||
self._port,
|
||||
self._auth_key.key if self._auth_key else b''
|
||||
))
|
||||
c.close()
|
||||
|
||||
def get_update_state(self, entity_id):
|
||||
c = self._cursor()
|
||||
|
@ -231,34 +221,30 @@ class SQLiteSession(MemorySession):
|
|||
return types.updates.State(pts, qts, date, seq, unread_count=0)
|
||||
|
||||
def set_update_state(self, entity_id, state):
|
||||
with self._db_lock:
|
||||
c = self._cursor()
|
||||
c.execute('insert or replace into update_state values (?,?,?,?,?)',
|
||||
(entity_id, state.pts, state.qts,
|
||||
state.date.timestamp(), state.seq))
|
||||
c.close()
|
||||
self.save()
|
||||
c = self._cursor()
|
||||
c.execute('insert or replace into update_state values (?,?,?,?,?)',
|
||||
(entity_id, state.pts, state.qts,
|
||||
state.date.timestamp(), state.seq))
|
||||
c.close()
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
"""Saves the current session object as session_user_id.session"""
|
||||
with self._db_lock:
|
||||
self._conn.commit()
|
||||
self._conn.commit()
|
||||
|
||||
def _cursor(self):
|
||||
"""Asserts that the connection is open and returns a cursor"""
|
||||
with self._db_lock:
|
||||
if self._conn is None:
|
||||
self._conn = sqlite3.connect(self.filename,
|
||||
check_same_thread=False)
|
||||
return self._conn.cursor()
|
||||
if self._conn is None:
|
||||
self._conn = sqlite3.connect(self.filename,
|
||||
check_same_thread=False)
|
||||
return self._conn.cursor()
|
||||
|
||||
def close(self):
|
||||
"""Closes the connection unless we're working in-memory"""
|
||||
if self.filename != ':memory:':
|
||||
with self._db_lock:
|
||||
if self._conn is not None:
|
||||
self._conn.close()
|
||||
self._conn = None
|
||||
if self._conn is not None:
|
||||
self._conn.close()
|
||||
self._conn = None
|
||||
|
||||
def delete(self):
|
||||
"""Deletes the current session file"""
|
||||
|
@ -293,11 +279,10 @@ class SQLiteSession(MemorySession):
|
|||
if not rows:
|
||||
return
|
||||
|
||||
with self._db_lock:
|
||||
self._cursor().executemany(
|
||||
'insert or replace into entities values (?,?,?,?,?)', rows
|
||||
)
|
||||
self.save()
|
||||
self._cursor().executemany(
|
||||
'insert or replace into entities values (?,?,?,?,?)', rows
|
||||
)
|
||||
self.save()
|
||||
|
||||
def _fetchone_entity(self, query, args):
|
||||
c = self._cursor()
|
||||
|
@ -346,11 +331,10 @@ class SQLiteSession(MemorySession):
|
|||
if not isinstance(instance, (InputDocument, InputPhoto)):
|
||||
raise TypeError('Cannot cache %s instance' % type(instance))
|
||||
|
||||
with self._db_lock:
|
||||
self._cursor().execute(
|
||||
'insert or replace into sent_files values (?,?,?,?,?)', (
|
||||
md5_digest, file_size,
|
||||
_SentFileType.from_type(type(instance)).value,
|
||||
instance.id, instance.access_hash
|
||||
))
|
||||
self.save()
|
||||
self._cursor().execute(
|
||||
'insert or replace into sent_files values (?,?,?,?,?)', (
|
||||
md5_digest, file_size,
|
||||
_SentFileType.from_type(type(instance)).value,
|
||||
instance.id, instance.access_hash
|
||||
))
|
||||
self.save()
|
||||
|
|
Loading…
Reference in New Issue
Block a user