在不同的Node.js进程之间共享令牌 – 最佳方法?

huangapple go评论66阅读模式
英文:

Share token between different Node.js processes - best approach?

问题

Node.js v16.20.0
基于 pm2 或 Docker 的进程处理,包括 4-16 个进程

在我们的用例中,我们希望从我们的服务器应用程序与 Rest/JSON API(例如 openverse、wikipedia、flickr、pixabay 等)建立 API 连接。

通过使用 clientId/clientSecret 进行请求,我们会获得一个有效期约为一小时的令牌。然后,可以使用此令牌来进行具体的搜索或 CRUD 操作。

我们希望令牌的请求由最多一个 Node.js 进程执行,以及在令牌已过期时重新获取操作。这是因为一些 API 不喜欢多个客户端并行地多次请求令牌。

您对如何在多个 Node.js 进程之间共享此类令牌有什么方法?其他进程可能需要等待令牌被获取。

  • Redis?
  • worker_threads?
  • LevelDB?
  • 数据库(基于 SQL 等)?

谢谢和亲切的问候。

英文:

Node.js v16.20.0
pm2 OR docker based process handling with 4-16 processes

In our use case, we want to establish an API connection from our server app to a Rest/JSON API (e.g. openverse, wikipedia, flickr, pixabay, etc).

For the request using a clientId/clientSecret, we get a token that is valid for about one hour. This token can then be used to make concrete requests such as searches or CRUD operations.

We want the request for a token to be carried out by maximum one Node.js process, as well as the refetch operation if a token has expired. We want this because some APIs don't like it when tokens are requested multiple times in parallel for the same client.

What is your approach to share such a token between multiple Node.js processes? Other processes may have to wait until a token has been fetched.

  • Redis ?
  • worker_threads ?
  • LevelDB?
  • database (SQL-based, etc.)?

Thanks and kind regards

答案1

得分: 0

我们最终决定使用Redis和Node-Redis作为一个包。效果很好。与此同时,我们也使用Redis进行缓存。

[1] https://www.npmjs.com/package/redis

英文:

We finally decided to use Redis and Node-Redis [¹] as a package. Works great. In the meantime we also use Redis for caching.

[1] https://www.npmjs.com/package/redis

huangapple
  • 本文由 发表于 2023年7月3日 17:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定