ProtoReflectionService.java 文件源码

java
阅读 41 收藏 0 点赞 0 评论 0

项目:grpc-java 作者:
/**
 * Checks for updates to the server's mutable services and updates the index if any changes are
 * detected. A change is any addition or removal in the set of file descriptors attached to the
 * mutable services or a change in the service names.
 *
 * @return The (potentially updated) index.
 */
private ServerReflectionIndex updateIndexIfNecessary() {
  synchronized (lock) {
    if (serverReflectionIndex == null) {
      serverReflectionIndex =
          new ServerReflectionIndex(server.getImmutableServices(), server.getMutableServices());
      return serverReflectionIndex;
    }

    Set<FileDescriptor> serverFileDescriptors = new HashSet<FileDescriptor>();
    Set<String> serverServiceNames = new HashSet<String>();
    List<ServerServiceDefinition> serverMutableServices = server.getMutableServices();
    for (ServerServiceDefinition mutableService : serverMutableServices) {
      io.grpc.ServiceDescriptor serviceDescriptor = mutableService.getServiceDescriptor();
      if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) {
        String serviceName = serviceDescriptor.getName();
        FileDescriptor fileDescriptor =
            ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor())
                .getFileDescriptor();
        serverFileDescriptors.add(fileDescriptor);
        serverServiceNames.add(serviceName);
      }
    }

    // Replace the index if the underlying mutable services have changed. Check both the file
    // descriptors and the service names, because one file descriptor can define multiple
    // services.
    FileDescriptorIndex mutableServicesIndex = serverReflectionIndex.getMutableServicesIndex();
    if (!mutableServicesIndex.getServiceFileDescriptors().equals(serverFileDescriptors)
        || !mutableServicesIndex.getServiceNames().equals(serverServiceNames)) {
      serverReflectionIndex =
          new ServerReflectionIndex(server.getImmutableServices(), serverMutableServices);
    }

    return serverReflectionIndex;
  }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号