rethinkdb-ts icon indicating copy to clipboard operation
rethinkdb-ts copied to clipboard

r.connectPool not waiting for connection/not blocking the rest.

Open yaneony opened this issue 5 years ago • 4 comments

My issue is referring to another RethinkDB library, rethinkdbdash and how the pool connection was made. My problem is that we use self-invoking/self-calling functions, and there where problem occur.

Following code work just fine, because self-invoked function is not being called before rethinkdb connection is established.

const rethinkdbdash = require('rethinkdbdash');
const r = rethinkdbdash({ host: '127.0.0.1', port: 28015, db: 'mydb', silent: true });

(async function getData() {
  var result = await r.table('items').run();
  console.log(result); // process data here
  getData()
})();

But, it's not working with rethinkdb-ts because you'll have to call it as await within async function. Putting await r.connectPool inside self-invoking function isn't probably a good move, because it will be called on each run.

const { r } = require('rethinkdb-ts');

(async function getData() {
  await r.connectPool({ host: '127.0.0.1', port: 28015, db: 'hexaone', silent: true });
  var result = await r.table('items').run();
  getData();
})();

Following code will not even work, because self-invoked function is called much earlier that connection is established.

const { r } = require('rethinkdb-ts');

r.connectPool({ host: '127.0.0.1', port: 28015, db: 'hexaone', silent: true });

(async function getData() {
  var result = await r.table('items').run();
  getData();
})();

Following error will be thrown:

ReqlDriverError: None of the pools have an opened connection and failed to open a new one.

And there is another point i wondering about:

and failed to open a new one

So, it there some way to maky it work like it was with rethinkdbdash? Block whole execution of script before db connection is established? And why it's not opening new connections as well?

Thanks!

yaneony avatar Jan 09 '20 11:01 yaneony

I don't think rethinkdbdash blocks whole execution, more like queues up queries and then starts to return them after connection is established. But I'm not sure since I haven't tested something like this in there

McSneaky avatar Jan 09 '20 11:01 McSneaky

@McSneaky running simple test:

console.log('1');
const rethinkdbdash = require('rethinkdbdash');

console.log('2');
const r = rethinkdbdash({ host: '127.0.0.1', port: 28015, db: 'hexaone', silent: true });

console.log('3');

(async function() {
  console.log('4');
})();

(async function getData() {
  console.log('5');
  await r.table('items').run();
  console.log('6');
})();

Outputs following:

1
2
3
4
5
... after some delay
6

Since self-invoking functions are called immediately, i think that rethinkdbdash is waiting for connection/blocking execution after... Running same code with rethinkdb-ts will throw an error above...

yaneony avatar Jan 09 '20 11:01 yaneony

I can externalize the waitForHealthy function:

r.connectPool({
  host: '127.0.0.1',
  port: 28015,
  db: 'test',
  silent: true,
  waitForHealthy: false // this will ignore initial pool initialization errors so you don't have to await them
});
(async function getData() {
  await (r.getPoolMaster() as any).waitForHealthy(); // this will actually not do anything if the pool is healthy, and if it's not it will just wait for the first connection to be available
  const result = await r.table('test').run();
  console.log(JSON.stringify(result));
})();

This is actually a feature, not a bug, I wanted to know ahead of time if the pool is connecting or not, the rethinkdbdash connect doesn't tell you on initialization weather it works or not, you have to wait for the first query... this way you can initialize the app and handle it better if you have no connection to the db on start. I also didn't want to make all connection stuck and instead fail fast that's why I didn't encorporate it in the run, probably can add a variable await r.table('test').run({waitForHealthy: true}) or r.connectPool({ host: '127.0.0.1', port: 28015, db: 'test', silent: true, waitForHealthyByDefault: true })

ronzeidman avatar Jan 12 '20 19:01 ronzeidman

// Update: I think my comment is slightly different and will make a seperate issue.

mishawakerman avatar Jun 19 '20 03:06 mishawakerman