如何减少对Googles Directory API的请求的总体执行时间?

时间:2019-04-24 22:07:55

标签: javascript node.js performance stream google-directory-api

初学者,使用nodejs环境。

我正在使用以下代码通过Googles Directory API从客户G Suite域读取所有用户帐户。我这样做是为了检查客户外部目录服务中是否存在每个帐户。该目录服务中不存在的帐户,稍后我将通过Directory API向Google发送删除请求。

G Suite域中大约有20000个用户,并且由于Google的限制,每个请求最多只能有500个用户帐户,因此将有40个请求。

我已经确定了整个过程的时间,每个请求大约要花2有时3秒才能收到Google的响应,因此,整个过程大约需要1.5分钟,有时甚至是2分钟。 循环响应对象并将每个用户帐户推送到googleUsers数组只需不到一毫秒的时间,因此现在不是问题。

我现在正在寻找减少总执行时间的方法,我最初的想法是是否有办法在下一个请求完成并传递所有数据之前将下一个请求触发到Google Directory API? 最好的情况是,我可以同时触发所有请求,从而减少执行时间。

除了我实际上还没有找到一种方法来做到这一点之外,我还看到一个问题,我需要上一个请求中的nextPageToken才能对下一组数据提出新的请求。

nodejs中的流能否解决我的问题,或者使该代码更高效的最佳方法是什么?

const getGoogleUsers = async () => {
  try {
    const client = new JWT(key.client_email, undefined, key.private_key,
      ["https://www.googleapis.com/auth/admin.directory.user"], "DOMAINADMIN"
    );
    timer('Authorizeing...');
    await client.authorize();
    timer('Done...');
    const g = new GoogleApis();
    const service = g.admin("directory_v1");
    timer('Getting first 500 users...');

    const res = await service.users.list({
      //'Accept-Encoding': 'gzip',
      auth: client,
      domain: 'DOMAIN',
      maxResults: 500,
      projection: 'full',
      fields: 'nextPageToken,users(id,primaryEmail,name,isAdmin,isDelegatedAdmin,lastLoginTime,creationTime,archived,orgUnitPath,customSchemas)'
    });
    timer('Done...');
    let googleUsers = new Array();
    timer('Looping first 500 users...');
    res.data.users.forEach(user => {
      googleUsers.push({
        'id': user.id,
        'primaryEmail': user.primaryEmail,
        'name': 'name' in user ? user.name.fullName : '',
        'isAdmin': user.isAdmin,
        'isDelegatedAdmin': user.isDelegatedAdmin,
        'lastLoginTime': user.lastLoginTime,
        'creationTime': user.creationTime,
        'archived': user.archived,
        'orgUnitPath': user.orgUnitPath,
        'EXTERNALSYSTEMUserId': 'customSchemas' in user ? user.customSchemas.EXTERNALSYSTEM.userId : null
      })
    });
    timer('Done...');

    //Retrieve all users if more than 500
    let nextPageToken = res.data.nextPageToken;
    if (nextPageToken || typeof nextPageToken !== 'undefined') {
      while (nextPageToken) {
        timer('Getting next 500 users...');
        const res = await service.users.list({
          auth: client,
          domain: 'DOMAIN',
          maxResults: 500,
          projection: 'full',
          pageToken: nextPageToken,
          fields: 'nextPageToken,users(id,primaryEmail,name,isAdmin,isDelegatedAdmin,lastLoginTime,creationTime,archived,orgUnitPath,customSchemas)',
        });
        timer('Done');
        timer('Looping next 500 users...');

        res.data.users.forEach(user => {
          googleUsers.push({
            'id': user.id,
            'primaryEmail': user.primaryEmail,
            'name': 'name' in user ? user.name.fullName : '',
            'isAdmin': user.isAdmin,
            'isDelegatedAdmin': user.isDelegatedAdmin,
            'lastLoginTime': user.lastLoginTime,
            'creationTime': user.creationTime,
            'archived': user.archived,
            'orgUnitPath': user.orgUnitPath,
            'EXTERNALSYSTEMUserId': 'customSchemas' in user ? user.customSchemas.EXTERNALSYSTEM.userId : null
          })
        });
        timer('Done');
        nextPageToken = res.data.nextPageToken;
      }
    }
    timer('All done getting google users');
    return googleUsers;

  } catch (err) {
    console.log(err);
  }
}

0 个答案:

没有答案