Browse Source

Add a simple API to provide a channel's connectivity state. (See #186.)

Daniel Alm 7 years ago
parent
commit
e68cec0db8

+ 19 - 0
Sources/CgRPC/shim/cgrpc.h

@@ -91,6 +91,22 @@ typedef enum grpc_completion_type {
   GRPC_OP_COMPLETE
 } grpc_completion_type;
 
+/** Connectivity state of a channel. */
+typedef enum grpc_connectivity_state {
+  /** channel has just been initialized */
+  GRPC_CHANNEL_INIT = -1,
+  /** channel is idle */
+  GRPC_CHANNEL_IDLE,
+  /** channel is connecting */
+  GRPC_CHANNEL_CONNECTING,
+  /** channel is ready for work */
+  GRPC_CHANNEL_READY,
+  /** channel has seen a failure but expects to recover */
+  GRPC_CHANNEL_TRANSIENT_FAILURE,
+  /** channel has seen a failure that it cannot recover from */
+  GRPC_CHANNEL_SHUTDOWN
+} grpc_connectivity_state;
+
 typedef struct grpc_event {
   /** The type of the completion. */
   grpc_completion_type type;
@@ -129,6 +145,9 @@ cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
                                       double timeout);
 cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel);
 
+grpc_connectivity_state cgrpc_channel_check_connectivity_state(
+    cgrpc_channel *channel, int try_to_connect);
+
 // server support
 cgrpc_server *cgrpc_server_create(const char *address);
 cgrpc_server *cgrpc_server_create_secure(const char *address,

+ 4 - 0
Sources/CgRPC/shim/channel.c

@@ -101,3 +101,7 @@ cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
 cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel) {
   return channel->completion_queue;
 }
+
+grpc_connectivity_state cgrpc_channel_check_connectivity_state(cgrpc_channel *channel, int try_to_connect) {
+  return grpc_channel_check_connectivity_state(channel->channel, try_to_connect);
+}

+ 4 - 0
Sources/SwiftGRPC/Core/Channel.swift

@@ -31,6 +31,10 @@ public class Channel {
 
   /// Default host to use for new calls
   public var host: String
+  
+  public var connectivityState: ConnectivityState? {
+    return ConnectivityState.fromCEnum(cgrpc_channel_check_connectivity_state(underlyingChannel, 0))
+  }
 
   /// Initializes a gRPC channel
   ///

+ 32 - 0
Sources/SwiftGRPC/Core/ConnectivityState.swift

@@ -0,0 +1,32 @@
+/*
+ * Copyright 2018, gRPC Authors All rights reserved.
+ *
+ * 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.
+ */
+#if SWIFT_PACKAGE
+  import CgRPC
+#endif
+import Foundation
+
+public enum ConnectivityState: Int32, Error {
+  case initializing = -1
+  case idle
+  case connecting
+  case ready
+  case transient_failure
+  case shutdown
+  
+  static func fromCEnum(_ connectivityState: grpc_connectivity_state) -> ConnectivityState? {
+    return ConnectivityState(rawValue: connectivityState.rawValue)
+  }
+}