Beispiel #1
0
def _abort(state, call, code, details):
  if state.client is not _CANCELLED:
    effective_code = _abortion_code(state, code)
    effective_details = details if state.details is None else state.details
    if state.initial_metadata_allowed:
      operations = (
          cygrpc.operation_send_initial_metadata(
              _EMPTY_METADATA, _EMPTY_FLAGS),
          cygrpc.operation_send_status_from_server(
              _common.cygrpc_metadata(state.trailing_metadata), effective_code,
              effective_details, _EMPTY_FLAGS),
      )
      token = _SEND_INITIAL_METADATA_AND_SEND_STATUS_FROM_SERVER_TOKEN
    else:
      operations = (
          cygrpc.operation_send_status_from_server(
              _common.cygrpc_metadata(state.trailing_metadata), effective_code,
              effective_details, _EMPTY_FLAGS),
      )
      token = _SEND_STATUS_FROM_SERVER_TOKEN
    call.start_server_batch(
        cygrpc.Operations(operations),
        _send_status_from_server(state, token))
    state.statused = True
    state.due.add(token)
Beispiel #2
0
def _abort(state, call, code, details):
  if state.client is not _CANCELLED:
    effective_code = _abortion_code(state, code)
    effective_details = details if state.details is None else state.details
    if state.initial_metadata_allowed:
      operations = (
          cygrpc.operation_send_initial_metadata(
              _EMPTY_METADATA, _EMPTY_FLAGS),
          cygrpc.operation_send_status_from_server(
              _common.cygrpc_metadata(state.trailing_metadata), effective_code,
              effective_details, _EMPTY_FLAGS),
      )
      token = _SEND_INITIAL_METADATA_AND_SEND_STATUS_FROM_SERVER_TOKEN
    else:
      operations = (
          cygrpc.operation_send_status_from_server(
              _common.cygrpc_metadata(state.trailing_metadata), effective_code,
              effective_details, _EMPTY_FLAGS),
      )
      token = _SEND_STATUS_FROM_SERVER_TOKEN
    call.start_server_batch(
        cygrpc.Operations(operations),
        _send_status_from_server(state, token))
    state.statused = True
    state.due.add(token)
Beispiel #3
0
 def _trailing_metadata(self):
     from google.protobuf.duration_pb2 import Duration
     from google.rpc.error_details_pb2 import RetryInfo
     from grpc._common import cygrpc_metadata
     if self._commit_abort_retry_nanos is None:
         return cygrpc_metadata(())
     retry_info = RetryInfo(
         retry_delay=Duration(seconds=self._commit_abort_retry_seconds,
                              nanos=self._commit_abort_retry_nanos))
     return cygrpc_metadata([('google.rpc.retryinfo-bin',
                              retry_info.SerializeToString())])
Beispiel #4
0
 def __call__(self,
              request_iterator,
              timeout=None,
              metadata=None,
              credentials=None):
     deadline, deadline_timespec = _deadline(timeout)
     state = _RPCState(_STREAM_STREAM_INITIAL_DUE, None, None, None, None)
     call, drive_call = self._managed_call(None, 0, self._method, None,
                                           deadline_timespec)
     if credentials is not None:
         call.set_credentials(credentials._credentials)
     event_handler = _event_handler(state, call,
                                    self._response_deserializer)
     with state.condition:
         call.start_client_batch(
             cygrpc.Operations(
                 (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
                  )), event_handler)
         operations = (
             cygrpc.operation_send_initial_metadata(
                 _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
         )
         call_error = call.start_client_batch(cygrpc.Operations(operations),
                                              event_handler)
         if call_error != cygrpc.CallError.ok:
             _call_error_set_RPCstate(state, call_error, metadata)
             return _Rendezvous(state, None, None, deadline)
         drive_call()
         _consume_request_iterator(request_iterator, state, call,
                                   self._request_serializer)
     return _Rendezvous(state, call, self._response_deserializer, deadline)
Beispiel #5
0
 def _blocking(self, request_iterator, timeout, metadata, credentials):
     deadline, deadline_timespec = _deadline(timeout)
     state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
     completion_queue = cygrpc.CompletionQueue()
     call = self._channel.create_call(None, 0, completion_queue,
                                      self._method, None, deadline_timespec)
     if credentials is not None:
         call.set_credentials(credentials._credentials)
     with state.condition:
         call.start_client_batch(
             cygrpc.Operations(
                 (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
                  )), None)
         operations = (
             cygrpc.operation_send_initial_metadata(
                 _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
             cygrpc.operation_receive_message(_EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
         )
         call_error = call.start_client_batch(cygrpc.Operations(operations),
                                              None)
         _check_call_error(call_error, metadata)
         _consume_request_iterator(request_iterator, state, call,
                                   self._request_serializer)
     while True:
         event = completion_queue.poll()
         with state.condition:
             _handle_event(event, state, self._response_deserializer)
             state.condition.notify_all()
             if not state.due:
                 break
     return state, deadline
Beispiel #6
0
 def _blocking(self, request_iterator, timeout, metadata, credentials):
   deadline, deadline_timespec = _deadline(timeout)
   state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
   completion_queue = cygrpc.CompletionQueue()
   call = self._channel.create_call(
       None, 0, completion_queue, self._method, None, deadline_timespec)
   if credentials is not None:
     call.set_credentials(credentials._credentials)
   with state.condition:
     call.start_client_batch(
         cygrpc.Operations(
             (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
         None)
     operations = (
         cygrpc.operation_send_initial_metadata(
             _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
         cygrpc.operation_receive_message(_EMPTY_FLAGS),
         cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
     )
     call.start_client_batch(cygrpc.Operations(operations), None)
     _consume_request_iterator(
         request_iterator, state, call, self._request_serializer)
   while True:
     event = completion_queue.poll()
     with state.condition:
       _handle_event(event, state, self._response_deserializer)
       state.condition.notify_all()
       if not state.due:
         break
   return state, deadline
Beispiel #7
0
 def __call__(self, request, timeout=None, metadata=None, credentials=None):
   deadline, deadline_timespec, serialized_request, rendezvous = (
       _start_unary_request(request, timeout, self._request_serializer))
   if serialized_request is None:
     raise rendezvous
   else:
     state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None, None)
     call = self._create_managed_call(
         None, 0, self._method, None, deadline_timespec)
     if credentials is not None:
       call.set_credentials(credentials._credentials)
     event_handler = _event_handler(state, call, self._response_deserializer)
     with state.condition:
       call.start_client_batch(
           cygrpc.Operations(
               (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
           event_handler)
       operations = (
           cygrpc.operation_send_initial_metadata(
               _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
           cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
           cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
           cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
       )
       call.start_client_batch(cygrpc.Operations(operations), event_handler)
     return _Rendezvous(state, call, self._response_deserializer, deadline)
Beispiel #8
0
 def __call__(self, request, timeout=None, metadata=None, credentials=None):
     deadline, deadline_timespec, serialized_request, rendezvous = (
         _start_unary_request(request, timeout, self._request_serializer))
     if serialized_request is None:
         raise rendezvous
     else:
         state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None,
                           None)
         call = self._create_managed_call(None, 0, self._method, None,
                                          deadline_timespec)
         if credentials is not None:
             call.set_credentials(credentials._credentials)
         event_handler = _event_handler(state, call,
                                        self._response_deserializer)
         with state.condition:
             call.start_client_batch(
                 cygrpc.Operations(
                     (cygrpc.operation_receive_initial_metadata(
                         _EMPTY_FLAGS), )), event_handler)
             operations = (
                 cygrpc.operation_send_initial_metadata(
                     _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
                 cygrpc.operation_send_message(serialized_request,
                                               _EMPTY_FLAGS),
                 cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
                 cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
             )
             call.start_client_batch(cygrpc.Operations(operations),
                                     event_handler)
         return _Rendezvous(state, call, self._response_deserializer,
                            deadline)
 def _invoke_success(self, metadata):
     try:
         cygrpc_metadata = _common.cygrpc_metadata(metadata)
     except Exception as error:
         self._invoke_failure(error)
         return
     self.cygrpc_callback(cygrpc_metadata, cygrpc.StatusCode.ok, b'')
Beispiel #10
0
 def __call__(self,
              request_iterator,
              timeout=None,
              metadata=None,
              credentials=None):
     deadline, deadline_timespec = _deadline(timeout)
     state = _RPCState(_STREAM_STREAM_INITIAL_DUE, None, None, None, None)
     call, drive_call = self._managed_call(None, 0, self._method, None,
                                           deadline_timespec)
     if credentials is not None:
         call.set_credentials(credentials._credentials)
     event_handler = _event_handler(state, call, self._response_deserializer)
     with state.condition:
         call.start_client_batch(
             cygrpc.Operations(
                 (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
             event_handler)
         operations = (
             cygrpc.operation_send_initial_metadata(
                 _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),)
         call_error = call.start_client_batch(
             cygrpc.Operations(operations), event_handler)
         if call_error != cygrpc.CallError.ok:
             _call_error_set_RPCstate(state, call_error, metadata)
             return _Rendezvous(state, None, None, deadline)
         drive_call()
         _consume_request_iterator(request_iterator, state, call,
                                   self._request_serializer)
     return _Rendezvous(state, call, self._response_deserializer, deadline)
Beispiel #11
0
def _status(rpc_event, state, serialized_response):
    with state.condition:
        if state.client is not _CANCELLED:
            trailing_metadata = _common.cygrpc_metadata(
                state.trailing_metadata)
            code = _completion_code(state)
            details = _details(state)
            operations = [
                cygrpc.operation_send_status_from_server(
                    trailing_metadata, code, details, _EMPTY_FLAGS),
            ]
            if state.initial_metadata_allowed:
                operations.append(
                    cygrpc.operation_send_initial_metadata(
                        _common.EMPTY_METADATA, _EMPTY_FLAGS))
            if serialized_response is not None:
                operations.append(
                    cygrpc.operation_send_message(serialized_response,
                                                  _EMPTY_FLAGS))
            rpc_event.operation_call.start_server_batch(
                cygrpc.Operations(operations),
                _send_status_from_server(state,
                                         _SEND_STATUS_FROM_SERVER_TOKEN))
            state.statused = True
            state.due.add(_SEND_STATUS_FROM_SERVER_TOKEN)
Beispiel #12
0
 def _invoke_success(self, metadata):
     try:
         cygrpc_metadata = _common.cygrpc_metadata(metadata)
     except Exception as error:
         self._invoke_failure(error)
         return
     self.cygrpc_callback(cygrpc_metadata, cygrpc.StatusCode.ok, b'')
Beispiel #13
0
 def send_initial_metadata(self, initial_metadata):
   with self._state.condition:
     if self._state.client is _CANCELLED:
       _raise_rpc_error(self._state)
     else:
       if self._state.initial_metadata_allowed:
         operation = cygrpc.operation_send_initial_metadata(
             _common.cygrpc_metadata(initial_metadata), _EMPTY_FLAGS)
         self._rpc_event.operation_call.start_server_batch(
             cygrpc.Operations((operation,)),
             _send_initial_metadata(self._state))
         self._state.initial_metadata_allowed = False
         self._state.due.add(_SEND_INITIAL_METADATA_TOKEN)
       else:
         raise ValueError('Initial metadata no longer allowed!')
Beispiel #14
0
 def send_initial_metadata(self, initial_metadata):
   with self._state.condition:
     if self._state.client is _CANCELLED:
       _raise_rpc_error(self._state)
     else:
       if self._state.initial_metadata_allowed:
         operation = cygrpc.operation_send_initial_metadata(
             _common.cygrpc_metadata(initial_metadata), _EMPTY_FLAGS)
         self._rpc_event.operation_call.start_server_batch(
             cygrpc.Operations((operation,)),
             _send_initial_metadata(self._state))
         self._state.initial_metadata_allowed = False
         self._state.due.add(_SEND_INITIAL_METADATA_TOKEN)
       else:
         raise ValueError('Initial metadata no longer allowed!')
Beispiel #15
0
 def _prepare(self, request, timeout, metadata):
     deadline, deadline_timespec, serialized_request, rendezvous = (
         _start_unary_request(request, timeout, self._request_serializer))
     if serialized_request is None:
         return None, None, None, None, rendezvous
     else:
         state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
         operations = (
             cygrpc.operation_send_initial_metadata(
                 _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
             cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
             cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
             cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
             cygrpc.operation_receive_message(_EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),)
         return state, operations, deadline, deadline_timespec, None
Beispiel #16
0
 def _prepare(self, request, timeout, metadata):
     deadline, deadline_timespec, serialized_request, rendezvous = (
         _start_unary_request(request, timeout, self._request_serializer))
     if serialized_request is None:
         return None, None, None, None, rendezvous
     else:
         state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
         operations = (
             cygrpc.operation_send_initial_metadata(
                 _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
             cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
             cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
             cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
             cygrpc.operation_receive_message(_EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),)
         return state, operations, deadline, deadline_timespec, None
Beispiel #17
0
def _status(rpc_event, state, serialized_response):
  with state.condition:
    if state.client is not _CANCELLED:
      trailing_metadata = _common.cygrpc_metadata(state.trailing_metadata)
      code = _completion_code(state)
      details = _details(state)
      operations = [
          cygrpc.operation_send_status_from_server(
              trailing_metadata, code, details, _EMPTY_FLAGS),
      ]
      if state.initial_metadata_allowed:
        operations.append(
            cygrpc.operation_send_initial_metadata(
                _EMPTY_METADATA, _EMPTY_FLAGS))
      if serialized_response is not None:
        operations.append(cygrpc.operation_send_message(
            serialized_response, _EMPTY_FLAGS))
      rpc_event.operation_call.start_server_batch(
          cygrpc.Operations(operations),
          _send_status_from_server(state, _SEND_STATUS_FROM_SERVER_TOKEN))
      state.statused = True
      state.due.add(_SEND_STATUS_FROM_SERVER_TOKEN)
Beispiel #18
0
 def set_trailing_metadata(self, trailing_metadata):
   with self._state.condition:
     self._state.trailing_metadata = _common.cygrpc_metadata(
         trailing_metadata)
Beispiel #19
0
 def invocation_metadata(self):
     return _common.cygrpc_metadata(
         self._servicer_context.invocation_metadata())
 def invocation_metadata(self):
   return _common.cygrpc_metadata(
       self._servicer_context.invocation_metadata())
Beispiel #21
0
 def set_trailing_metadata(self, trailing_metadata):
     with self._state.condition:
         self._state.trailing_metadata = _common.cygrpc_metadata(
             trailing_metadata)