1

Currently I am working at a simple session handling within a Cloud Function as Dialogflow fulfillment. In my script, I keep various variables, either entered by the user or acquired from external APIs. To handle sessions, I save the variable data inside a Firestore database. However, when two different users are communicating with the bot at the same time, the sessions are still overwriting each other's variables which leads to broken conversations.

Right now, I have the following workflow:

  • when an intent is detected, look for existing session data in Firestore (the Dialogflow session ID given by the external trigger is used as storage key)
  • if there is already any data, load the variable data into the global variables inside the script
  • process the intent's code based on the loaded variables
  • generate an output to the user based on the variables
  • after agent.add(...), save the data back to the Firestore database

For me, this still looks logical. Also, loading and saving to the database is tested and works as intended. But somehow, if two or more users communicate at the same time, the data is overwritten in between. How can I prevent this? And shouldn't Cloud Functions be able to "scale" by themselves, which for me implies that the functions are working for a thousand users at one time without any race conditions?

Deniss T.
  • 2,526
  • 9
  • 21
user2651050
  • 73
  • 1
  • 1
  • 7

1 Answers1

0

You don't show your code, which would help us diagnose the exact problem, but the problematic phrase in your description is

load the variable data into the global variables inside the script

Cloud Functions may use the same instance for multiple calls. So local variables inside a function should be safe to use, but truly global variables may get trampled.

If you continue to have issues, updating your question with the exact coding problems may let us help you better.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • In the code snippet above, I already changed the variable declaration to local (inside the exports.dialogflowFirebaseFulfillment block). However, this also does not work, maybe the massive amount of local variables (now around 30) slows the cloud function down. – user2651050 Nov 15 '19 at 17:15