Merge pull request #19852 from gnossen/server_termination

Quit waiting for a day
pull/19891/head
Richard Belleville 6 years ago committed by GitHub
commit d932d2368e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      examples/python/auth/customized_auth_client.py
  2. 13
      examples/python/auth/customized_auth_server.py
  3. 6
      examples/python/auth/test/_auth_example_test.py
  4. 16
      examples/python/cancellation/server.py
  5. 8
      examples/python/compression/server.py
  6. 8
      examples/python/debug/debug_server.py
  7. 9
      examples/python/errors/server.py
  8. 9
      examples/python/helloworld/greeter_server.py
  9. 9
      examples/python/helloworld/greeter_server_with_reflection.py
  10. 9
      examples/python/interceptors/headers/greeter_server.py
  11. 9
      examples/python/metadata/metadata_server.py
  12. 8
      examples/python/multiplex/multiplex_server.py
  13. 8
      examples/python/route_guide/route_guide_server.py
  14. 2
      examples/python/wait_for_ready/wait_for_ready_example.py
  15. 11
      src/python/grpcio_tests/tests/interop/server.py

@ -29,8 +29,6 @@ from examples.python.auth import _credentials
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.INFO) _LOGGER.setLevel(logging.INFO)
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
_SERVER_ADDR_TEMPLATE = 'localhost:%d' _SERVER_ADDR_TEMPLATE = 'localhost:%d'
_SIGNATURE_HEADER_KEY = 'x-signature' _SIGNATURE_HEADER_KEY = 'x-signature'

@ -20,7 +20,6 @@ from __future__ import print_function
import argparse import argparse
import contextlib import contextlib
import logging import logging
import time
from concurrent import futures from concurrent import futures
import grpc import grpc
@ -31,8 +30,6 @@ from examples.python.auth import _credentials
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.INFO) _LOGGER.setLevel(logging.INFO)
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
_LISTEN_ADDRESS_TEMPLATE = 'localhost:%d' _LISTEN_ADDRESS_TEMPLATE = 'localhost:%d'
_SIGNATURE_HEADER_KEY = 'x-signature' _SIGNATURE_HEADER_KEY = 'x-signature'
@ -85,7 +82,7 @@ def run_server(port):
server.start() server.start()
try: try:
yield port yield server, port
finally: finally:
server.stop(0) server.stop(0)
@ -96,13 +93,9 @@ def main():
'--port', nargs='?', type=int, default=50051, help='the listening port') '--port', nargs='?', type=int, default=50051, help='the listening port')
args = parser.parse_args() args = parser.parse_args()
with run_server(args.port) as port: with run_server(args.port) as (server, port):
logging.info('Server is listening at port :%d', port) logging.info('Server is listening at port :%d', port)
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
pass
if __name__ == '__main__': if __name__ == '__main__':

@ -30,20 +30,20 @@ _SERVER_ADDR_TEMPLATE = 'localhost:%d'
class AuthExampleTest(unittest.TestCase): class AuthExampleTest(unittest.TestCase):
def test_successful_call(self): def test_successful_call(self):
with customized_auth_server.run_server(0) as port: with customized_auth_server.run_server(0) as (_, port):
with customized_auth_client.create_client_channel( with customized_auth_client.create_client_channel(
_SERVER_ADDR_TEMPLATE % port) as channel: _SERVER_ADDR_TEMPLATE % port) as channel:
customized_auth_client.send_rpc(channel) customized_auth_client.send_rpc(channel)
# No unhandled exception raised, test passed! # No unhandled exception raised, test passed!
def test_no_channel_credential(self): def test_no_channel_credential(self):
with customized_auth_server.run_server(0) as port: with customized_auth_server.run_server(0) as (_, port):
with grpc.insecure_channel(_SERVER_ADDR_TEMPLATE % port) as channel: with grpc.insecure_channel(_SERVER_ADDR_TEMPLATE % port) as channel:
resp = customized_auth_client.send_rpc(channel) resp = customized_auth_client.send_rpc(channel)
self.assertEqual(resp.code(), grpc.StatusCode.UNAVAILABLE) self.assertEqual(resp.code(), grpc.StatusCode.UNAVAILABLE)
def test_no_call_credential(self): def test_no_call_credential(self):
with customized_auth_server.run_server(0) as port: with customized_auth_server.run_server(0) as (_, port):
channel_credential = grpc.ssl_channel_credentials( channel_credential = grpc.ssl_channel_credentials(
_credentials.ROOT_CERTIFICATE) _credentials.ROOT_CERTIFICATE)
with grpc.secure_channel(_SERVER_ADDR_TEMPLATE % port, with grpc.secure_channel(_SERVER_ADDR_TEMPLATE % port,

@ -19,9 +19,7 @@ from __future__ import print_function
from concurrent import futures from concurrent import futures
import argparse import argparse
import contextlib
import logging import logging
import time
import threading import threading
import grpc import grpc
@ -32,7 +30,6 @@ from examples.python.cancellation import hash_name_pb2_grpc
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_SERVER_HOST = 'localhost' _SERVER_HOST = 'localhost'
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
_DESCRIPTION = "A server for finding hashes similar to names." _DESCRIPTION = "A server for finding hashes similar to names."
@ -88,7 +85,6 @@ class HashFinder(hash_name_pb2_grpc.HashFinderServicer):
_LOGGER.debug("Regained servicer thread.") _LOGGER.debug("Regained servicer thread.")
@contextlib.contextmanager
def _running_server(port, maximum_hashes): def _running_server(port, maximum_hashes):
# We use only a single servicer thread here to demonstrate that, if managed # We use only a single servicer thread here to demonstrate that, if managed
# carefully, cancelled RPCs can need not continue occupying servicers # carefully, cancelled RPCs can need not continue occupying servicers
@ -101,12 +97,7 @@ def _running_server(port, maximum_hashes):
actual_port = server.add_insecure_port(address) actual_port = server.add_insecure_port(address)
server.start() server.start()
print("Server listening at '{}'".format(address)) print("Server listening at '{}'".format(address))
try: return server
yield actual_port
except KeyboardInterrupt:
pass
finally:
server.stop(None)
def main(): def main():
@ -124,9 +115,8 @@ def main():
nargs='?', nargs='?',
help='The maximum number of hashes to search before cancelling.') help='The maximum number of hashes to search before cancelling.')
args = parser.parse_args() args = parser.parse_args()
with _running_server(args.port, args.maximum_hashes): server = _running_server(args.port, args.maximum_hashes)
while True: server.wait_for_termination()
time.sleep(_ONE_DAY_IN_SECONDS)
if __name__ == "__main__": if __name__ == "__main__":

@ -21,7 +21,6 @@ from concurrent import futures
import argparse import argparse
import logging import logging
import threading import threading
import time
import grpc import grpc
from examples import helloworld_pb2 from examples import helloworld_pb2
@ -36,7 +35,6 @@ _COMPRESSION_OPTIONS = {
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_SERVER_HOST = 'localhost' _SERVER_HOST = 'localhost'
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer): class Greeter(helloworld_pb2_grpc.GreeterServicer):
@ -72,11 +70,7 @@ def run_server(server_compression, no_compress_every_n, port):
server.add_insecure_port(address) server.add_insecure_port(address)
server.start() server.start()
print("Server listening at '{}'".format(address)) print("Server listening at '{}'".format(address))
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(None)
def main(): def main():

@ -19,7 +19,6 @@ from __future__ import print_function
import argparse import argparse
import logging import logging
import time
from concurrent import futures from concurrent import futures
import random import random
@ -32,7 +31,6 @@ from examples import helloworld_pb2_grpc
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.INFO) _LOGGER.setLevel(logging.INFO)
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
_RANDOM_FAILURE_RATE = 0.3 _RANDOM_FAILURE_RATE = 0.3
@ -78,11 +76,7 @@ def main():
server = create_server(addr=args.addr, failure_rate=args.failure_rate) server = create_server(addr=args.addr, failure_rate=args.failure_rate)
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -14,7 +14,6 @@
"""This example sends out rich error status from server-side.""" """This example sends out rich error status from server-side."""
from concurrent import futures from concurrent import futures
import time
import logging import logging
import threading import threading
@ -27,8 +26,6 @@ from google.rpc import code_pb2, status_pb2, error_details_pb2
from examples import helloworld_pb2 from examples import helloworld_pb2
from examples import helloworld_pb2_grpc from examples import helloworld_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
def create_greet_limit_exceed_error_status(name): def create_greet_limit_exceed_error_status(name):
detail = any_pb2.Any() detail = any_pb2.Any()
@ -73,11 +70,7 @@ def create_server(server_address):
def serve(server): def serve(server):
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(None)
def main(): def main():

@ -14,7 +14,6 @@
"""The Python implementation of the GRPC helloworld.Greeter server.""" """The Python implementation of the GRPC helloworld.Greeter server."""
from concurrent import futures from concurrent import futures
import time
import logging import logging
import grpc import grpc
@ -22,8 +21,6 @@ import grpc
import helloworld_pb2 import helloworld_pb2
import helloworld_pb2_grpc import helloworld_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer): class Greeter(helloworld_pb2_grpc.GreeterServicer):
@ -36,11 +33,7 @@ def serve():
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -14,7 +14,6 @@
"""The reflection-enabled version of gRPC helloworld.Greeter server.""" """The reflection-enabled version of gRPC helloworld.Greeter server."""
from concurrent import futures from concurrent import futures
import time
import logging import logging
import grpc import grpc
@ -23,8 +22,6 @@ from grpc_reflection.v1alpha import reflection
import helloworld_pb2 import helloworld_pb2
import helloworld_pb2_grpc import helloworld_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer): class Greeter(helloworld_pb2_grpc.GreeterServicer):
@ -42,11 +39,7 @@ def serve():
reflection.enable_server_reflection(SERVICE_NAMES, server) reflection.enable_server_reflection(SERVICE_NAMES, server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -14,7 +14,6 @@
"""The Python implementation of the GRPC helloworld.Greeter server.""" """The Python implementation of the GRPC helloworld.Greeter server."""
from concurrent import futures from concurrent import futures
import time
import logging import logging
import grpc import grpc
@ -23,8 +22,6 @@ import helloworld_pb2
import helloworld_pb2_grpc import helloworld_pb2_grpc
from request_header_validator_interceptor import RequestHeaderValidatorInterceptor from request_header_validator_interceptor import RequestHeaderValidatorInterceptor
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer): class Greeter(helloworld_pb2_grpc.GreeterServicer):
@ -42,11 +39,7 @@ def serve():
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -15,7 +15,6 @@
from __future__ import print_function from __future__ import print_function
from concurrent import futures from concurrent import futures
import time
import logging import logging
import grpc import grpc
@ -23,8 +22,6 @@ import grpc
import helloworld_pb2 import helloworld_pb2
import helloworld_pb2_grpc import helloworld_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer): class Greeter(helloworld_pb2_grpc.GreeterServicer):
@ -44,11 +41,7 @@ def serve():
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -26,8 +26,6 @@ import route_guide_pb2
import route_guide_pb2_grpc import route_guide_pb2_grpc
import route_guide_resources import route_guide_resources
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
def _get_feature(feature_db, point): def _get_feature(feature_db, point):
"""Returns Feature at given location or None.""" """Returns Feature at given location or None."""
@ -129,11 +127,7 @@ def serve():
_RouteGuideServicer(), server) _RouteGuideServicer(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -24,8 +24,6 @@ import route_guide_pb2
import route_guide_pb2_grpc import route_guide_pb2_grpc
import route_guide_resources import route_guide_resources
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
def get_feature(feature_db, point): def get_feature(feature_db, point):
"""Returns Feature at given location or None.""" """Returns Feature at given location or None."""
@ -119,11 +117,7 @@ def serve():
RouteGuideServicer(), server) RouteGuideServicer(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')
server.start() server.start()
try: server.wait_for_termination()
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__': if __name__ == '__main__':

@ -28,8 +28,6 @@ from examples import helloworld_pb2_grpc
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.INFO) _LOGGER.setLevel(logging.INFO)
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
@contextmanager @contextmanager
def get_free_loopback_tcp_port(): def get_free_loopback_tcp_port():

@ -16,7 +16,6 @@
import argparse import argparse
from concurrent import futures from concurrent import futures
import logging import logging
import time
import grpc import grpc
from src.proto.grpc.testing import test_pb2_grpc from src.proto.grpc.testing import test_pb2_grpc
@ -25,7 +24,6 @@ from tests.interop import service
from tests.interop import resources from tests.interop import resources
from tests.unit import test_common from tests.unit import test_common
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
logging.basicConfig() logging.basicConfig()
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -55,13 +53,8 @@ def serve():
server.start() server.start()
_LOGGER.info('Server serving.') _LOGGER.info('Server serving.')
try: server.wait_for_termination()
while True: _LOGGER.info('Server stopped; exiting.')
time.sleep(_ONE_DAY_IN_SECONDS)
except BaseException as e:
_LOGGER.info('Caught exception "%s"; stopping server...', e)
server.stop(None)
_LOGGER.info('Server stopped; exiting.')
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save