Add option to disable automatic last message loading - #3731
Conversation
- Skip automatic last-message history requests without message database. - Keep the current behavior by default.
|
The last message is required for all chats in the user chat list to maintain the correct position of the chat in the list. Any such change will break app UX and therefore can't be accepted to the upstream, but you are free to add it to your fork of TDLib. |
|
@levlam Thanks, I understand the concern now. The option being disabled by default preserves behavior for existing clients, but I agree that enabling it can leave the last message unknown and therefore keep the chat position stale. I also noticed that the TDLib API explicitly allows messages to be added without a corresponding The problem I am trying to solve is the per-dialog Would an implementation that preserves automatic last-message repair, but batches or coalesces repairs for multiple dialogs, be acceptable upstream? |
|
There is no way to "batch" the requests. The last message hardly ever needs to be repaired, therefore regular users have no issues. Bots don't need the last chat message at all. This behavior isn't optional, it is something that must be done always. |
|
Hi |
|
@levlam I agree that the last-message invariant must not be optional. If the proposed option can violate that invariant, then this PR is not the correct implementation. However, the cold-start behavior we observed with a real user account still seems unresolved:
This suggests that the issue is not merely whether last-message loading can be skipped. When TDLib starts without reusable local state, it sends a concentrated fan-out of automatic RPCs. Disabling all databases makes every restart such a cold start. Could you please clarify the intended support contract?
I accept that this PR is not the correct solution if it breaks the last-message invariant. But the cold-start request amplification still appears to need an upstream solution, for example by persisting bounded essential state when the full message database is disabled, restoring non-immediate state lazily, or coordinating automatic background requests so that they are not fanned out at startup. The goal is not to make TDLib's state invariants optional. It is to restore them in a way that allows supported database-less or empty-database startup without repeatedly triggering |
Summary
This PR adds a writable Boolean option named
disable_automatic_last_message_loading.When
use_message_databaseisfalseand this option istrue, TDLib no longer automatically requests message history throughload_last_dialog_messageto populate a chat's last message.The option defaults to
false, so the existing behavior remains unchanged for current clients.Context: #62, #1605, #2286, #2893
Background
TDLib's local database grows over time, and the message database currently has no size limit. This has been discussed in #62 and #1605.
In #2893, disabling all databases through
setTdlibParametersis suggested for applications that don't need them, so thatdb.sqliteis no longer used.However, when the message database is disabled, TDLib can't restore a dialog's last message from the local database.
load_last_dialog_messagecurrently callsget_history_impl, which may eventually send a remotemessages.getHistoryrequest.#2286 documents this behavior: when the last message of a chat becomes unknown, TDLib automatically calls
getChatHistoryto repair it. A large number of such requests may result inFLOOD_WAITerrors.Automatic last-message loading can be triggered by multiple internal paths, including:
Disabling the databases can therefore cause a history-request fan-out that the application can't control. For accounts with many dialogs, these automatic requests may result in
FLOOD_WAITerrors.Some applications intentionally disable the message database and can handle chats whose
last_messageis temporarily unknown. For these applications, automatically loading history isn't required.Changes
This PR adds the following check to the common
MessagesManager::load_last_dialog_messageentry point:It also registers
disable_automatic_last_message_loadingas a writable Boolean option inOptionManager.Placing the check in the common entry point covers all current automatic loading paths that use
load_last_dialog_message, without changing each call site separately.Option semantics
disable_automatic_last_message_loading:falseuse_message_database == falseWhen enabled, TDLib doesn't automatically load message history to populate a chat's last message. The application must be able to handle chats with an unknown last message.
The option has no effect when the message database is enabled.
Unaffected behavior
This change doesn't prevent:
getChatHistory;getDifferenceorgetChannelDifferencerequests;The change only gates automatic history loads made through
load_last_dialog_message.Why this is opt-in
Unconditionally skipping automatic last-message loading whenever the message database is disabled would change the behavior of all existing database-less clients.
Some clients may rely on TDLib to populate
chat.last_messageautomatically. A disabled-by-default option preserves backward compatibility while allowing applications that don't need this behavior to opt out explicitly.Why not use
ignore_background_updatesignore_background_updateschanges offline update recovery and update-state persistence, so its scope is much broader than last-message loading.The new option only controls automatic last-message loading and doesn't modify update-difference code paths.
Usage
The
setOptionrequest needs to be sent to TDLib before thesetTdlibParametersrequest. The client doesn't need to wait for thesetOptionresponse before sending the initialization parameters.First send:
{ "@type": "setOption", "name": "disable_automatic_last_message_loading", "value": { "@type": "optionValueBoolean", "value": true } }Then initialize a database-less client with the relevant fields set to:
{ "@type": "setTdlibParameters", "use_file_database": false, "use_chat_info_database": false, "use_message_database": false }Trade-offs
When the option is enabled:
last_messagemay remain unknown for some chats until TDLib learns it from a subsequent update, a dialog response, or an explicit application request;These changes apply only to clients that explicitly enable the option.
Non-goals
This PR doesn't address:
getPeerDialogs,getPeerSettings, orgetBirthdays;Validation
tdcorebuilds successfully.run_all_testspasses.git diff --checkpasses.