ChannelBase.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading.Tasks;
  18. using Grpc.Core.Utils;
  19. namespace Grpc.Core
  20. {
  21. /// <summary>
  22. /// Base class for gRPC channel. Channels are an abstraction of long-lived connections to remote servers.
  23. /// </summary>
  24. public abstract class ChannelBase
  25. {
  26. private readonly string target;
  27. /// <summary>
  28. /// Initializes a new instance of <see cref="ChannelBase"/> class that connects to a specific host.
  29. /// </summary>
  30. /// <param name="target">Target of the channel.</param>
  31. protected ChannelBase(string target)
  32. {
  33. this.target = GrpcPreconditions.CheckNotNull(target, nameof(target));
  34. }
  35. /// <summary>The original target used to create the channel.</summary>
  36. public string Target
  37. {
  38. get { return this.target; }
  39. }
  40. /// <summary>
  41. /// Create a new <see cref="CallInvoker"/> for the channel.
  42. /// </summary>
  43. /// <returns>A new <see cref="CallInvoker"/>.</returns>
  44. public abstract CallInvoker CreateCallInvoker();
  45. /// <summary>
  46. /// Shuts down the channel cleanly. It is strongly recommended to shutdown
  47. /// the channel once you stopped using it.
  48. /// </summary>
  49. /// <remarks>
  50. /// Guidance for implementors:
  51. /// This method doesn't wait for all calls on this channel to finish (nor does
  52. /// it have to explicitly cancel all outstanding calls). It is user's responsibility to make sure
  53. /// all the calls on this channel have finished (successfully or with an error)
  54. /// before shutting down the channel to ensure channel shutdown won't impact
  55. /// the outcome of those remote calls.
  56. /// </remarks>
  57. public Task ShutdownAsync()
  58. {
  59. return ShutdownAsyncCore();
  60. }
  61. /// <summary>Provides implementation of a non-virtual public member.</summary>
  62. #pragma warning disable 1998
  63. protected virtual async Task ShutdownAsyncCore()
  64. {
  65. // default implementation is no-op for backwards compatibility, but all implementations
  66. // are expected to override this method.
  67. // warning 1998 is disabled to avoid needing TaskUtils.CompletedTask, which is
  68. // only available in Grpc.Core
  69. }
  70. #pragma warning restore 1998
  71. }
  72. }