serverless-chat icon indicating copy to clipboard operation
serverless-chat copied to clipboard

Idempotency Concern of the ChatFunction

Open NullPointer4096 opened this issue 2 years ago • 1 comments

Description:

I would like to kindly bring attention to a potential issue in the ChatFunction, which is invoked by the chatRule IoT TopicRule. This function calls the IotData.publish to send chat messages to the frontend, where the frontend appends the new messages with previous ones. However, this message sending is not idempotent. Suppose the IoT-triggered function crashes after successfully sending a chat message to the frontend-facing IoT topic (but before acknowledging the event source that the function has finished). In that case, when the function is retried, another message will be sent and received by the frontend.

Suggested Fix:

Although there's no serious harm in sending two identical chat messages, this duplicate sending due to untimely retries can be prevented. Specifically, one can attach a unique id to each chat message when the message is being generated. Before the frontend appends the new message to the message list, it should check whether the message id is already present in a deduplication set for holding all previous chat message ids. If the id is already present, don't append the message. Otherwise, append the message as usual.

Thank you for considering this feedback. I hope that this suggestion could help improve the idempotency and user experience of the ChatFunction. Please feel free to reach out if you have any questions or concerns.

NullPointer4096 avatar Apr 16 '23 20:04 NullPointer4096

Thanks, that's a very good point! If you'd like to contribute a PR with your suggested fix, I'd be happy to merge it.

danilop avatar Apr 18 '23 14:04 danilop