Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core rewrite #843

Merged
merged 56 commits into from
Jun 14, 2018
Merged

Core rewrite #843

merged 56 commits into from
Jun 14, 2018

Conversation

Lonami
Copy link
Member

@Lonami Lonami commented Jun 11, 2018

Summary

This branch is an attempt to rewrite the heart of the library under asyncio with the lessons learnt. The following is a rough UML diagram that depicts the current state:
image

The basic idea is that the MTProtoSender should not be concerned about Session files and just be able to handle arbitrary connections to arbitrary datacenters, as well as handling automatic reconnection in case of failure (but not reconnection for the first connect() call- that should be users' responsability).

Rationale

By sending all requests and receiving all responses from a separate loop, it makes it much easier to reason about the flow of the program, which is really important:

  1. A request will be enqueued for sending, and its Future returned.
  2. The request will be sent eventually, going from the sender, to the connection, to the TCP socket.
  3. The future will be set with a response eventually, coming from the TCP socket, passing through the connection.

Using the standard way to handle Future gives much more power than handrolling a custom solution, and forces a send in order to get a handle you can await for receival. The futures can be cancelled, and if they have not been sent yet, they will be popped from the queue, which means timeouts can occur at this level.

The MTProtoSender is perfectly usable to test the performance at its lowest level to possibly spot new bottlenecks.

Bugs that this probably fixes

These issues will be closed upon merging, and should be reopened if they still occur.

Pending things

  • Probably double check everything to make sure that, at least, the basic functionality is implemented, such as enough async/await and exporting clients to different datacenters to download media.
  • Make sure the new telethon.client package is properly documented in readthedocs.
  • Turn master into the asyncio version, and create a backward-compatible "threaded" variant where you don't need to worry about await'ing anything. This is very important.
  • Rename asyncio into asyncio-stale or similar since this will be its new variant, but the history should be preserved.
  • Test the automatic sending of MessageContainer when a future may be cancelled. May be a good time to start writing proper tests.

Lonami added 30 commits June 6, 2018 20:41
This means that the TcpClient and the Connection (currently only
ConnectionTcpFull) will no longer be concerned about handling
errors, but the MTProtoSender will.

The foundation of the library will now be based on asyncio.
This frees us from using entire Session objects in something
that's supposed to just send and receive items from the net.
This results in a cleaner MTProtoSender, which now can always
read a TLObject with a guaranteed item, if the message is OK.
This simplifies the flow instead of having separate request/body
attributes, and also means that BinaryReader.tgread_object() can
be used without so many special cases.
There's 12..1024 padding for the MTProto 2.0 protocol, and
the length of the message can be used to determine how much
must be read on rpc_results. However this random padding
can be safely ignored.
@Lonami Lonami merged commit bb3a564 into master Jun 14, 2018
@Lonami Lonami deleted the core-rewrite branch June 14, 2018 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant