Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore executor configuration for zio-grpc #443

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void start() throws IOException {
* <p>
* <ul>
* <li>JVM_EXECUTOR_TYPE: direct, workStealing, single, fixed, cached</li>
* <li>JVM_EXECUTOR_THREADS: integer value.</li>
* <li>GRPC_SERVER_CPUS: integer value.</li>
* </ul>
* </p>
*
Expand All @@ -66,7 +66,7 @@ private void start() throws IOException {
* this value.
*/
private ServerBuilder<?> configureExecutor(ServerBuilder<?> sb) {
var threads = System.getenv("JVM_EXECUTOR_THREADS");
var threads = System.getenv("GRPC_SERVER_CPUS");
var i_threads = Runtime.getRuntime().availableProcessors();
if (threads != null && !threads.isEmpty()) {
i_threads = Integer.parseInt(threads);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ServerBuilder<?> onCreated(BeanCreatedEvent<ServerBuilder<?>> event) {
* <p>
* <ul>
* <li>JVM_EXECUTOR_TYPE: direct, workStealing, single, fixed, cached</li>
* <li>JVM_EXECUTOR_THREADS: integer value.</li>
* <li>GRPC_SERVER_CPUS: integer value.</li>
* </ul>
* </p>
*
Expand All @@ -36,7 +36,7 @@ public ServerBuilder<?> onCreated(BeanCreatedEvent<ServerBuilder<?>> event) {
* this value.
*/
private ServerBuilder<?> configureExecutor(ServerBuilder<?> sb) {
var threads = System.getenv("JVM_EXECUTOR_THREADS");
var threads = System.getenv("GRPC_SERVER_CPUS");
var i_threads = Runtime.getRuntime().availableProcessors();
if (threads != null && !threads.isEmpty()) {
i_threads = Integer.parseInt(threads);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HelloWorldServer(val port: Int) {
val server: Server

init {
val threads = System.getenv("JVM_EXECUTOR_THREADS")
val threads = System.getenv("GRPC_SERVER_CPUS")
var i_threads = Runtime.getRuntime().availableProcessors()
if (threads != null && !threads.isEmpty()) {
i_threads = Integer.parseInt(threads)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ object GreeterServer extends IOApp {
* <p>
* <ul>
* <li>JVM_EXECUTOR_TYPE: direct, workStealing, single, fixed, cached</li>
* <li>JVM_EXECUTOR_THREADS: integer value.</li>
* <li>GRPC_SERVER_CPUS: integer value.</li>
* </ul>
* </p>
*
* The number of Executor Threads will default to the number of
* availableProcessors(). Only the workStealing and fixed executors will use
* this value.
*/
val threads = System.getenv("JVM_EXECUTOR_THREADS")
val threads = System.getenv("GRPC_SERVER_CPUS")
var i_threads = Runtime.getRuntime.availableProcessors
if (threads != null && !threads.isEmpty) i_threads = threads.toInt
val value = System.getenv.getOrDefault("JVM_EXECUTOR_TYPE", "workStealing")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
package com.example.helloworld

import scalapb.zio_grpc.{ServerMain, ServiceList}
import scalapb.zio_grpc.{Server, ServerLayer, ServerMain, ServiceList}
import zio.ZLayer

import java.util.concurrent.Executors

object GreeterServer extends ServerMain {
def services: ServiceList[Any] = ServiceList.add(GreeterImpl)

// Default port is 9000
override def port = 50051

override def serverLive: ZLayer[Any, Throwable, Server] = {
val sb = builder

/**
* Allow customization of the Executor with two environment variables:
*
* <p>
* <ul>
* <li>JVM_EXECUTOR_TYPE: direct, workStealing, single, fixed, cached</li>
* <li>GRPC_SERVER_CPUS: integer value.</li>
* </ul>
* </p>
*
* The number of Executor Threads will default to the number of
* availableProcessors(). Only the workStealing and fixed executors will use
* this value.
*/
val threads = System.getenv("GRPC_SERVER_CPUS")
var i_threads = Runtime.getRuntime.availableProcessors
if (threads != null && !threads.isEmpty) i_threads = threads.toInt
val value = System.getenv.getOrDefault("JVM_EXECUTOR_TYPE", "workStealing")
value match {
case "direct" => sb.directExecutor
case "single" => sb.executor(Executors.newSingleThreadExecutor)
case "fixed" => sb.executor(Executors.newFixedThreadPool(i_threads))
case "workStealing" => sb.executor(Executors.newWorkStealingPool(i_threads))
case "cached" => sb.executor(Executors.newCachedThreadPool)
}

ServerLayer.fromServiceList(sb, services)
}
}
Loading