Skip to main content

Module util

Module util 

Source

Constants§

COMPRESSION_THRESHOLD
Minimum body size above which we attempt zlib compression.

Functions§

build_container_body
Build the body of a msg_container#73f1f8dc from a list of (msg_id, seqno, body) inner messages.
build_msgs_ack_body
Build the TL body for msgs_ack#62d6b459 msg_ids:Vector<long>.
crc32_ieee
CRC-32 using the standard IEEE 802.3 polynomial (for Full transport framing).
gz_inflate
Decompress a gzip_packed payload. Tries gzip first (the standard format); if that fails or yields nothing, retries as raw zlib (no gzip header) before giving up.
gz_pack_body
Wrap data in a gzip_packed#3072cfa1 packed_data:bytes TL frame.
jitter_delay
Apply ±20 % random jitter to a backoff delay. Prevents thundering-herd when many clients reconnect simultaneously (e.g. after a server restart or a shared network outage).
maybe_gz_decompress
Unwrap a response body if it’s wrapped in gzip_packed, identified by its constructor ID; otherwise returns body unchanged.
maybe_gz_pack
Optionally compress data. Returns the compressed gzip_packed wrapper if it is shorter than the original; otherwise returns data unchanged.
random_i64
Generate a random i64. Used for random_id fields in RPC requests, where Telegram only needs uniqueness, not cryptographic strength.
tl_read_bytes
Decode a TL bytes value: the read-side counterpart to tl_write_bytes. Returns None on a truncated or malformed length prefix. Doesn’t skip the 4-byte alignment padding TL normally adds after the payload, since this is only used to unwrap gzip_packed’s single trailing field.
tl_read_string
Like tl_read_bytes but decodes the payload as UTF-8 (lossily, so invalid sequences become replacement characters instead of failing).
tl_write_bytes
TL bytes wire encoding (used inside gzip_packed).