Back

Function randomly timing out doing listDocument query

  • 0
  • Flutter
  • Functions
DennisLBC
8 Feb, 2024, 03:56

I have a very simple function that runs listDocuments on a collection with literally 2 items in it. There are no relationships or anything complicated. It times out roughly half the time, even when its running the identical query with the exact same inputs. The hang always occurs after the log 'feting documents' so I know its the initial listDocuments causing the hang. I am testing with a single user, I've tested it with document existing, document not existing, expired token etc and it all works. But it just consistently hangs roughly half the time. Self hosted.

(The function is supposed to check if there's an existing record in the collection for this users token. If not, generate a token and create a record. If exists, just use that token if its not expired. If its expired, generate a new token and update the record in the collection. This all works as intended when the function doesn't hang.)

TypeScript
import { Client, Databases, Query, ID } from 'node-appwrite';
import { StreamChat } from 'stream-chat';

const serverClient = StreamChat.getInstance('xxxxxx', 'xxxxxxxxxxxxxxxxxxxx');

export default async ({ req, res, log, error }) => {

  const client = new Client();

  client
    .setEndpoint('https://api.xxx.com/v1') // Set your Appwrite endpoint here
    .setProject('xxxxxxxx') // Set your project ID here
    .setKey(process.env.APPWRITE_API_KEY); // Set your API key here

  const tokensCollection = 'xxxxx';
  const dbId = 'xxxxxx';

  // Initialize the database client
  const database = new Databases(client);

  const data = req.headers;
  const uid = data['x-appwrite-user-id'];
  log(`processing UID ${uid}`);

  if (req.method === 'POST') {
    try {
      log(`Fetching Documents`);
      let token;
      const docs = await database.listDocuments(dbId, tokensCollection, [
        Query.equal('uid', uid),
      ]);
      log(`docs: ${docs.total}`);

   //Rest cut for character limit
};
TL;DR
I have a function that times out occasionally when running a listDocuments query on a collection with only 2 items. The function checks if there's an existing record in the collection for a user's token and performs certain actions accordingly. The hang always happens after the "fetching documents" log. I've tested it with different scenarios and it works fine except for the random hang. I'm using a self-hosted setup. Solution: One possible solution is to increase the timeout for the listDocuments query. This can be done by adding a parameter `timeout=5000` (or any desired value) to the `listDocuments` function
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