使用 Firebase Admin SDK 更新用户时超出配额 - 我如何请求更多?

Quota exceeded when updating users with Firebase Admin SDK - how can I request more?

当整个租户需要 enabled/disabled 时,我们尝试更新多租户设置中的许多用户。有了这个,我们有时会 运行 进入 QUOTA_EXCEEDED 错误,特别是对于拥有许多用户的大型租户。我在这里问过如何更好地做到这一点:

由于目前有 ,我该如何申请提高配额?

如何提高配额?

firebaser 在这里

如果您 运行 遇到无法从 Firebase 控制台更改的配额限制,reach out to Firebase support 寻求个性化帮助。他们通常可以帮助暂时提高配额,这也是我们的工程团队需要一项功能的重要指标。

对于遇到同样问题的每个人,这里有一个解决方案。

我联系了 firebase 支持并得到了这个答案

If you try to update a user’s information at once (more than 10 users or requests per second), then you'll be encountering this error message "Exceeded quota for updating account information". Please note that when we get a huge amount of requests in a short period of time, throttling is applied automatically to protect our servers. You may try sending the requests sequentially (distribute the load) instead of sending them in parallel. So basically, you'll need to process them one at a time (instead of all at once) in order to avoid the error.

所以我用每 100 毫秒发送一条单独记录的超时解决了这个问题

function listAllUsers(nextPageToken) {
        let timeout = 0;
        admin.auth().listUsers(1000, nextPageToken)
            .then(function (listUsersResult) {
                listUsersResult.users.forEach(function (userRecord) {
                    timeout = timeout + 100
                    nextUser(userRecord.uid, timeout)
                });
                if (listUsersResult.pageToken) {
                    listAllUsers(listUsersResult.pageToken);
                }
            })
            .catch(function (error) {
                console.log('Error listing users:', error);
            });
    }
    listAllUsers();

下一个用户函数:

function nextUser(uid, timeout) { 
        setTimeout(() => {
            admin.auth().setCustomUserClaims(uid, { client: true }).then(() => {
            }).catch(function (error) {
                console.log('Error setting user:', error);
            });
        }, timeout);
    }

我 运行 在为所有用户更新自定义身份验证声明时遇到了上述相同问题。我能够通过这种 运行 顺序更新的替代实现来解决它,而不依赖于超时。

这会在执行下一次更新之前等待先前的承诺解决

try {
    const promises = await IterateAllUsers();
    await promises.reduce(async (previousPromise, nextAsyncFunctionPromise) => {
        await previousPromise;
        const nextAsyncFunction = await nextAsyncFunctionPromise
        // Actual execution
        await nextAsyncFunction();
    }, Promise.resolve());
    console.log('done');
} catch(error) {
    console.error(error);
}

无论你必须迭代什么,但它至少应该 return 一个函数的承诺数组,以便稍后执行

async function IterateAllUsers(): Promise<Array<Promise<Function>>> {
    const promises: Array<Promise<Function>> = [];
    await // Iterate Authentication Users or Database Collection etc.
    promises.push(updateAuthCustomClaims(user.key, someValue));    
    return Promise.resolve(promises);
}

用稍后执行的任务填充数组的高阶函数

async function updateAuthCustomClaims(uid: string, someValue): Promise<Function> {
    return async () => {
        try {
            await admin.auth().setCustomUserClaims(uid,{someValue});
            console.log('Updated user: ', uid);
        } catch(error) {
            console.warn('Could not add custom claim due to: ', error);
        }
        return Promise.resolve();
    };
}