-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Core rewrite #843
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There is only one method sending and one method receiving, so it doesn't make sense to lock-protect those operations.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:The basic idea is that the
MTProtoSender
should not be concerned aboutSession
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 firstconnect()
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:
Future
returned.Using the standard way to handle
Future
gives much more power than handrolling a custom solution, and forces asend
in order to get a handle you canawait
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
disconnect()
willawait
for the started send and receive pseudo-loops to finish.asyncio
version.These issues will be closed upon merging, and should be reopened if they still occur.
Pending things
async
/await
and exporting clients to different datacenters to download media.telethon.client
package is properly documented inreadthedocs
.master
into theasyncio
version, and create a backward-compatible "threaded" variant where you don't need to worry aboutawait
'ing anything. This is very important.asyncio
intoasyncio-stale
or similar since this will be its new variant, but the history should be preserved.MessageContainer
when a future may be cancelled. May be a good time to start writing proper tests.