Skip to content
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

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

Open
yaneony opened this issue Jan 9, 2020 · 4 comments
Open

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

yaneony opened this issue Jan 9, 2020 · 4 comments
Milestone

Comments

@yaneony
Copy link

yaneony commented Jan 9, 2020

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!

@McSneaky
Copy link

McSneaky commented Jan 9, 2020

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

@yaneony
Copy link
Author

yaneony commented Jan 9, 2020

@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...

@ronzeidman
Copy link

ronzeidman commented Jan 12, 2020

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 })

@mishawakerman
Copy link

mishawakerman commented Jun 19, 2020

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

@atassis atassis added this to the Version 3.0 milestone May 11, 2021
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

5 participants