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

Concurrent readAllItems requests #76

Open
KamalAman opened this issue Mar 20, 2019 · 2 comments
Open

Concurrent readAllItems requests #76

KamalAman opened this issue Mar 20, 2019 · 2 comments

Comments

@KamalAman
Copy link

Two simultaneous requests to readAllItems should currently resolve their callbacks.

However since nodes7 uses the contextual readDoneCallback, the second call to readAllItems interferes with the response with the first request. This results in the first requests callback to never get called, and the second requests callback to get called twice.

client.readAllItems((err, values) => {
  console.log(1);
});

client.readAllItems((err, values) => {
  console.log(2);
});

// 2
// 2
@gfcittolin
Copy link
Collaborator

Indeed. Unfortunately, the library currently can't handle requests like this, and therefore you'd need to synchronize on the outside with something like (please note this hasn't been tested):

var queue = [];
var currentCb = null;

function _processQueue() {
  if (currentCb) return; //there's a request in progress, skip
  currentCb = queue.splice(0,1);
  if (!currentCb) return; //there's nothing to request, skip
  client.readAllItems((err, values) => {
    currentCb(err, values);
    currentCb = null;
    _processQueue();
  });
}

function asyncReadAllItems(cb) {
  queue.push(cb);
  _processQueue():
}

There's a rework of the library going on that will fix this and other issues. So maybe we'll have to workaround until that work is done :)

@LiaraT
Copy link

LiaraT commented Apr 20, 2021

Hello. Could you write a slightly more detailed example of how to work with two data queries? I'm trying to make 2 requests for different data (twice I write different data to "conn. addItems", and twice I try to read "readAllItems"). As we wrote above, only the second request is executed.

I apologize for the mistakes, English is not my native language, so I use a translator.
Thank you in advance

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

3 participants