C++: Can I delete/reset/cancel an existing clientcontext deadline to set new clientcontext deadline?

595 views
Skip to first unread message

Rohan Chawhan

unread,
Apr 19, 2019, 9:28:00 AM4/19/19
to grpc.io
Hey All,

I am using C++ to communicate with google speech api server.

I am using the usual approach of setting a deadline for the clientcontext to handle timeout functionality.

I have put the deadlines like:
void SetContextDeadline(grpc::ClientContext *context, unsigned int user_defined_deadline){
context->set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(user_defined_deadline));
}

Now, this function is called whenever I want to set the deadline from now(time)
int main(){
  auto creds = grpc::GoogleDefaultCredentials();
  auto channel = grpc::CreateChannel("speech.googleapis.com", creds);
  std::unique_ptr<Speech::Stub> speech(Speech::NewStub(channel));

  grpc::ClientContext context;

// Set timeout of 8 seconds => also this is the timeout which I am not able to cancel to set new timer
// Whenever it ends (after 8 seconds) my program is terminated
  SetContextDeadline(&context,8);
  auto streamer = speech->StreamingRecognize(&context);

 // The microphone thread writes the audio content.
  std::thread microphone_thread(&MicrophoneThreadMain, streamer.get(),
                                file_path);
// Read responses.
  StreamingRecognizeResponse response;
  while (streamer->Read(&response)) {  // Returns false when no more to read.
    // Now I want to cancel the previous deadline to set this new deadline, everytime I enter this loop
    // I am seeing that this is setting the new deadline but the program still terminates at the older timeout
    SetContextDeadline(&context,8);

    // Dump the transcript of all the results.
    // Implementation of response display     
  }
}



Thanks in advance for your help

Sidhartha Thota

unread,
Apr 19, 2019, 2:56:49 PM4/19/19
to Rohan Chawhan, grpc.io
If I am not wrong, clientContext cannot be applied for streaming APIs. Thats what I saw from my experience. But I am happy to know if there is solution for this.

Matrix Comsec Pvt. Ltd.
#394, GIDC Makarpura - 390010. Dist: Vadodara, Gujarat, India
Matrix Telecom | Security
IP Video Surveillance | People Mobility Management | Unified Communications
1 Million+ Customers | 2,500+ Partners | 60+ Products | 50+ Countries
We put more in the box, so your business can think more out of the box.
Facebooklinkedintwitteryoutube

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/d8e3fbb2-ef13-4c97-8ed3-57c1c58486c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Thanks,
Sidhartha Thota.

Rohan Chawhan

unread,
Apr 20, 2019, 2:28:23 AM4/20/19
to grpc.io
Hi Sidhartha,
So is there another way for applying timeouts on streaming APIs? specially in the given context? I basically want to solve the internet connection error for my code.
To unsubscribe from this group and stop receiving emails from it, send an email to grp...@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/d8e3fbb2-ef13-4c97-8ed3-57c1c58486c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Thanks,
Sidhartha Thota.

Sidhartha Thota

unread,
Apr 20, 2019, 6:43:58 PM4/20/19
to Rohan Chawhan, grpc.io
We used ClientWriter in our project and write() method fails if the other end/network is down. Check if read() will bailout in case of down event.

I am not aware of any other solution.

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

For more options, visit https://groups.google.com/d/optout.


--
Thanks,
Sidhartha Thota.
Reply all
Reply to author
Forward
0 new messages