Understand the request lifecycle of an RPC call
mainWhen calling a method like client.API().SomeMethod(ctx, ...), the request flows through the following layers:
- Serialization: The generated
tg.Clientmethod serializes the request using thebinpackage and callsInvokeon thetelegram.Client. - Middleware: Middlewares (such as rate limiting, flood-wait retry, or tracing) process the request.
- Connection Pooling:
telegram.Clientacquires a connection from thepoolfor the target Data Center (DC), handling migrations or redirects to CDN DCs if necessary. - MTProto Layer:
mtproto.Connwraps the payload in an MTProto message (proto), encrypts it using the sessionauth_key(crypto), and writes it via thetransportcodec. - RPC Engine: The
rpcengine manages message IDs, sends acknowledgments (acks), and handles retransmissions. - Response Handling: The response is decrypted, decoded, and routed back to the caller. Simultaneously, updates are dispatched to the
telegram/updatesmanager and then to the user'sUpdateHandler.