recycling of requestcallcontext

pull/13521/head
Jan Tattermusch 7 years ago
parent e8b331305c
commit fb6d01a30c
  1. 2
      src/csharp/Grpc.Core.Tests/Internal/CompletionQueueSafeHandleTest.cs
  2. 3
      src/csharp/Grpc.Core/Internal/NativeMethods.cs
  3. 22
      src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs
  4. 13
      src/csharp/ext/grpc_csharp_ext.c

@ -40,7 +40,7 @@ namespace Grpc.Core.Internal.Tests
public void CreateAsyncAndShutdown()
{
var env = GrpcEnvironment.AddRef();
var cq = CompletionQueueSafeHandle.CreateAsync(new CompletionRegistry(env, () => BatchContextSafeHandle.Create()));
var cq = CompletionQueueSafeHandle.CreateAsync(new CompletionRegistry(env, () => BatchContextSafeHandle.Create(), () => RequestCallContextSafeHandle.Create()));
cq.Shutdown();
var ev = cq.Next();
cq.Dispose();

@ -61,6 +61,7 @@ namespace Grpc.Core.Internal
public readonly Delegates.grpcsharp_request_call_context_host_delegate grpcsharp_request_call_context_host;
public readonly Delegates.grpcsharp_request_call_context_deadline_delegate grpcsharp_request_call_context_deadline;
public readonly Delegates.grpcsharp_request_call_context_request_metadata_delegate grpcsharp_request_call_context_request_metadata;
public readonly Delegates.grpcsharp_request_call_context_reset_delegate grpcsharp_request_call_context_reset;
public readonly Delegates.grpcsharp_request_call_context_destroy_delegate grpcsharp_request_call_context_destroy;
public readonly Delegates.grpcsharp_composite_call_credentials_create_delegate grpcsharp_composite_call_credentials_create;
@ -179,6 +180,7 @@ namespace Grpc.Core.Internal
this.grpcsharp_request_call_context_host = GetMethodDelegate<Delegates.grpcsharp_request_call_context_host_delegate>(library);
this.grpcsharp_request_call_context_deadline = GetMethodDelegate<Delegates.grpcsharp_request_call_context_deadline_delegate>(library);
this.grpcsharp_request_call_context_request_metadata = GetMethodDelegate<Delegates.grpcsharp_request_call_context_request_metadata_delegate>(library);
this.grpcsharp_request_call_context_reset = GetMethodDelegate<Delegates.grpcsharp_request_call_context_reset_delegate>(library);
this.grpcsharp_request_call_context_destroy = GetMethodDelegate<Delegates.grpcsharp_request_call_context_destroy_delegate>(library);
this.grpcsharp_composite_call_credentials_create = GetMethodDelegate<Delegates.grpcsharp_composite_call_credentials_create_delegate>(library);
@ -322,6 +324,7 @@ namespace Grpc.Core.Internal
public delegate IntPtr grpcsharp_request_call_context_host_delegate(RequestCallContextSafeHandle ctx, out UIntPtr hostLength);
public delegate Timespec grpcsharp_request_call_context_deadline_delegate(RequestCallContextSafeHandle ctx);
public delegate IntPtr grpcsharp_request_call_context_request_metadata_delegate(RequestCallContextSafeHandle ctx);
public delegate void grpcsharp_request_call_context_reset_delegate(RequestCallContextSafeHandle ctx);
public delegate void grpcsharp_request_call_context_destroy_delegate(IntPtr ctx);
public delegate CallCredentialsSafeHandle grpcsharp_composite_call_credentials_create_delegate(CallCredentialsSafeHandle creds1, CallCredentialsSafeHandle creds2);

@ -30,14 +30,17 @@ namespace Grpc.Core.Internal
{
static readonly NativeMethods Native = NativeMethods.Get();
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<RequestCallContextSafeHandle>();
IObjectPool<RequestCallContextSafeHandle> ownedByPool;
private RequestCallContextSafeHandle()
{
}
public static RequestCallContextSafeHandle Create()
public static RequestCallContextSafeHandle Create(IObjectPool<RequestCallContextSafeHandle> ownedByPool = null)
{
return Native.grpcsharp_request_call_context_create();
var ctx = Native.grpcsharp_request_call_context_create();
ctx.ownedByPool = ownedByPool;
return ctx;
}
public IntPtr Handle
@ -71,6 +74,19 @@ namespace Grpc.Core.Internal
return new ServerRpcNew(server, call, method, host, deadline, metadata);
}
public void Recycle()
{
if (ownedByPool != null)
{
Native.grpcsharp_request_call_context_reset(this);
ownedByPool.Return(this);
}
else
{
Dispose();
}
}
protected override bool ReleaseHandle()
{
Native.grpcsharp_request_call_context_destroy(handle);
@ -90,7 +106,7 @@ namespace Grpc.Core.Internal
finally
{
CompletionCallback = null;
Dispose();
Recycle();
}
}
}

@ -226,17 +226,22 @@ grpcsharp_batch_context_destroy(grpcsharp_batch_context* ctx) {
}
GPR_EXPORT void GPR_CALLTYPE
grpcsharp_request_call_context_destroy(grpcsharp_request_call_context* ctx) {
if (!ctx) {
return;
}
grpcsharp_request_call_context_reset(grpcsharp_request_call_context* ctx) {
/* NOTE: ctx->server_rpc_new.call is not destroyed because callback handler is
supposed
to take its ownership. */
grpc_call_details_destroy(&(ctx->call_details));
grpcsharp_metadata_array_destroy_metadata_only(&(ctx->request_metadata));
memset(ctx, 0, sizeof(grpcsharp_request_call_context));
}
GPR_EXPORT void GPR_CALLTYPE
grpcsharp_request_call_context_destroy(grpcsharp_request_call_context* ctx) {
if (!ctx) {
return;
}
grpcsharp_request_call_context_reset(ctx);
gpr_free(ctx);
}

Loading…
Cancel
Save