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

Wrong response from previous request using future or eventbase #717

Closed
kapcino opened this issue Nov 29, 2017 · 1 comment
Closed

Wrong response from previous request using future or eventbase #717

kapcino opened this issue Nov 29, 2017 · 1 comment

Comments

@kapcino
Copy link

kapcino commented Nov 29, 2017

I created a server using Proxygen based on EchoServer, since it is most based on folly, I post here for help.

The basic procedure to receive a request from a client, and then I create a thread to fetch data from db. After the data is fetched, send the response back to the client.

onEOM() {
    auto eventBase = folly::EventBaseManager::get()->getEventBase();
    thread t([&, eventBase]() {

        auto d = fetch(); // call function to fetch data from db

        eventBase->runInEventBaseThread([&]() {
            sendResponse(d);
          });
});

 t.detach();
}

It works well for slow requests, but when I want to use wrk for benchmark, I found a problem.

Say the service is like ip2location, I use wrk to benchmark, like this:

wrk -t1 -c100 -d2m  'http://127.0.0.1:80/test?q=8.8.8.8'

Before it finishes, interrupt with Ctrl+C, and then do manual request with curl, like this:

curl -sv 'http://127.0.0.1:80/test?q=4.4.4.4'

The expected result for curl is for 4.4.4.4, but the actual result is for 8.8.8.8.

After several retry, it some times return the correct info, but most times are incorrect.

I tried to use Future and then pattern, but it has the same problem.

Future<string> getData(const string& ip) {
  Promise<string> promise;
  auto f = promise.getFuture();
  auto ev = EventBaseManager::get()->getEventBase();
      ev->runInLoop([promise = std::move(promise), &]() mutable {
      auto d = fetch();
      promise.setValue(d);
  });
  return f;
}

I also tried to set the idleTimeout to 100ms, and it still exists!

options.idleTimeout = std::chrono::milliseconds(100);

I tried to add flag in onError function, but it seems never called when I sends Ctrl+C.

I also tried to use folly::async to get a Future, and use the then pattern, but it only works with one CPUExecutor, if I set more than one, there will be crash saying runInEventBaseThread not the request one.

Please help to resolve this problem, thanks.

@kapcino
Copy link
Author

kapcino commented Nov 29, 2017

Just found the bug in my own code, 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

1 participant