-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathserver_util.py
More file actions
233 lines (189 loc) · 10.3 KB
/
server_util.py
File metadata and controls
233 lines (189 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Copyright (c) 2023 Boston Dynamics, Inc. All rights reserved.
#
# Downloading, reproducing, distributing or otherwise using the SDK Software
# is subject to the terms and conditions of the Boston Dynamics Software
# Development Kit License (20191101-BDSDK-SL).
"""Helper functions and classes for creating and running a gRPC service."""
import copy
import logging
import signal
import sys
import time
from concurrent import futures
import grpc
import bosdyn.util
from bosdyn.api import (data_acquisition_store_pb2, data_buffer_pb2, header_pb2, image_pb2,
local_grid_pb2)
from bosdyn.client.channel import generate_channel_options
_LOGGER = logging.getLogger(__name__)
class ResponseContext(object):
"""Helper to log gRPC request and response message to the data buffer for a service.
It should be called using a "with" statement each time an RPC is received such that
the request and response proto messages can be passed in. It will automatically log
the request and response to the data buffer, and mutates the headers to add additional
information before logging.
Args:
response (protobuf): any gRPC response message with a bosdyn.api.ResponseHeader proto.
request (protobuf): any gRPC request message with a bosdyn.api.RequestHeader proto.
rpc_logger (DataBufferClient): Optional data buffer client to log the messages; if not
provided, only the headers will be mutated and nothing will be logged.
channel_prefix (string): the prefix you want this req / resp pair logged under.
exc_callback (function): called with exception type, value, and traceback info if an
exception is raised in the body of the "with" statement.
"""
def __init__(self, response, request, rpc_logger=None, channel_prefix=None, exc_callback=None):
self.response = response
self.response.header.request_header.CopyFrom(request.header)
self.request = request
self.rpc_logger = rpc_logger
self.channel_prefix = channel_prefix
self.exc_callback = exc_callback
def __enter__(self):
"""Adds a start timestamp to the response header and logs the request RPC."""
self.response.header.request_received_timestamp.CopyFrom(bosdyn.util.now_timestamp())
if self.rpc_logger:
if self.channel_prefix is None:
channel = None
else:
channel = self.channel_prefix + "/" + self.request.DESCRIPTOR.full_name
self.rpc_logger.add_protobuf_async(self.request, channel)
return self.response
def __exit__(self, exc_type, exc_val, exc_tb):
"""Updates the header code if unset and logs the response RPC."""
if self.response.header.error.code == self.response.header.error.CODE_UNSPECIFIED:
self.response.header.error.code = self.response.header.error.CODE_OK
if exc_type is not None:
# An uncaught exception was raised by the service. Automatically set the header
# to be an internal error.
self.response.header.error.code = self.response.header.error.CODE_INTERNAL_SERVER_ERROR
self.response.header.error.message = "[%s] %s" % (exc_type.__name__, exc_val)
if self.exc_callback:
self.exc_callback(exc_type, exc_val, exc_tb)
if self.rpc_logger:
if self.channel_prefix is None:
channel = None
else:
channel = self.channel_prefix + "/" + self.response.DESCRIPTOR.full_name
self.rpc_logger.add_protobuf_async(self.response, channel)
if not self.response.header.HasField("response_timestamp"):
self.response.header.response_timestamp.CopyFrom(bosdyn.util.now_timestamp())
class GrpcServiceRunner(object):
"""A runner to start a gRPC server on a background thread and allow easy cleanup.
Args:
service_servicer (custom servicer class derived from ServiceServicer): Servicer that
defines server behavior.
add_servicer_to_server_fn (function): Function generated by gRPC compilation that
attaches the servicer to the gRPC server.
port (int): The port number the service can be accessed through on the host system.
Defaults to 0, which will assign an ephemeral port.
max_send_message_length (int): Max message length (bytes) allowed for messages sent.
max_receive_message_length (int): Max message length (bytes) allowed for messages received.
timeout_secs (int): Number of seconds to wait for a clean server shutdown.
force_sigint_capture (bool): Re-assign the termination signal handlers to default in order to prevent
other scripts from blocking a clean exit. Defaults to True.
logger (logging.Logger): Logger to log with.
"""
def __init__(self, service_servicer, add_servicer_to_server_fn, port=0, max_workers=4,
max_send_message_length=None, max_receive_message_length=None, timeout_secs=3,
force_sigint_capture=True, logger=None):
self.logger = logger or _LOGGER
self.timeout_secs = timeout_secs
self.force_sigint_capture = force_sigint_capture
# Use the name of the service_servicer class for print messages.
self.server_type_name = type(service_servicer).__name__
self.server = grpc.server(
futures.ThreadPoolExecutor(max_workers=max_workers),
options=generate_channel_options(max_send_message_length, max_receive_message_length))
add_servicer_to_server_fn(service_servicer, self.server)
self.port = self.server.add_insecure_port('[::]:{}'.format(port))
self.server.start()
self.logger.info('Started the {} server.'.format(self.server_type_name))
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()
def stop(self):
"""Blocks until the gRPC server shuts down."""
self.logger.info("Shutting down the {} server.".format(self.server_type_name))
shutdown_complete = self.server.stop(None)
shutdown_complete.wait(self.timeout_secs)
def run_until_interrupt(self):
"""Spin the thread until a SIGINT, SIGTERM, or SIGQUIT is received and then shut down cleanly."""
if self.force_sigint_capture:
# Ensure that KeyboardInterrupt is raised on a SIGINT.
signal.signal(signal.SIGINT, signal.default_int_handler)
# Included SIGTERM for "docker stop". See https://docs.docker.com/engine/reference/commandline/stop/
signal.signal(signal.SIGTERM, signal.default_int_handler)
# OS check because on Windows, signal() can only be called with SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM.
if not sys.platform.startswith("win32"):
signal.signal(signal.SIGQUIT, signal.default_int_handler)
# Monitor for SIGINT, SIGTERM, or SIGQUIT and shut down cleanly.
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
self.stop()
def populate_response_header(response, request, error_code=header_pb2.CommonError.CODE_OK,
error_msg=None):
"""Sets the ResponseHeader header in the response.
Args:
response (bosdyn.api Response message): The GRPC response message to be populated.
request (bosdyn.api Request message): The header from the request is added to the response.
error_code (header_pb2.CommonError): The status for the RPC response.
error_msg (str): An optional error message describing a bad header status failure.
Returns:
Mutates the response message's header to be fully populated.
"""
header = header_pb2.ResponseHeader()
header.request_received_timestamp.CopyFrom(bosdyn.util.now_timestamp())
header.request_header.CopyFrom(request.header)
header.error.code = error_code
if error_msg:
header.error.message = error_msg
copied_request = copy.copy(request)
strip_large_bytes_fields(copied_request)
header.request.Pack(copied_request)
response.header.CopyFrom(header)
def strip_large_bytes_fields(proto_message):
"""Removes any large bytes fields from a protobuf message depending on the proto type."""
message_type = type(proto_message)
allowlist_map = get_bytes_field_allowlist()
if message_type in allowlist_map:
allowlist_map[message_type](proto_message)
def get_bytes_field_allowlist():
"""Creates set of protos which will have bytes fields removed."""
allowlist_map = {
image_pb2.GetImageResponse: strip_get_image_response,
local_grid_pb2.GetLocalGridsResponse: strip_local_grid_responses,
data_acquisition_store_pb2.StoreDataRequest: strip_store_data_request,
data_acquisition_store_pb2.StoreImageRequest: strip_store_image_request,
data_buffer_pb2.RecordSignalTicksRequest: strip_record_signal_tick,
data_buffer_pb2.RecordDataBlobsRequest: strip_record_data_blob,
}
return allowlist_map
def strip_image_response(proto_message):
"""Removes bytes from the image_pb2.ImageResponse proto."""
proto_message.shot.image.ClearField("data")
def strip_get_image_response(proto_message):
"""Removes bytes from the image_pb2.GetImageResponse proto."""
for img_resp in proto_message.image_responses:
strip_image_response(img_resp)
def strip_local_grid_responses(proto_message):
"""Removes bytes from the local_grid_pb2.GetLocalGridsResponse proto."""
for grid_resp in proto_message.local_grid_responses:
grid_resp.local_grid.ClearField("data")
def strip_store_image_request(proto_message):
"""Removes bytes from the data_acquisition_store_pb2.StoreImageRequest proto."""
proto_message.image.image.ClearField("data")
def strip_store_data_request(proto_message):
"""Removes bytes from the data_acquisition_store_pb2.StoreDataRequest proto."""
proto_message.ClearField("data")
def strip_record_signal_tick(proto_message):
"""Removes bytes from the data_buffer_pb2.RecordSignalTicksRequest proto."""
for tick_data in proto_message.tick_data:
tick_data.ClearField("data")
def strip_record_data_blob(proto_message):
"""Removes bytes from the data_buffer_pb2.RecordDataBlobsRequest proto."""
for blob in proto_message.blob_data:
blob.ClearField("data")