Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/nginx/grpc_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ namespace nginx {
// GRPC team to create an API for integrating libgrpc into arbitrary
// event loops.

namespace {
const std::chrono::seconds kShutdownTimeout(2);
}

std::weak_ptr<NgxEspGrpcQueue> NgxEspGrpcQueue::instance;

std::shared_ptr<NgxEspGrpcQueue> NgxEspGrpcQueue::Instance() {
Expand Down Expand Up @@ -125,7 +129,17 @@ void NgxEspGrpcQueue::NginxTagHandler(ngx_event_t *) {
void NgxEspGrpcQueue::WorkerThread(NgxEspGrpcQueue *queue) {
void *tag;
bool ok;
while (queue->cq_->Next(&tag, &ok)) {
while (true) {
auto status = queue->cq_->AsyncNext(
&tag, &ok, std::chrono::system_clock::now() + kShutdownTimeout);
if (status == ::grpc::CompletionQueue::NextStatus::SHUTDOWN ||
(status == ::grpc::CompletionQueue::NextStatus::TIMEOUT &&
queue->shutting_down_)) {
break;
}
if (status == ::grpc::CompletionQueue::NextStatus::TIMEOUT) {
continue;
}
std::unique_ptr<Tag> cb(static_cast<Tag *>(tag));
if (cb) {
bool notify_nginx = false;
Expand All @@ -147,7 +161,9 @@ void NgxEspGrpcQueue::WorkerThread(NgxEspGrpcQueue *queue) {
void NgxEspGrpcQueue::Deleter(NgxEspGrpcQueue *lib) { delete lib; }

NgxEspGrpcQueue::NgxEspGrpcQueue()
: cq_(new ::grpc::CompletionQueue()), notified_(false) {
: cq_(new ::grpc::CompletionQueue()),
notified_(false),
shutting_down_(false) {
worker_thread_ = std::thread(&NgxEspGrpcQueue::WorkerThread, this);
}

Expand Down Expand Up @@ -176,6 +192,10 @@ NgxEspGrpcQueue::~NgxEspGrpcQueue() {

cq_->Shutdown();

// TODO: This flag is a temporary workaround to force shutdown
// completing queue. To be removed.
shutting_down_ = true;

// N.B. Joining on the worker thread is essential, as that thread
// maintains a raw pointer to this datastructure.
worker_thread_.join();
Expand Down
1 change: 1 addition & 0 deletions src/nginx/grpc_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class NgxEspGrpcQueue : public AsyncGrpcQueue {
std::unique_ptr<::grpc::CompletionQueue> cq_;
std::deque<Finalizer> pending_;
bool notified_;
bool shutting_down_;

std::thread worker_thread_;
};
Expand Down
5 changes: 2 additions & 3 deletions src/nginx/t/grpc_interop_cancel.t
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ my $ServiceControlPort = ApiManager::pick_port();
my $GrpcBackendPort = ApiManager::pick_port();
my $HttpBackendPort = ApiManager::pick_port();

my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(4);
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(5);

$t->write_file(
'service.pb.txt',
Expand Down Expand Up @@ -85,8 +85,7 @@ is($t->waitforsocket("127.0.0.1:${Http2NginxPort}"), 1, 'Nginx socket ready.');

################################################################################
my @test_cases = (
# Temporary disabled per b/35314304
# 'cancel_after_begin',
'cancel_after_begin',
'cancel_after_first_response',
);

Expand Down