larksuite / node-sdk

larksuite open sdk for nodejs
MIT License
136 stars 27 forks source link

TokanManager 中 Cache 过期时间要不要略低于得到的 expire #14

Closed shfshanyue closed 1 year ago

shfshanyue commented 1 year ago

https://github.com/larksuite/node-sdk/blob/main/client/token-manager.ts#L79

在配置 cache 时,过期时间是服务器得到的时间,但是请求 tenant_access_token 这个过程以及配置 cache 的这个过程也会浪费些许时间,导致 cache 晚于服务器的真正过期时间。

Bug 复现在我每天有 10 点和12点的两个定时任务,12点的定时任务,将拿到一个过期的 token 发送请求,请求失败。

期望:此时应该使得客户端 cache 的 token 过期删除,将会重新去拿新的 token。

所以需不需要配置过期时间时,比真正的过期时间短一点,或者可以配置参数传递。

const { tenant_access_token, expire } = await http
            .post(
                `${this.domain}/open-apis/auth/v3/tenant_access_token/internal`,
                {
                    app_id: this.appId,
                    app_secret: this.appSecret,
                }
            )
            .catch((e) => {
                this.logger.error(e);
            });

        await this.cache?.set(
            CTenantAccessToken,
            tenant_access_token,
            new Date().getTime() + expire * 1000
        );
mazhe-nerd commented 1 year ago

https://github.com/larksuite/node-sdk/blob/main/client/token-manager.ts#L79

在配置 cache 时,过期时间是服务器得到的时间,但是请求 tenant_access_token 这个过程以及配置 cache 的这个过程也会浪费些许时间,导致 cache 晚于服务器的真正过期时间。

Bug 复现在我每天有 10 点和12点的两个定时任务,12点的定时任务,将拿到一个过期的 token 发送请求,请求失败。 期望:此时应该使得客户端 cache 的 token 过期删除,将会重新去拿新的 token。

所以需不需要配置过期时间时,比真正的过期时间短一点,或者可以配置参数传递。

const { tenant_access_token, expire } = await http
            .post(
                `${this.domain}/open-apis/auth/v3/tenant_access_token/internal`,
                {
                    app_id: this.appId,
                    app_secret: this.appSecret,
                }
            )
            .catch((e) => {
                this.logger.error(e);
            });

        await this.cache?.set(
            CTenantAccessToken,
            tenant_access_token,
            new Date().getTime() + expire * 1000
        );

有道理同学~我今晚发版的时候修复下,提前3分钟(和golang的sdk对齐)。感谢~

mazhe-nerd commented 1 year ago

1.6.0版本已修复