Back

Appwrite function can't handle concurrent users?!

  • 0
  • Functions
nightwielder
19 Jun, 2023, 07:21

I created a function that fetches some data from the Appwrite database. When I alone test my website, it works nice and smoothly, very fast too. But whenever my friend I visit the website at the same time, it gets stuck either on my side or his, and after 15 seconds it results in an error. An internal curl error has occurred within the executor! Error Msg: Operation timed out The Appwrite instance is running on a DigitalOcean droplet. How do I solve this issue? Thanks.

TL;DR
Appwrite function can't handle concurrent users on a website, causing it to become unusable. The code is enclosed in a try-catch block and the function timeout is set to 5 minutes. Increase the function timeout and check for any AppwriteExceptions in the catch block. This might not be the only function failing, as smaller functions also fail. It's suggested to check if all functions are enclosed in a try-catch block. This issue occurs even with concurrent users on a self-hosted instance or in the cloud. The error message received is "An internal curl error has occurred within the executor! Error Msg: Operation timed out
safwan
19 Jun, 2023, 07:28

This is super weird.

I've had concurrent users on a self-hosted instance and even on cloud. Never had an issue with a function not executing πŸ€”

safwan
19 Jun, 2023, 07:28

Could you share the code of your function?

nightwielder
19 Jun, 2023, 07:32

Yeah I find it very weird too! But it's happening constantly, if there are two users the website is becoming unusable. This could be a problem with DigitalOcean too? I don't know.

nightwielder
19 Jun, 2023, 07:36

Sure. Something like this:

TypeScript
const comment = JSON.parse(req.payload); 
  const commentDoc = {
  questionId: comment.questionId,
  body: comment.body,
  by: req.variables["APPWRITE_FUNCTION_USER_ID"],
};

const response = await database.createDocument(
  "643d4f079b55031ba6b6",
  "646b45b9e454f1b4d1c4",
  sdk.ID.unique(),
  commentDoc,
  [
    // permissions
  ]
);
// Updating Question's Comment Count
const question = await database.getDocument(
  "643d4f079b55031ba6b6",
  "643d4f2cd70649dd9083",
  comment.questionId
);
await database.updateDocument(
  "643d4f079b55031ba6b6",
  "643d4f2cd70649dd9083",
  comment.questionId,
  { commentCount: question.commentCount + 1 }
);

// Creating Notification
if (question.askerId !== req.variables["APPWRITE_FUNCTION_USER_ID"]) {
  const notification = {
    for: question.askerId,
    message: `${comment.by.name} has commented on your question.`,
    read: false,
    payload: question.$id,
    type: "comment",
  };

  await database.createDocument(
    "643d4f079b55031ba6b6",
    "648f59f58faa3642c794",
    sdk.ID.unique(),
    notification,
    [
       // permissions
    ]
  );

  await database.updateDocument(
    "643d4f079b55031ba6b6",
    "643dbd7c35b3663184f3",
    question.askerId,
    { newNotifications: true }
  )
}
nightwielder
19 Jun, 2023, 07:37

But, do note that this isn't the only function that fails. Smaller functions fail too.

safwan
19 Jun, 2023, 07:37

is this code enclosed in a try-catch?

safwan
19 Jun, 2023, 07:37

in that case, are all these functions enclosed in a try-catch?

nightwielder
19 Jun, 2023, 07:38

Nope, it isn't. Would it help?

safwan
19 Jun, 2023, 07:40

Worth a try!

  • In the catch block, look for an AppwriteException.
  • Increase the function timeout to 5 minutes so it keep running and you will be able to see any errors that may occur.
safwan
19 Jun, 2023, 07:41

Also, make sure to call res.json in the catch block before the return statement.

nightwielder
19 Jun, 2023, 07:41

Okay trying both now! I shall let you know.

safwan
19 Jun, 2023, 07:42
TypeScript
catch (AppwriteException e) {
  res.json({
    'message': e.message,
    'status': 'failed'
  });
}
safwan
19 Jun, 2023, 07:42

an example ^

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more