Clean-up client's __init__ and remove entity cache

Entity cache uses are removed. It was a source of ever-growing memory
usage that has to be reworked. This affects everything that tried to
obtain an input entity, input sender or input chat (such as the
SenderGetter or calls to _get_entity_pair). Input entities need to be
reworked in any case.

Its removal also affects the automatic cache of any raw API request.

Raise last error parameter is removed, and its behaviour made default.

The connection type parameter has been removed, since users really have
no need to change it.

A few more attributes have been made private, since users should not
mess with those.
This commit is contained in:
Lonami Exo
2022-01-18 12:52:22 +01:00
parent 85a9c13129
commit f8264abb5a
18 changed files with 104 additions and 253 deletions

View File

@@ -76,9 +76,6 @@ async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sl
exceptions.append(e)
results.append(None)
continue
entities = self._entity_cache.add(result)
if entities:
await self.session.insert_entities(entities)
exceptions.append(None)
results.append(result)
request_index += 1
@@ -88,9 +85,6 @@ async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sl
return results
else:
result = await future
entities = self._entity_cache.add(result)
if entities:
await self.session.insert_entities(entities)
return result
except ServerError as e:
last_error = e
@@ -129,10 +123,7 @@ async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sl
raise
await self._switch_dc(e.new_dc)
if self._raise_last_call_error and last_error is not None:
raise last_error
raise ValueError('Request was unsuccessful {} time(s)'
.format(attempt))
raise last_error
async def get_me(self: 'TelegramClient', input_peer: bool = False) \
@@ -147,15 +138,12 @@ async def is_bot(self: 'TelegramClient') -> bool:
return self._session_state.bot if self._session_state else False
async def is_user_authorized(self: 'TelegramClient') -> bool:
if self._authorized is None:
try:
# Any request that requires authorization will work
await self(_tl.fn.updates.GetState())
self._authorized = True
except RpcError:
self._authorized = False
return self._authorized
try:
# Any request that requires authorization will work
await self(_tl.fn.updates.GetState())
return True
except RpcError:
return False
async def get_entity(
self: 'TelegramClient',
@@ -236,14 +224,6 @@ async def get_input_entity(
except TypeError:
pass
# Next in priority is having a peer (or its ID) cached in-memory
try:
# 0x2d45687 == crc32(b'Peer')
if isinstance(peer, int) or peer.SUBCLASS_OF_ID == 0x2d45687:
return self._entity_cache[peer]
except (AttributeError, KeyError):
pass
# Then come known strings that take precedence
if peer in ('me', 'self'):
return _tl.InputPeerSelf()
@@ -254,7 +234,7 @@ async def get_input_entity(
except TypeError:
pass
else:
entity = await self.session.get_entity(None, peer_id)
entity = await self._session.get_entity(None, peer_id)
if entity:
if entity.ty in (Entity.USER, Entity.BOT):
return _tl.InputPeerUser(entity.id, entity.access_hash)