Java Code Examples for io.grpc.ConnectivityState#SHUTDOWN

The following examples show how to use io.grpc.ConnectivityState#SHUTDOWN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectivityStateManager.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
/**
 * Connectivity state is changed to the specified value. Will trigger some notifications that have
 * been registered earlier by {@link ManagedChannel#notifyWhenStateChanged}.
 */
void gotoState(@Nonnull ConnectivityState newState) {
  checkNotNull(newState, "newState");
  if (state != newState && state != ConnectivityState.SHUTDOWN) {
    state = newState;
    if (listeners.isEmpty()) {
      return;
    }
    // Swap out callback list before calling them, because a callback may register new callbacks,
    // if run in direct executor, can cause ConcurrentModificationException.
    ArrayList<Listener> savedListeners = listeners;
    listeners = new ArrayList<>();
    for (Listener listener : savedListeners) {
      listener.runInExecutor();
    }
  }
}
 
Example 2
Source File: TripleClientTransport.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected boolean channelAvailable(ManagedChannel channel) {
    if (channel == null) {
        return false;
    }
    ConnectivityState state = channel.getState(false);
    if (ConnectivityState.READY == state) {
        return true;
    }
    if (ConnectivityState.SHUTDOWN == state || ConnectivityState.TRANSIENT_FAILURE == state) {
        return false;
    }
    if (ConnectivityState.IDLE == state || ConnectivityState.CONNECTING == state) {
        return true;
    }
    return false;
}
 
Example 3
Source File: ConnectivityStateManager.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * Connectivity state is changed to the specified value. Will trigger some notifications that have
 * been registered earlier by {@link ManagedChannel#notifyWhenStateChanged}.
 */
void gotoState(@Nonnull ConnectivityState newState) {
  checkNotNull(newState, "newState");
  if (state != newState && state != ConnectivityState.SHUTDOWN) {
    state = newState;
    if (listeners.isEmpty()) {
      return;
    }
    // Swap out callback list before calling them, because a callback may register new callbacks,
    // if run in direct executor, can cause ConcurrentModificationException.
    ArrayList<Listener> savedListeners = listeners;
    listeners = new ArrayList<>();
    for (Listener listener : savedListeners) {
      listener.runInExecutor();
    }
  }
}
 
Example 4
Source File: AbstractChannelFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Watch the given channel for connectivity changes.
 *
 * @param name The name of the channel in the state overview.
 * @param channel The channel to watch the state of.
 */
protected void watchConnectivityState(final String name, final ManagedChannel channel) {
    final ConnectivityState state = channel.getState(false);
    this.channelStates.put(name, state);
    if (state != ConnectivityState.SHUTDOWN) {
        channel.notifyWhenStateChanged(state, () -> watchConnectivityState(name, channel));
    }
}
 
Example 5
Source File: AbstractChannelFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Watch the given channel for connectivity changes.
 *
 * @param name The name of the channel in the state overview.
 * @param channel The channel to watch the state of.
 */
protected void watchConnectivityState(final String name, final ManagedChannel channel) {
    final ConnectivityState state = channel.getState(false);
    this.channelStates.put(name, state);
    if (state != ConnectivityState.SHUTDOWN) {
        channel.notifyWhenStateChanged(state, () -> watchConnectivityState(name, channel));
    }
}
 
Example 6
Source File: SubchannelStateManagerImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void updateState(String name, ConnectivityState newState) {
  checkNotNull(name, "name");
  checkNotNull(newState, "newState");
  ConnectivityState existing;
  if (newState == ConnectivityState.SHUTDOWN) {
    existing = stateMap.remove(name);
  } else {
    existing = stateMap.put(name, newState);
    stateMultiset.add(newState);
  }
  if (existing != null) {
    stateMultiset.remove(existing);
  }
}
 
Example 7
Source File: SubchannelStateManagerImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getAggregatedStatus_single() {
  for (ConnectivityState value : ConnectivityState.values()) {
    if (value == ConnectivityState.SHUTDOWN) {
      continue;
    }
    SubchannelStateManager stateManager = new SubchannelStateManagerImpl();

    stateManager.updateState("foo", value);

    assertThat(stateManager.getAggregatedState()).isEqualTo(value);
  }
}
 
Example 8
Source File: AbstractGrpcClient.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Invoked at each change of the channel connectivity state. New callbacks
 * are created as long as the channel is not shut down.
 */
private void channelStateCallback() {
    final ConnectivityState newState = channel.getState(false);
    final DeviceAgentEvent.Type eventType;
    switch (newState) {
        // On gRPC connectivity states:
        // https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md
        case READY:
            eventType = DeviceAgentEvent.Type.CHANNEL_OPEN;
            break;
        case TRANSIENT_FAILURE:
            eventType = DeviceAgentEvent.Type.CHANNEL_ERROR;
            break;
        case SHUTDOWN:
            eventType = DeviceAgentEvent.Type.CHANNEL_CLOSED;
            break;
        case IDLE:
            // IDLE and CONNECTING are transient states that will
            // eventually move to READY or TRANSIENT_FAILURE. Do not
            // generate an event for now.
            if (persistent) {
                log.debug("Forcing channel for {} to exist state IDLE...", deviceId);
                channel.getState(true);
            }
            eventType = null;
            break;
        case CONNECTING:
            eventType = null;
            break;
        default:
            log.error("Unrecognized connectivity state {}", newState);
            eventType = null;
    }

    if (log.isTraceEnabled()) {
        log.trace("Detected channel connectivity change for {}, new state is {}",
                  deviceId, newState);
    }

    if (eventType != null) {
        // Avoid sending consecutive duplicate events.
        final boolean present = eventType == DeviceAgentEvent.Type.CHANNEL_OPEN;
        final boolean past = channelOpen.getAndSet(present);
        if (present != past) {
            log.debug("Notifying event {} for {}", eventType, deviceId);
            controller.postEvent(new DeviceAgentEvent(eventType, deviceId));
        }
    }

    if (newState != ConnectivityState.SHUTDOWN) {
        // Channels never leave SHUTDOWN state, no need for a new callback.
        setChannelCallback(newState);
    }
}