-
Notifications
You must be signed in to change notification settings - Fork 208
reuse connections on table.insert #41
Description
TL;DR: the current implementation of BigQuery establishes a new connections on each table.insert() call. This causes problems for Cloud Functions because of connection quota that our users hit. Please update the client libraries to reuse connections.
Why new connections cause problems:
Cloud Functions is a serverless platform for executing snippets of code. Many our customers use function to store events in Big Query. GCF has a quota on connections, and this quota is relatively low because each connection requires a NAT port to be allocated and maintained until TCP packet timeout; and there are only 32k ports per server.
Why BigQuery is worse than other client libraries:
I found three types of clients:
- Clients that always reuse connections - even if we construct the client object in a local scope - @google-cloud/storage
- Clients that reuse connectoins only if we declare the client object at global scope - @google-cloud/pubsub, @google-cloud/language, @google-cloud/spanner
- Clients that never reuse connectoins - @google-cloud/bigquery
It would be nice if all libraries worked as 1.
How to reproduce the problem:
Every call to the function exported by the code below creates a new connection.
******************* function.js ****************
const bigquery = require('@google-cloud/bigquery')();
const table = bigquery.dataset('Tests').table('RandomNumbers');
exports.function = function(req, res) {
var r = Math.floor(Math.random() * 100);
console.log("Generated random number " + r);
var row = {number: r};
table.insert(row, function(err, apiResponse) {
if (err) {
result = 'Error inserting data to bigquery';
console.log(result + ": " + JSON.stringify(err, null, 2));
res.status(500).send("Error" + err);
} else {
res.status(200).send("" + r);
}
});
};************* package.json ****************
{
"dependencies": {
"@google-cloud/bigquery": "^0.10.0"
}
}We also found that BigQuery uses IPv4, while other libraries prefer IPv6. This should also be fixed.
[Googlers: internal tracking id b/68240537]