Skip to content

Commit

Permalink
Add a simple API to provide a channel's connectivity state. (See #186.)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMage committed Mar 19, 2018
1 parent 9b9ba22 commit e68cec0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/CgRPC/shim/cgrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions Sources/CgRPC/shim/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 4 additions & 0 deletions Sources/SwiftGRPC/Core/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
32 changes: 32 additions & 0 deletions Sources/SwiftGRPC/Core/ConnectivityState.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit e68cec0

Please sign in to comment.