Expand description
Session persistence types and storage backends for ferogram.
This crate is part of ferogram, an async Rust MTProto client built by Ankit Chaubey.
- Channel: t.me/Ferogram
- Chat: t.me/FerogramChat
Most users do not use this crate directly. The ferogram crate wires it up
automatically when you call Client::builder().session(...) or
.session_string(...).
§What it stores
- DC address table with per-DC auth keys, salts, and capability flags
(
DcFlags: IPv6, media-only, TCP-obfuscated-only, CDN, static). Both IPv4 and IPv6 entries can be kept for the same DC;PersistedSession::dc_forpicks the right one for a givenprefer_ipv6. - MTProto update counters: pts, qts, seq, date, and per-channel pts.
- Peer access-hash cache for users, channels, groups, and Communities.
Communities are tracked separately via
is_communityso they are never mistaken for a supergroup on reload. - Min-user message contexts for
InputPeerUserFromMessage.
Call PersistedSession::stats for a SessionStats breakdown, peer
counts by kind, DCs with a negotiated auth key, approximate serialized
size, useful for spotting a bloated min_peers cache before it needs
pruning.
§What’s in here
PersistedSession: the serializable session struct. Holds the DC table, update sequence counters, and the peer access-hash cache.SessionBackend: the trait all backends implement:save,load,delete,name.BinaryFileBackend: stores the session as a binary file on disk. Default backend. Saves are atomic, written to a.tmpfile then renamed.InMemoryBackend: keeps everything in memory. Nothing survives process exit. Used for tests and ephemeral tasks.StringSessionBackend: serializes the session to a base64 string. Useful for serverless environments where you store state in an env var or database column. Load it withClient::builder().session_string(s).SqliteBackend: SQLite-backed storage via rusqlite, behind thesqlite-sessionfeature.openfor a file,in_memoryfor tests.LibSqlBackend: libSQL storage, behindlibsql-session.open_localandin_memorywork with no remote server.open_remote(a live Turso connection) andopen_replica(a local file kept synced with Turso) needlibsql-remote-sessionon top, and can’t be combined withsqlite-session, both link a sqlite3 C source and the build fails at link time.
You can also implement SessionBackend yourself for Redis, PostgreSQL, or
anything else.
§String sessions
Two formats, both accepted by Client::builder().session_string("..."),
which auto-detects which one it got.
- Compact (
export_session_string()): dc_id, ip, port, user_id, and auth key only. Good for serverless or portable deployments. - Native (
export_native_session_string()): full DC table, update counters, and peer cache. Use it when you need to resume update processing from exactly where you left off.
§Binary format
The file backends start with a version byte:
0x01: legacy (DC table only, no update state or peer cache).0x02: current (DC table + update state + peer cache).0x06: adds per-channelChannelKindbyte to each peer entry.0x07: addsfuture_auth_tokenfor fast re-login aftersign_out().0x08: adds a per-peeris_communitybyte, marking Telegram Community entities so they no longer collapse into a plain channel on reload.
load() handles all versions. save() always writes v8.
§Example: export and re-import a session
// Export
let s = client.export_session_string().await?;
// Later, in another process or after a restart:
let (client, _) = ferogram::Client::builder()
.api_id(12345)
.api_hash("api_hash")
.session_string(s)
.connect()
.await?;Re-exports§
pub use string_session::FullSession;pub use string_session::Session;pub use string_session::StringSession;pub use string_session::StringSessionError;
Modules§
- string_
session - Portable compact string-session encoding/decoding (V1/V2 binary base64).
Structs§
- Binary
File Backend - Stores the session in a compact binary file (v2 format).
- Cached
MinPeer - A min-user context entry: the user was seen with
min=true(access_hash not usable directly) so we store the peer+message where they appeared so thatInputPeerUserFromMessagecan be constructed on restart. - Cached
Peer - A cached access-hash entry so that the peer can be addressed across restarts without re-resolving it from Telegram.
- DcEntry
- One entry in the DC address table.
- DcFlags
- Per-DC option flags.
- InMemory
Backend - Ephemeral in-process session: nothing persisted to disk.
- Persisted
Session - Everything that needs to survive a process restart.
- Session
Stats - A breakdown of what a
PersistedSessioncurrently holds. - Sqlite
Backend sqlite-session - SQLite-backed session (via
rusqlite). - String
Session Backend - Portable base64 string session backend.
- Updates
State Snap - Snapshot of the MTProto update-sequence state that we persist so that
catch_up: truecan callupdates.getDifferencewith the pre-shutdown pts.
Enums§
- Channel
Kind - The kind of a Telegram channel, persisted in the session so that
is_megagroup()/is_broadcast()work correctly after a restart. - Update
State Change - A single update-sequence change, applied via
SessionBackend::apply_update_state.
Traits§
- Session
Backend - Synchronous snapshot backend: saves and loads the full session at once.
Functions§
- default_
dc_ addresses - Bootstrap DC address table (fallback if GetConfig fails).