我有一个Firebase Cloud 功能,可执行以下操作:

const firestore = require('firebase-admin')
const functions = require('firebase-functions')
const regex = require('../../utils/regex')

exports = module.exports = functions.https.onCall((data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.')
  }

  if (!data['displayName']) {
    throw new functions.https.HttpsError('failed-precondition', 'An display name must be provided.')
  }

  if (!regex.noSpecialCharactersExceptSpace.test(data['displayName'])) {
    throw new functions.https.HttpsError('input-validation-failed', 'Your display name cannot have special characters in it.')
  }

  return firestore.firestore().collection('profiles').doc(context.auth.uid)
    .update({
      displayName: data['displayName']
    })
    .then(() => {
      console.info('Successful public profile update user='+ context.auth.uid)
      return { text: 'Your profile has successfully been updated' };
    })
    .catch((error) => {
      console.error('Error updating public profile user=' + context.auth.uid + ' error=' + error)
      throw new functions.https.HttpsError('failed-precondition', 'An error happened, our team will look into it.')
    })
})

从我的前端开始,当我调用此函数时,它可能需要20-30秒才能完成并返回状态代码 . 这种延迟确实破坏了用户体验 .

什么是改善响应时间的最佳方法?

Testing

  • 从UI直接调用Firestore可以非常快速地解决 . 因此,问题似乎不太可能来自我们这边的DNS .

  • 来自UI的其他API调用Cloud Function,如“邀请朋友”,可以快速解决,并且不受此错误的影响 .

  • 调用其他 Cloud 函数,这些函数不返回Promise,但执行诸如使用Postmark发送电子邮件之类的操作,会受到此故障的影响,并且也会非常快速地解决 . 因此,问题似乎不是Firebase项目的位置( us-central1 ) . 虽然改变位置尚未经过测试 .

  • 故障仅发生在这一功能上 . 复制并粘贴在上面的那个 .

  • 受影响的函数和我们的其他函数之间的唯一区别是,具有该错误的函数返回Firestore操作的Promise .