Skip to content

Commit

Permalink
Merge pull request grpc#7160 from nathanielmanistaatgoogle/handlers-o…
Browse files Browse the repository at this point in the history
…ptional

Make handlers optional at server construction
  • Loading branch information
kpayson64 authored Jul 8, 2016
2 parents 8444f2f + a6b2a5a commit 340d396
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 22 deletions.
16 changes: 7 additions & 9 deletions src/python/grpcio/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,25 +1211,23 @@ def secure_channel(target, credentials, options=None):
return _channel.Channel(target, options, credentials._credentials)


def server(generic_rpc_handlers, thread_pool, options=None):
def server(thread_pool, handlers=None):
"""Creates a Server with which RPCs can be serviced.
The GenericRpcHandlers passed to this function needn't be the only
GenericRpcHandlers that will be used to serve RPCs; others may be added later
by calling add_generic_rpc_handlers any time before the returned server is
started.
Args:
generic_rpc_handlers: Some number of GenericRpcHandlers that will be used
to service RPCs after the returned Server is started.
thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
to service RPCs.
handlers: An optional sequence of GenericRpcHandlers to be used to service
RPCs after the returned Server is started. These handlers need not be the
only handlers the returned Server will use to service RPCs; other
handlers may later be added to the returned Server by calling its
add_generic_rpc_handlers method any time before it is started.
Returns:
A Server with which RPCs can be serviced.
"""
from grpc import _server
return _server.Server(generic_rpc_handlers, thread_pool)
return _server.Server(thread_pool, () if handlers is None else handlers)


################################### __all__ #################################
Expand Down
2 changes: 1 addition & 1 deletion src/python/grpcio/grpc/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ def cleanup_server(timeout):

class Server(grpc.Server):

def __init__(self, generic_handlers, thread_pool):
def __init__(self, thread_pool, generic_handlers):
completion_queue = cygrpc.CompletionQueue()
server = cygrpc.Server()
server.register_completion_queue(completion_queue)
Expand Down
3 changes: 2 additions & 1 deletion src/python/grpcio/grpc/beta/_server_adaptations.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,5 @@ def server(
_DEFAULT_POOL_SIZE if thread_pool_size is None else thread_pool_size)
else:
effective_thread_pool = thread_pool
return _Server(grpc.server((generic_rpc_handler,), effective_thread_pool))
return _Server(
grpc.server(effective_thread_pool, handlers=(generic_rpc_handler,)))
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def HalfDuplexCall(self, request_iter, context):
return servicer_methods.HalfDuplexCall(request_iter, context)

server = grpc.server(
(), futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
getattr(service_pb2, ADD_SERVICER_TO_SERVER_IDENTIFIER)(Servicer(), server)
port = server.add_insecure_port('[::]:0')
server.start()
Expand All @@ -192,7 +192,7 @@ class Servicer(getattr(service_pb2, SERVICER_IDENTIFIER)):
pass

server = grpc.server(
(), futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
getattr(service_pb2, ADD_SERVICER_TO_SERVER_IDENTIFIER)(Servicer(), server)
port = server.add_insecure_port('[::]:0')
server.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_lonely_channel_connectivity(self):
grpc.ChannelConnectivity.READY, fifth_connectivities)

def test_immediately_connectable_channel_connectivity(self):
server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
port = server.add_insecure_port('[::]:0')
server.start()
first_callback = _Callback()
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_immediately_connectable_channel_connectivity(self):
grpc.ChannelConnectivity.SHUTDOWN, fourth_connectivities)

def test_reachable_then_unreachable_channel_connectivity(self):
server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
port = server.add_insecure_port('[::]:0')
server.start()
callback = _Callback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_lonely_channel_connectivity(self):
self.assertFalse(ready_future.running())

def test_immediately_connectable_channel_connectivity(self):
server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
port = server.add_insecure_port('[::]:0')
server.start()
channel = grpc.insecure_channel('localhost:{}'.format(port))
Expand Down
3 changes: 2 additions & 1 deletion src/python/grpcio_tests/tests/unit/_compression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class CompressionTest(unittest.TestCase):

def setUp(self):
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server((_GenericHandler(),), self._server_pool)
self._server = grpc.server(
self._server_pool, handlers=(_GenericHandler(),))
self._port = self._server.add_insecure_port('[::]:0')
self._server.start()

Expand Down
3 changes: 2 additions & 1 deletion src/python/grpcio_tests/tests/unit/_empty_message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class EmptyMessageTest(unittest.TestCase):

def setUp(self):
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server((_GenericHandler(),), self._server_pool)
self._server = grpc.server(
self._server_pool, handlers=(_GenericHandler(),))
port = self._server.add_insecure_port('[::]:0')
self._server.start()
self._channel = grpc.insecure_channel('localhost:%d' % port)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def setUp(self):
self._servicer = _Servicer()
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server(
(_generic_handler(self._servicer),), self._server_pool)
self._server_pool, handlers=(_generic_handler(self._servicer),))
port = self._server.add_insecure_port('[::]:0')
self._server.start()

Expand Down
4 changes: 2 additions & 2 deletions src/python/grpcio_tests/tests/unit/_metadata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class MetadataTest(unittest.TestCase):

def setUp(self):
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server((_GenericHandler(weakref.proxy(self)),),
self._server_pool)
self._server = grpc.server(
self._server_pool, handlers=(_GenericHandler(weakref.proxy(self)),))
port = self._server.add_insecure_port('[::]:0')
self._server.start()
self._channel = grpc.insecure_channel('localhost:%d' % port,
Expand Down
2 changes: 1 addition & 1 deletion src/python/grpcio_tests/tests/unit/_rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def setUp(self):
self._handler = _Handler(self._control)
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)

self._server = grpc.server((), self._server_pool)
self._server = grpc.server(self._server_pool)
port = self._server.add_insecure_port('[::]:0')
self._server.add_generic_rpc_handlers((_GenericHandler(self._handler),))
self._server.start()
Expand Down

0 comments on commit 340d396

Please sign in to comment.