Free grpc_channel_args after creation

pull/17600/head
Richard Belleville 6 years ago
parent c4d5b78077
commit 0daa420417
  1. 13
      src/python/grpcio/grpc/_cython/_cygrpc/arguments.pxd.pxi
  2. 37
      src/python/grpcio/grpc/_cython/_cygrpc/arguments.pyx.pxi
  3. 9
      src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi
  4. 1
      src/python/grpcio/grpc/_cython/_cygrpc/server.pxd.pxi
  5. 8
      src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi

@ -28,19 +28,22 @@ cdef tuple _wrap_grpc_arg(grpc_arg arg)
cdef grpc_arg _unwrap_grpc_arg(tuple wrapped_arg) cdef grpc_arg _unwrap_grpc_arg(tuple wrapped_arg)
cdef class _ArgumentProcessor: cdef class _ChannelArg:
cdef grpc_arg c_argument cdef grpc_arg c_argument
cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references) except * cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references) except *
cdef class _ArgumentsProcessor: cdef class _ChannelArgs:
cdef readonly tuple _arguments cdef readonly tuple _arguments
cdef list _argument_processors cdef list _channel_args
cdef readonly list _references cdef readonly list _references
cdef grpc_channel_args _c_arguments cdef grpc_channel_args _c_arguments
cdef grpc_channel_args *c(self, grpc_arg_pointer_vtable *vtable) except * cdef void _c(self, grpc_arg_pointer_vtable *vtable) except *
cdef un_c(self) cdef grpc_channel_args *c_args(self) except *
@staticmethod
cdef _ChannelArgs from_args(object arguments, grpc_arg_pointer_vtable * vtable)

@ -50,7 +50,7 @@ cdef grpc_arg _unwrap_grpc_arg(tuple wrapped_arg):
return wrapped.arg return wrapped.arg
cdef class _ArgumentProcessor: cdef class _ChannelArg:
cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references) except *: cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references) except *:
key, value = argument key, value = argument
@ -82,27 +82,34 @@ cdef class _ArgumentProcessor:
'Expected int, bytes, or behavior, got {}'.format(type(value))) 'Expected int, bytes, or behavior, got {}'.format(type(value)))
cdef class _ArgumentsProcessor: cdef class _ChannelArgs:
def __cinit__(self, arguments): def __cinit__(self, arguments):
self._arguments = () if arguments is None else tuple(arguments) self._arguments = () if arguments is None else tuple(arguments)
self._argument_processors = [] self._channel_args = []
self._references = [] self._references = []
self._c_arguments.arguments = NULL
cdef grpc_channel_args *c(self, grpc_arg_pointer_vtable *vtable) except *: cdef void _c(self, grpc_arg_pointer_vtable *vtable) except *:
self._c_arguments.arguments_length = len(self._arguments) self._c_arguments.arguments_length = len(self._arguments)
if self._c_arguments.arguments_length == 0: if self._c_arguments.arguments_length != 0:
return NULL
else:
self._c_arguments.arguments = <grpc_arg *>gpr_malloc( self._c_arguments.arguments = <grpc_arg *>gpr_malloc(
self._c_arguments.arguments_length * sizeof(grpc_arg)) self._c_arguments.arguments_length * sizeof(grpc_arg))
for index, argument in enumerate(self._arguments): for index, argument in enumerate(self._arguments):
argument_processor = _ArgumentProcessor() channel_arg = _ChannelArg()
argument_processor.c(argument, vtable, self._references) channel_arg.c(argument, vtable, self._references)
self._c_arguments.arguments[index] = argument_processor.c_argument self._c_arguments.arguments[index] = channel_arg.c_argument
self._argument_processors.append(argument_processor) self._channel_args.append(channel_arg)
return &self._c_arguments
cdef grpc_channel_args *c_args(self) except *:
cdef un_c(self): return &self._c_arguments
if self._arguments:
def __dealloc__(self):
if self._c_arguments.arguments != NULL:
gpr_free(self._c_arguments.arguments) gpr_free(self._c_arguments.arguments)
@staticmethod
cdef _ChannelArgs from_args(object arguments, grpc_arg_pointer_vtable * vtable):
cdef _ChannelArgs channel_args = _ChannelArgs(arguments)
channel_args._c(vtable)
return channel_args

@ -423,16 +423,15 @@ cdef class Channel:
self._vtable.copy = &_copy_pointer self._vtable.copy = &_copy_pointer
self._vtable.destroy = &_destroy_pointer self._vtable.destroy = &_destroy_pointer
self._vtable.cmp = &_compare_pointer self._vtable.cmp = &_compare_pointer
cdef _ArgumentsProcessor arguments_processor = _ArgumentsProcessor( cdef _ChannelArgs channel_args = _ChannelArgs.from_args(
arguments) arguments, &self._vtable)
cdef grpc_channel_args *c_arguments = arguments_processor.c(&self._vtable)
if channel_credentials is None: if channel_credentials is None:
self._state.c_channel = grpc_insecure_channel_create( self._state.c_channel = grpc_insecure_channel_create(
<char *>target, c_arguments, NULL) <char *>target, channel_args.c_args(), NULL)
else: else:
c_channel_credentials = channel_credentials.c() c_channel_credentials = channel_credentials.c()
self._state.c_channel = grpc_secure_channel_create( self._state.c_channel = grpc_secure_channel_create(
c_channel_credentials, <char *>target, c_arguments, NULL) c_channel_credentials, <char *>target, channel_args.c_args(), NULL)
grpc_channel_credentials_release(c_channel_credentials) grpc_channel_credentials_release(c_channel_credentials)
self._state.c_call_completion_queue = ( self._state.c_call_completion_queue = (
grpc_completion_queue_create_for_next(NULL)) grpc_completion_queue_create_for_next(NULL))

@ -16,7 +16,6 @@
cdef class Server: cdef class Server:
cdef grpc_arg_pointer_vtable _vtable cdef grpc_arg_pointer_vtable _vtable
cdef readonly _ArgumentsProcessor _arguments_processor
cdef grpc_server *c_server cdef grpc_server *c_server
cdef bint is_started # start has been called cdef bint is_started # start has been called
cdef bint is_shutting_down # shutdown has been called cdef bint is_shutting_down # shutdown has been called

@ -29,11 +29,9 @@ cdef class Server:
self._vtable.copy = &_copy_pointer self._vtable.copy = &_copy_pointer
self._vtable.destroy = &_destroy_pointer self._vtable.destroy = &_destroy_pointer
self._vtable.cmp = &_compare_pointer self._vtable.cmp = &_compare_pointer
cdef _ArgumentsProcessor arguments_processor = _ArgumentsProcessor( cdef _ChannelArgs channel_args = _ChannelArgs.from_args(
arguments) arguments, &self._vtable)
cdef grpc_channel_args *c_arguments = arguments_processor.c(&self._vtable) self.c_server = grpc_server_create(channel_args.c_args(), NULL)
self.c_server = grpc_server_create(c_arguments, NULL)
arguments_processor.un_c()
self.references.append(arguments) self.references.append(arguments)
self.is_started = False self.is_started = False
self.is_shutting_down = False self.is_shutting_down = False

Loading…
Cancel
Save