Skip to content

Commit

Permalink
feat: Dispose channel on client shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh-V committed May 5, 2023
1 parent 5f3808f commit d38ab04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,25 @@ private async Task<PublisherClient> BuildAsyncImpl(CancellationToken cancellatio
? await builder.BuildAsync(cancellationToken).ConfigureAwait(false)
: builder.Build();
var channel = builder.LastCreatedChannel;
shutdowns[i] = channel is null ? () => Task.CompletedTask : channel.ShutdownAsync;
shutdowns[i] = () => DisposeChannelAsync(channel);
}
Func<Task> shutdown = () => Task.WhenAll(shutdowns.Select(x => x()));
return new PublisherClientImpl(TopicName, clients, settings, shutdown);

// TODO: Move this local method to a common place. We have it here and in SubscriberClientBuilder.
static Task DisposeChannelAsync(Grpc.Core.ChannelBase channel)
{
if (channel is null)
{
return Task.CompletedTask;
}
if (channel is IDisposable disposable)
{
disposable.Dispose();
}

return channel.ShutdownAsync();
}
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,25 @@ private async Task<SubscriberClient> BuildAsyncImpl(CancellationToken cancellati
? await builder.BuildAsync(cancellationToken).ConfigureAwait(false)
: builder.Build();
var channel = builder.LastCreatedChannel;
shutdowns[i] = channel is null ? () => Task.CompletedTask : channel.ShutdownAsync;
shutdowns[i] = () => DisposeChannelAsync(channel);
}
Func<Task> shutdown = () => Task.WhenAll(shutdowns.Select(x => x()));
return new SubscriberClientImpl(SubscriptionName, clients, settings, shutdown);

// TODO: Move this local method to a common place. We have it here and in PublisherClientBuilder.
static Task DisposeChannelAsync(Grpc.Core.ChannelBase channel)
{
if (channel is null)
{
return Task.CompletedTask;
}
if (channel is IDisposable disposable)
{
disposable.Dispose();
}

return channel.ShutdownAsync();
}
}

/// <inheritdoc />
Expand Down

0 comments on commit d38ab04

Please sign in to comment.