_handler.py
Go to the documentation of this file.
1 # Copyright 2017 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import abc
16 import threading
17 
18 import grpc
19 from grpc_testing import _common
20 
21 _CLIENT_INACTIVE = object()
22 
23 
25 
26  @abc.abstractmethod
27  def initial_metadata(self):
28  raise NotImplementedError()
29 
30  @abc.abstractmethod
31  def add_request(self, request):
32  raise NotImplementedError()
33 
34  @abc.abstractmethod
35  def take_response(self):
36  raise NotImplementedError()
37 
38  @abc.abstractmethod
39  def requests_closed(self):
40  raise NotImplementedError()
41 
42  @abc.abstractmethod
43  def cancel(self):
44  raise NotImplementedError()
45 
46  @abc.abstractmethod
48  raise NotImplementedError()
49 
50  @abc.abstractmethod
52  raise NotImplementedError()
53 
54 
56 
57  def __init__(self, requests_closed):
58  self._condition = threading.Condition()
59  self._requests = []
60  self._requests_closed = requests_closed
61  self._initial_metadata = None
62  self._responses = []
63  self._trailing_metadata = None
64  self._code = None
65  self._details = None
66  self._unary_response = None
67  self._expiration_future = None
69 
70  def send_initial_metadata(self, initial_metadata):
71  with self._condition:
72  self._initial_metadata = initial_metadata
73  self._condition.notify_all()
74 
75  def take_request(self):
76  with self._condition:
77  while True:
78  if self._code is None:
79  if self._requests:
80  request = self._requests.pop(0)
81  self._condition.notify_all()
82  return _common.ServerRpcRead(request, False, False)
83  elif self._requests_closed:
84  return _common.REQUESTS_CLOSED
85  else:
86  self._condition.wait()
87  else:
88  return _common.TERMINATED
89 
90  def is_active(self):
91  with self._condition:
92  return self._code is None
93 
94  def add_response(self, response):
95  with self._condition:
96  self._responses.append(response)
97  self._condition.notify_all()
98 
99  def send_termination(self, trailing_metadata, code, details):
100  with self._condition:
101  self._trailing_metadata = trailing_metadata
102  self._code = code
103  self._details = details
104  if self._expiration_future is not None:
106  self._condition.notify_all()
107 
108  def add_termination_callback(self, callback):
109  with self._condition:
110  if self._code is None:
111  self._termination_callbacks.append(callback)
112  return True
113  else:
114  return False
115 
116  def initial_metadata(self):
117  with self._condition:
118  while True:
119  if self._initial_metadata is None:
120  if self._code is None:
121  self._condition.wait()
122  else:
123  raise ValueError(
124  'No initial metadata despite status code!')
125  else:
126  return self._initial_metadata
127 
128  def add_request(self, request):
129  with self._condition:
130  self._requests.append(request)
131  self._condition.notify_all()
132 
133  def take_response(self):
134  with self._condition:
135  while True:
136  if self._responses:
137  response = self._responses.pop(0)
138  self._condition.notify_all()
139  return response
140  elif self._code is None:
141  self._condition.wait()
142  else:
143  raise ValueError('No more responses!')
144 
145  def requests_closed(self):
146  with self._condition:
147  self._requests_closed = True
148  self._condition.notify_all()
149 
150  def cancel(self):
151  with self._condition:
152  if self._code is None:
153  self._code = _CLIENT_INACTIVE
154  termination_callbacks = self._termination_callbacks
155  self._termination_callbacks = None
156  if self._expiration_future is not None:
158  self._condition.notify_all()
159  for termination_callback in termination_callbacks:
160  termination_callback()
161 
163  with self._condition:
164  while True:
165  if self._code is _CLIENT_INACTIVE:
166  raise ValueError('Huh? Cancelled but wanting status?')
167  elif self._code is None:
168  self._condition.wait()
169  else:
170  if self._unary_response is None:
171  if self._responses:
172  self._unary_response = self._responses.pop(0)
173  return (
174  self._unary_response,
175  self._trailing_metadata,
176  self._code,
177  self._details,
178  )
179 
181  with self._condition:
182  while True:
183  if self._code is _CLIENT_INACTIVE:
184  raise ValueError('Huh? Cancelled but wanting status?')
185  elif self._code is None:
186  self._condition.wait()
187  else:
188  return self._trailing_metadata, self._code, self._details
189 
190  def expire(self):
191  with self._condition:
192  if self._code is None:
193  if self._initial_metadata is None:
194  self._initial_metadata = _common.FUSSED_EMPTY_METADATA
195  self._trailing_metadata = _common.FUSSED_EMPTY_METADATA
196  self._code = grpc.StatusCode.DEADLINE_EXCEEDED
197  self._details = 'Took too much time!'
198  termination_callbacks = self._termination_callbacks
199  self._termination_callbacks = None
200  self._condition.notify_all()
201  for termination_callback in termination_callbacks:
202  termination_callback()
203 
204  def set_expiration_future(self, expiration_future):
205  with self._condition:
206  self._expiration_future = expiration_future
207 
208 
209 def handler_without_deadline(requests_closed):
210  return _Handler(requests_closed)
211 
212 
213 def handler_with_deadline(requests_closed, time, deadline):
214  handler = _Handler(requests_closed)
215  expiration_future = time.call_at(handler.expire, deadline)
216  handler.set_expiration_future(expiration_future)
217  return handler
grpc_testing._server._handler.Handler.take_response
def take_response(self)
Definition: _handler.py:35
grpc_testing._server._handler._Handler._responses
_responses
Definition: _handler.py:62
grpc_testing._server._handler._Handler.add_response
def add_response(self, response)
Definition: _handler.py:94
grpc_testing._server._handler._Handler.is_active
def is_active(self)
Definition: _handler.py:90
grpc_testing._server._handler._Handler._trailing_metadata
_trailing_metadata
Definition: _handler.py:63
grpc_testing._server._handler._Handler.initial_metadata
def initial_metadata(self)
Definition: _handler.py:116
grpc_testing._server._handler.Handler.stream_response_termination
def stream_response_termination(self)
Definition: _handler.py:51
grpc_testing._common.ServerRpcRead
Definition: testing/grpc_testing/_common.py:111
grpc_testing._server._handler._Handler.unary_response_termination
def unary_response_termination(self)
Definition: _handler.py:162
grpc_testing._server._handler._Handler._termination_callbacks
_termination_callbacks
Definition: _handler.py:68
grpc_testing._server._handler.Handler.requests_closed
def requests_closed(self)
Definition: _handler.py:39
grpc_testing._server._handler._Handler._requests_closed
_requests_closed
Definition: _handler.py:60
grpc_testing._server._handler._Handler.send_termination
def send_termination(self, trailing_metadata, code, details)
Definition: _handler.py:99
grpc_testing._server._handler._Handler.take_response
def take_response(self)
Definition: _handler.py:133
grpc_testing._server._handler._Handler.add_request
def add_request(self, request)
Definition: _handler.py:128
grpc_testing._server._handler._Handler.set_expiration_future
def set_expiration_future(self, expiration_future)
Definition: _handler.py:204
grpc_testing._server._handler.Handler.add_request
def add_request(self, request)
Definition: _handler.py:31
grpc_testing._server._handler._Handler.send_initial_metadata
def send_initial_metadata(self, initial_metadata)
Definition: _handler.py:70
grpc_testing._server._handler.handler_without_deadline
def handler_without_deadline(requests_closed)
Definition: _handler.py:209
grpc_testing._server._handler._Handler
Definition: _handler.py:55
grpc_testing._server._handler._Handler.add_termination_callback
def add_termination_callback(self, callback)
Definition: _handler.py:108
grpc_testing._server._handler._Handler.stream_response_termination
def stream_response_termination(self)
Definition: _handler.py:180
grpc_testing._server._handler.Handler.cancel
def cancel(self)
Definition: _handler.py:43
grpc_testing._server._handler.Handler
Definition: _handler.py:24
grpc_testing._server._handler._Handler._requests
_requests
Definition: _handler.py:59
grpc_testing._server._handler.Handler.unary_response_termination
def unary_response_termination(self)
Definition: _handler.py:47
grpc_testing._server._handler.Handler.initial_metadata
def initial_metadata(self)
Definition: _handler.py:27
grpc_testing._server._handler._Handler.expire
def expire(self)
Definition: _handler.py:190
grpc_testing._common.ServerRpcHandler
Definition: testing/grpc_testing/_common.py:119
grpc_testing._server._handler._Handler._initial_metadata
_initial_metadata
Definition: _handler.py:61
grpc_testing._server._handler._Handler.cancel
def cancel(self)
Definition: _handler.py:150
grpc_testing._server._handler._Handler._condition
_condition
Definition: _handler.py:58
grpc_testing._server._handler._Handler._details
_details
Definition: _handler.py:65
wait
static void wait(notification *n)
Definition: alts_tsi_handshaker_test.cc:114
grpc_testing._server._handler._Handler.__init__
def __init__(self, requests_closed)
Definition: _handler.py:57
grpc_testing._server._handler._Handler._code
_code
Definition: _handler.py:64
grpc_testing._server._handler._Handler._expiration_future
_expiration_future
Definition: _handler.py:67
grpc_testing._server._handler._Handler.take_request
def take_request(self)
Definition: _handler.py:75
grpc_testing._server._handler.handler_with_deadline
def handler_with_deadline(requests_closed, time, deadline)
Definition: _handler.py:213
grpc_testing._server._handler._Handler.requests_closed
def requests_closed(self)
Definition: _handler.py:145
grpc_testing._server._handler._Handler._unary_response
_unary_response
Definition: _handler.py:66


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:27