Java 类io.grpc.internal.SharedResourceHolder 实例源码

项目:indeed-grpc-java    文件:ConsulNameResolver.java   
@Override
public final synchronized void shutdown() {
    if (shutdown) {
        return;
    }
    shutdown = true;

    if (resolutionTask != null) {
        resolutionTask.cancel(false);
        resolutionTask = null;
    }

    if (timerService != null) {
        timerService = SharedResourceHolder.release(timerServiceResource, timerService);
    }

    if (executor != null) {
        executor = SharedResourceHolder.release(executorResource, executor);
    }
}
项目:grpc-java-by-example    文件:KubernetesNameResolver.java   
@Override
@GuardedBy("this")
public void refresh() {
  if (refreshing) return;
  try {
    refreshing = true;

    Endpoints endpoints = kubernetesClient.endpoints().inNamespace(namespace)
        .withName(name)
        .get();

    if (endpoints == null) {
      // Didn't find anything, retrying
      ScheduledExecutorService timerService = SharedResourceHolder.get(timerServiceResource);
      timerService.schedule(() -> {
        refresh();
      }, 30, TimeUnit.SECONDS);
      return;
    }

    update(endpoints);
    watch();
  } finally {
    refreshing = false;
  }
}
项目:grpc-java    文件:NettyServer.java   
@Override
protected void deallocate() {
  try {
    if (usingSharedBossGroup && bossGroup != null) {
      SharedResourceHolder.release(Utils.DEFAULT_BOSS_EVENT_LOOP_GROUP, bossGroup);
    }
  } finally {
    bossGroup = null;
    try {
      if (usingSharedWorkerGroup && workerGroup != null) {
        SharedResourceHolder.release(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP, workerGroup);
      }
    } finally {
      workerGroup = null;
    }
  }
}
项目:indeed-grpc-java    文件:ConsulNameResolver.java   
@Override
public final synchronized void start(final Listener listener) {
    checkState(this.listener == null, "ConsulNameResolver already started");
    timerService = SharedResourceHolder.get(timerServiceResource);
    executor = SharedResourceHolder.get(executorResource);
    this.listener = checkNotNull(listener, "listener cannot be null");
    resolve();
}
项目:jetcd    文件:SmartNameResolver.java   
@Override
public void start(Listener listener) {
  synchronized (lock) {
    Preconditions.checkState(this.listener == null, "already started");
    this.executor = SharedResourceHolder.get(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
    this.listener = Preconditions.checkNotNull(listener, "listener");
    resolve();
  }
}
项目:jetcd    文件:SmartNameResolver.java   
@Override
public void shutdown() {
  if (shutdown) {
    return;
  }
  shutdown = true;

  synchronized (lock) {
    if (executor != null) {
      executor = SharedResourceHolder.release(GrpcUtil.SHARED_CHANNEL_EXECUTOR, executor);
    }
  }
}
项目:grpc-java-by-example    文件:KubernetesNameResolver.java   
public KubernetesNameResolver(String namespace, String name, int port, Attributes params, SharedResourceHolder.Resource<ScheduledExecutorService> timerServiceResource, SharedResourceHolder.Resource<ExecutorService> sharedChannelExecutorResource) {
  this.namespace = namespace;
  this.name = name;
  this.port = port;
  this.params = params;
  this.timerServiceResource = timerServiceResource;
  this.sharedChannelExecutorResource = sharedChannelExecutorResource;
  this.kubernetesClient = new DefaultKubernetesClient();
}
项目:grpc-java    文件:OkHttpClientTransport.java   
/**
 * When the transport is in goAway state, we should stop it once all active streams finish.
 */
@GuardedBy("lock")
private void stopIfNecessary() {
  if (!(goAwayStatus != null && streams.isEmpty() && pendingStreams.isEmpty())) {
    return;
  }
  if (stopped) {
    return;
  }
  stopped = true;

  if (keepAliveManager != null) {
    keepAliveManager.onTransportTermination();
    // KeepAliveManager should stop using the scheduler after onTransportTermination gets called.
    scheduler = SharedResourceHolder.release(TIMER_SERVICE, scheduler);
  }

  if (ping != null) {
    ping.failed(getPingFailure());
    ping = null;
  }

  if (!goAwaySent) {
    // Send GOAWAY with lastGoodStreamId of 0, since we don't expect any server-initiated
    // streams. The GOAWAY is part of graceful shutdown.
    goAwaySent = true;
    frameWriter.goAway(0, ErrorCode.NO_ERROR, new byte[0]);
  }

  // We will close the underlying socket in the writing thread to break out the reader
  // thread, which will close the frameReader and notify the listener.
  frameWriter.close();
}
项目:grpc-java    文件:OkHttpChannelBuilder.java   
private OkHttpTransportFactory(Executor executor,
    @Nullable SSLSocketFactory socketFactory,
    @Nullable HostnameVerifier hostnameVerifier,
    ConnectionSpec connectionSpec,
    int maxMessageSize,
    boolean enableKeepAlive,
    long keepAliveTimeNanos,
    long keepAliveTimeoutNanos,
    boolean keepAliveWithoutCalls,
    TransportTracer.Factory transportTracerFactory) {
  this.socketFactory = socketFactory;
  this.hostnameVerifier = hostnameVerifier;
  this.connectionSpec = connectionSpec;
  this.maxMessageSize = maxMessageSize;
  this.enableKeepAlive = enableKeepAlive;
  this.keepAliveTimeNanos = new AtomicBackoff("keepalive time nanos", keepAliveTimeNanos);
  this.keepAliveTimeoutNanos = keepAliveTimeoutNanos;
  this.keepAliveWithoutCalls = keepAliveWithoutCalls;

  usingSharedExecutor = executor == null;
  this.transportTracerFactory =
      Preconditions.checkNotNull(transportTracerFactory, "transportTracerFactory");
  if (usingSharedExecutor) {
    // The executor was unspecified, using the shared executor.
    this.executor = SharedResourceHolder.get(SHARED_EXECUTOR);
  } else {
    this.executor = executor;
  }
}
项目:grpc-java    文件:OkHttpChannelBuilder.java   
@Override
public void close() {
  if (closed) {
    return;
  }
  closed = true;
  SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, timeoutService);

  if (usingSharedExecutor) {
    SharedResourceHolder.release(SHARED_EXECUTOR, (ExecutorService) executor);
  }
}
项目:grpc-java    文件:NettyServer.java   
private void allocateSharedGroups() {
  if (bossGroup == null) {
    bossGroup = SharedResourceHolder.get(Utils.DEFAULT_BOSS_EVENT_LOOP_GROUP);
  }
  if (workerGroup == null) {
    workerGroup = SharedResourceHolder.get(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP);
  }
}
项目:grpc-java    文件:NettyChannelBuilder.java   
NettyTransportFactory(TransportCreationParamsFilterFactory transportCreationParamsFilterFactory,
    Class<? extends Channel> channelType, Map<ChannelOption<?>, ?> channelOptions,
    NegotiationType negotiationType, SslContext sslContext, EventLoopGroup group,
    int flowControlWindow, int maxMessageSize, int maxHeaderListSize,
    long keepAliveTimeNanos, long keepAliveTimeoutNanos, boolean keepAliveWithoutCalls,
    TransportTracer transportTracer) {
  this.channelType = channelType;
  this.negotiationType = negotiationType;
  this.channelOptions = new HashMap<ChannelOption<?>, Object>(channelOptions);
  this.transportTracer = transportTracer;

  if (transportCreationParamsFilterFactory == null) {
    transportCreationParamsFilterFactory =
        new DefaultNettyTransportCreationParamsFilterFactory(sslContext);
  }
  this.transportCreationParamsFilterFactory = transportCreationParamsFilterFactory;

  this.flowControlWindow = flowControlWindow;
  this.maxMessageSize = maxMessageSize;
  this.maxHeaderListSize = maxHeaderListSize;
  this.keepAliveTimeNanos = new AtomicBackoff("keepalive time nanos", keepAliveTimeNanos);
  this.keepAliveTimeoutNanos = keepAliveTimeoutNanos;
  this.keepAliveWithoutCalls = keepAliveWithoutCalls;
  usingSharedGroup = group == null;
  if (usingSharedGroup) {
    // The group was unspecified, using the shared group.
    this.group = SharedResourceHolder.get(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP);
  } else {
    this.group = group;
  }
}
项目:grpc-java    文件:NettyChannelBuilder.java   
@Override
public void close() {
  if (closed) {
    return;
  }
  closed = true;

  if (usingSharedGroup) {
    SharedResourceHolder.release(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP, group);
  }
}
项目:grpc-java    文件:InProcessChannelBuilder.java   
@Override
public void close() {
  if (closed) {
    return;
  }
  closed = true;
  SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, timerService);
}
项目:grpc-java    文件:CronetChannelBuilder.java   
@Override
public void close() {
  SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, timeoutService);
}