question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Q] How to use custom ThreadPool's and WorkQueue for GRPCServers?

See original GitHub issue

We can achieve this by using the GrpcEnvironment class here but as we know that Grpc.Core lib got Deprecated so the only way to create GRPCServer is via native ASP.Core gRPCServer’s but I need some suggestions on creating Server with custom threadpools like in JAVA.

Java’s gRPC Server class with Executor.

public abstract class ServerBuilder<T extends ServerBuilder<T>> {
    public ServerBuilder() {
    }

    public static ServerBuilder<?> forPort(int port) {
        return ServerProvider.provider().builderForPort(port);
    }

    public abstract T directExecutor();

    public abstract T executor(@Nullable Executor var1);

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8274")
    public T callExecutor(ServerCallExecutorSupplier executorSupplier) {
        return this.thisT();
    }

    public abstract T addService(ServerServiceDefinition var1);

    public abstract T addService(BindableService var1);

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/7925")
    public final T addServices(List<ServerServiceDefinition> services) {
        Preconditions.checkNotNull(services, "services");
        Iterator var2 = services.iterator();

        while(var2.hasNext()) {
            ServerServiceDefinition service = (ServerServiceDefinition)var2.next();
            this.addService(service);
        }

        return this.thisT();
    }

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/3117")
    public T intercept(ServerInterceptor interceptor) {
        throw new UnsupportedOperationException();
    }

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2132")
    public T addTransportFilter(ServerTransportFilter filter) {
        throw new UnsupportedOperationException();
    }

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861")
    public T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
        throw new UnsupportedOperationException();
    }

    public abstract T fallbackHandlerRegistry(@Nullable HandlerRegistry var1);

    public abstract T useTransportSecurity(File var1, File var2);

    public T useTransportSecurity(InputStream certChain, InputStream privateKey) {
        throw new UnsupportedOperationException();
    }

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
    public abstract T decompressorRegistry(@Nullable DecompressorRegistry var1);

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
    public abstract T compressorRegistry(@Nullable CompressorRegistry var1);

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/3706")
    public T handshakeTimeout(long timeout, TimeUnit unit) {
        throw new UnsupportedOperationException();
    }

    public T maxInboundMessageSize(int bytes) {
        Preconditions.checkArgument(bytes >= 0, "bytes must be >= 0");
        return this.thisT();
    }

    public T maxInboundMetadataSize(int bytes) {
        Preconditions.checkArgument(bytes > 0, "maxInboundMetadataSize must be > 0");
        return this.thisT();
    }

    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017")
    public T setBinaryLog(BinaryLog binaryLog) {
        throw new UnsupportedOperationException();
    }

    public abstract Server build();

    private T thisT() {
        return this;
    }
}

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, Mar 28, 2023

There is no thread pool, queuing, or execution logic in Grpc.AspNetCore. It exists in ASP.NET Core which Grpc.AspNetCore runs on top of.

This is not appropriate for Grpc.AspNetCore.

0reactions
Jeevananthan-23commented, Mar 28, 2023

@JamesNK, Let me explain my uses case:

  • GrpcServer should have the pool of threads that WorkerQueue/set of completions queues ( the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method).
 Server server= ServerBuilder.forPort(ServerConfiguration.getPort())
            .addService(ServerInterceptors.intercept(serverImpl, monitoringInterceptor))
            .addService(ProtoReflectionService.newInstance())
            .executor(
                ThreadPoolExecutorFactory.getThreadPoolExecutor(
                    ThreadPoolExecutorFactory.ExecutorType.SERVER,
                    ServerConfiguration.getThreadPoolConfiguration()))
            .maxInboundMessageSize(MAX_MESSAGE_BYTES_SIZE)
            .compressorRegistry(ServerStubBuilder.COMPRESSOR_REGISTRY)
            .decompressorRegistry(ServerStubBuilder.DECOMPRESSOR_REGISTRY)
            .build()
            .start();
 public static ThreadPoolExecutor getThreadPoolExecutor(
   ExecutorType executorType, ThreadPoolConfiguration threadPoolConfiguration) {
 ThreadPoolExecutor threadPoolExecutor;
 if (executorType.equals(ExecutorType.SERVER)) {
   logger.info(
       "Creating GrpcServerExecutor of size "
           + threadPoolConfiguration.getMaxGrpcserverThreads());
   BlockingQueue<Runnable> docsToIndex =
       new LinkedBlockingQueue<Runnable>(
           threadPoolConfiguration.getMaxGrpcserverBufferedItems());
   threadPoolExecutor =
       new ThreadPoolExecutor(
           threadPoolConfiguration.getMaxGrpcserverThreads(),
           threadPoolConfiguration.getMaxGrpcserverThreads(),
           0,
           TimeUnit.SECONDS,
           docsToIndex,
           new NamedThreadFactory("GrpcServerExecutor"));
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How to do client side load-balancing with custom ...
This question is slightly different. How can I pass executor to grpc.insecure_channel? gRPC client doesn't accept executor as an input.
Read more >
Java Development Practices: Using Thread Pools and ...
You can create a thread pool by using new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory ...
Read more >
Threading Model - Apache Camel
Contents. Thread pool profiles; Configuring default thread pool profile; Using thread pool profiles; Creating custom thread pools; Customizing thread names ...
Read more >
ThreadPoolExecutor - Java Thread Pool Example
We can use ThreadPoolExecutor to create thread pool in Java. ... Here is our custom implementation of RejectedExecutionHandler interface.
Read more >
ThreadPool.QueueUserWorkItem Method
Queues a method for execution. The method executes when a thread pool thread becomes available.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found