Google's official Cloud Spanner client library for Go has a NewClient()
function which accepts a Context
. Typically, Context
s 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.
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.