8000 Reusing connections · Issue #393 · influxdata/influxdb-client-js · GitHub
[go: up one dir, main page]

Skip to content

Reusing connections #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xjohnp00 opened this issue Dec 2, 2021 · 2 comments
Closed

Reusing connections #393

xjohnp00 opened this issue Dec 2, 2021 · 2 comments
Assignees
Milestone

Comments

@xjohnp00
Copy link
xjohnp00 commented Dec 2, 2021

Hi,
Is there a way to reuse the connection and still flush the buffer (using JavaScript on AWS)? I tried to provide a custom connection header (Connection: Keep-Alive) and it did not work. The connection was closed anyway (I tested this on local InfluxDB 2.0.8 in docker). Does this behavior differ between InfluxDB OSS and Cloud?
I am currently creating the instance of write api like this

const writeApi = this.client.getWriteApi(this.org, this._bucket, "s");

And flushing written points using

writeApi.flush();

I tried providing Connection: Keep-Alive header by passing my WriteOptions to InfluxDB.getWriteApi()
Thanks.

@sranka
Copy link
Contributor
sranka commented Dec 3, 2021

Thank you @xjohnp00 for using the client and posting an issue. Node.js HTTP client OOTB does not reuse connections, you can change the default with a help of an Agent. Your client code would then look like this:

const {Agent} = require('http')
...
const agent = new Agent({
  keepAlive: true,
  keepAliveMsecs: 20 * 1000, // 20 seconds keep alive
})
const writeApi = new InfluxDB({
  url,
  token,
  transportOptions: {agent},
}).getWriteApi(org, bucket, 'ns')
...
// It is good practice, to destroy() an Agent instance when it is no longer in use,
// because unused sockets consume OS resources.
// process.on( 'SIGINT', function() {
//  agent.destroy()
// })

There is no need to specify the Connection header, the agent does it. I double-checked it with a modified write.js example and a fake HTTP server:

const express = require('express')
const port = 8086
const app = express()
app.post('/api/v2/write', (req, resp) => {
  console.log(req.path, req.headers)
  resp.sendStatus(204)
})
app.listen(port, () => {
  console.log(`listening on http://localhost:${port}`)
})

prints:

/api/v2/write {
  'content-type': 'text/plain; charset=utf-8',
  'user-agent': 'influxdb-client-js/1.21.0',
  authorization: 'Token my-token',
  'content-length': '159',
  host: 'localhost:8086',
  connection: 'keep-alive'
}

@sranka sranka self-assigned this Dec 3, 2021
@sranka sranka added this to the 1.22.0 milestone Dec 3, 2021
@xjohnp00
Copy link
Author
xjohnp00 commented Dec 3, 2021

Great, works flawlessly.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0