每次请求都应该创建一个新的Cloud Spanner Client实例吗?

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

Should a new Cloud Spanner Client instance be created on every request?

问题

Google官方为Go语言提供了Cloud Spanner的客户端库,其中包含一个NewClient()函数,该函数接受一个Context参数。通常,Context会作为一个(可能是长时间运行的)请求链的一部分传递。这是否意味着NewClient()函数应该在每个需要访问Cloud Spanner的服务处理的唯一请求中被调用?

如果我在使用传统的关系型数据库,我会创建一个共享的客户端池,以便进程可以多次使用它们,以减少资源使用(例如网络连接)。每当请求处理程序启动时创建一个唯一的数据库客户端的想法让我有些困惑,我想知道我是否误解了客户端库的预期用法。

另一方面,Cloud Spanner在一般情况下确实有一些神奇之处,所以如果我得知这实际上是一种被鼓励和高效的模式,我也不会感到太惊讶。不过,确切的情况还是很好知道的,而文档中并没有详细讨论这个问题。

英文:

Google's official Cloud Spanner client library for Go has a NewClient() function which accepts a Context. Typically, Contexts are passed around as part of a (potentially long-running) request chain. Does this imply that NewClient() is intended to be invoked for each unique request handled by a service which needs to access Cloud Spanner?

If I were using a traditional relational database, I'd be creating a shared pool of clients to be used many times by a process in order to cut down on resource usage (e.g. network connections). The idea of creating a unique database client whenever a request handler kicks off is a little off-putting to me, and I wonder if I'm misunderstanding the intended usage of the client library.

On the other hand, Cloud Spanner is rather magical in general, so it wouldn't surprise me too much to learn that this is in fact an encouraged and efficient pattern. Still, it'd be nice to know for sure, and the docs don't really discuss it.

答案1

得分: 2

这是否意味着每个需要访问Cloud Spanner的服务处理的唯一请求都应该调用NewClient()?

不是的。将客户端视为与数据库的连接。每个连接可以处理多个请求,通常在应用程序初始化期间创建,并在应用程序关闭或不再需要数据库时关闭。

您可以拥有多个客户端(连接),因此如果需要,可以并行处理请求。

英文:

> Does this imply that NewClient() is intended to be invoked for each
> unique request handled by a service which needs to access Cloud
> Spanner?

No. Think of a client as a connection to the database. Each connection can service many requests and is typically created at during application initialization and closed when your application closes or no longer needs a database.

You can have many clients (connections) so you can process requests in parallel, if needed.

huangapple
  • 本文由 发表于 2017年5月9日 11:13:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/43860449.html
匿名

发表评论

匿名网友

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

确定