Class MultiEndpoint

java.lang.Object
com.google.cloud.grpc.multiendpoint.MultiEndpoint

@CheckReturnValue public final class MultiEndpoint extends Object
MultiEndpoint holds a list of endpoints, tracks their availability and defines the current endpoint. An endpoint has a priority defined by its position in the list (first item has top priority). MultiEndpoint returns top priority endpoint that is available as current. If no endpoint is available, MultiEndpoint returns the top priority endpoint.

Sometimes switching between endpoints can be costly, and it is worth waiting for some time after current endpoint becomes unavailable. For this case, use MultiEndpoint.Builder.withRecoveryTimeout(java.time.Duration) to set the recovery timeout. MultiEndpoint will keep the current endpoint for up to recovery timeout after it became unavailable to give it some time to recover.

The list of endpoints can be changed at any time with setEndpoints(java.util.List<java.lang.String>) method. MultiEndpoint will preserve endpoints' state and update their priority according to their new positions.

After updating the list of endpoints, MultiEndpoint will switch the current endpoint to the top-priority available endpoint. If you have many processes using MultiEndpoint, this may lead to immediate shift of all traffic which may be undesired. To smooth this transfer, use MultiEndpoint.Builder.withSwitchingDelay(java.time.Duration) with randomized value to introduce a jitter. MultiEndpoint will delay switching from an available endpoint to another endpoint for this amount of time.

The initial state of endpoint is "unavailable" or "recovering" if using recovery timeout.

  • Method Details

    • getCurrentId

      public String getCurrentId()
      Returns current endpoint id.

      Note that the read is not synchronized and in case of a race condition there is a chance of getting an outdated current id.

    • getFallbackCnt

      public long getFallbackCnt()
    • getRecoverCnt

      public long getRecoverCnt()
    • getReplaceCnt

      public long getReplaceCnt()
    • getEndpoints

      public List<String> getEndpoints()
    • setEndpointAvailable

      public void setEndpointAvailable(String endpointId, boolean available)
      Inform MultiEndpoint when an endpoint becomes available or unavailable.
    • setEndpoints

      public void setEndpoints(List<String> endpoints)
      Provide an updated list of endpoints to MultiEndpoint.

      MultiEndpoint will preserve current endpoints' state and update their priority according to their new positions.

    • toString

      public String toString()
      Overrides:
      toString in class Object