Add ChannelBase, change client base argument

pull/19599/head
James Newton-King 5 years ago
parent 0da6fcfa05
commit 6fbe9d916d
No known key found for this signature in database
GPG Key ID: A66B2F456BF5526
  1. 52
      src/csharp/Grpc.Core.Api/ChannelBase.cs
  2. 24
      src/csharp/Grpc.Core/Channel.cs
  3. 4
      src/csharp/Grpc.Core/ClientBase.cs

@ -0,0 +1,52 @@
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using Grpc.Core.Utils;
namespace Grpc.Core
{
/// <summary>
/// Base class for gRPC channel. Channels are an abstraction of long-lived connections to remote servers.
/// </summary>
public abstract class ChannelBase
{
private readonly string target;
/// <summary>
/// Initializes a new instance of <see cref="ChannelBase"/> class that connects to a specific host.
/// </summary>
/// <param name="target">Target of the channel.</param>
protected ChannelBase(string target)
{
this.target = GrpcPreconditions.CheckNotNull(target, nameof(target));
}
/// <summary>The original target used to create the channel.</summary>
public string Target
{
get { return this.target; }
}
/// <summary>
/// Create a new <see cref="CallInvoker"/> for the channel.
/// </summary>
/// <returns>A new <see cref="CallInvoker"/>.</returns>
public abstract CallInvoker CreateCallInvoker();
}
}

@ -30,7 +30,7 @@ namespace Grpc.Core
/// More client objects can reuse the same channel. Creating a channel is an expensive operation compared to invoking
/// a remote call so in general you should reuse a single channel for as many calls as possible.
/// </summary>
public class Channel
public class Channel : ChannelBase
{
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Channel>();
@ -38,7 +38,6 @@ namespace Grpc.Core
readonly AtomicCounter activeCallCounter = new AtomicCounter();
readonly CancellationTokenSource shutdownTokenSource = new CancellationTokenSource();
readonly string target;
readonly GrpcEnvironment environment;
readonly CompletionQueueSafeHandle completionQueue;
readonly ChannelSafeHandle handle;
@ -64,9 +63,8 @@ namespace Grpc.Core
/// <param name="target">Target of the channel.</param>
/// <param name="credentials">Credentials to secure the channel.</param>
/// <param name="options">Channel options.</param>
public Channel(string target, ChannelCredentials credentials, IEnumerable<ChannelOption> options)
public Channel(string target, ChannelCredentials credentials, IEnumerable<ChannelOption> options) : base(target)
{
this.target = GrpcPreconditions.CheckNotNull(target, "target");
this.options = CreateOptionsDictionary(options);
EnsureUserAgentChannelOption(this.options);
this.environment = GrpcEnvironment.AddRef();
@ -179,15 +177,6 @@ namespace Grpc.Core
}
}
/// <summary>The original target used to create the channel.</summary>
public string Target
{
get
{
return this.target;
}
}
/// <summary>
/// Returns a token that gets cancelled once <c>ShutdownAsync</c> is invoked.
/// </summary>
@ -257,6 +246,15 @@ namespace Grpc.Core
await GrpcEnvironment.ReleaseAsync().ConfigureAwait(false);
}
/// <summary>
/// Create a new <see cref="CallInvoker"/> for the channel.
/// </summary>
/// <returns>A new <see cref="CallInvoker"/>.</returns>
public override CallInvoker CreateCallInvoker()
{
return new DefaultCallInvoker(this);
}
internal ChannelSafeHandle Handle
{
get

@ -51,7 +51,7 @@ namespace Grpc.Core
/// Initializes a new instance of <c>ClientBase</c> class.
/// </summary>
/// <param name="channel">The channel to use for remote call invocation.</param>
public ClientBase(Channel channel) : base(channel)
public ClientBase(ChannelBase channel) : base(channel)
{
}
@ -113,7 +113,7 @@ namespace Grpc.Core
/// Initializes a new instance of <c>ClientBase</c> class.
/// </summary>
/// <param name="channel">The channel to use for remote call invocation.</param>
public ClientBase(Channel channel) : this(new DefaultCallInvoker(channel))
public ClientBase(ChannelBase channel) : this(channel.CreateCallInvoker())
{
}

Loading…
Cancel
Save