grammyjs / conversations

Conversational interfaces for grammY.
https://grammy.dev/plugins/conversations
MIT License
53 stars 17 forks source link

Conversation touch other sessions. #86

Closed BUTURUM closed 1 year ago

BUTURUM commented 1 year ago

I use two sessions in my bot: one with external storage, one with memory storage (for conversations).

bot.use(session({
    profile : {
        storage : require('./storage.js')
    },
    conversation : {}, type : 'multi'
}));

I am traking when different methods of storage was called and was suprised when I found that each time when user enters the conversation profile storage's write method is called with no changes. I didn't expect such behaviour and still don't understand it. Encoding to documentation conversation should only use session provided for them (conversation session).

KnorpelSenf commented 1 year ago

The conversations plugin doesn't do this. The session plugin calls write for all session parts as soon as it's done handling an update.

The conversations plugin has to serialise the entire session so that it can replay old session data as necessary. The session plugin sees that someone has accessed the session, but it has no way to determine whether or not it was modified, so it needs to write back the data.

This is therefore the expected behaviour.

BUTURUM commented 1 year ago

Thanks for explanation @KnorpelSenf. Now this behaviour for me isn't unexpected, but unwanted. I don't want to request database after literally every message inside conversation, when nothing in database supposed to be changed. In my opinion it wastes machine's resources and harms application's logic. How to avoid it?

BUTURUM commented 1 year ago

Well the solution was lazy sessions. Thanks everybody for help.

BUTURUM commented 1 year ago

I am sorry, propably I closed issue too early. I found out that there no way to lazy multi sessions! Despite I solved my own issue, I assume that respond to my question could be useful for other people. So how to use lazy sessions in order to allow request to database only if it reasonable change, but conversation plugin's data?

KnorpelSenf commented 1 year ago
  1. Conversations cannot work correctly if they are not allowed access to the session data. If you would access the session after another wait call, the data might be gone, so this would be a logical error. Session data must be copied, there's no way around it.
  2. Lazy multi sessions add substantial implementation complexity, but in most cases, they will lead to poor performance for the bot. We added them initially, but they did not make it to the final release because they make little sense.
  3. You can use a database instead of sessions if you need more control. However, if used inside conversations, you will have to wrap your database communication into conversation.external calls which temporarily copy the data to the session again in order to avoid logic errors.
  4. If your session database is so slow that you cannot afford querying it, you should consider storing things in memory instead.
rojvv commented 1 year ago

@BUTURUM Is your issue resolved?

KnorpelSenf commented 1 year ago

Closing due to inactivity.