diff --git a/src/main/java/com/google/devtools/build/lib/remote/BUILD b/src/main/java/com/google/devtools/build/lib/remote/BUILD index df9e8a31d22fa4..3f1b39f731533a 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/BUILD +++ b/src/main/java/com/google/devtools/build/lib/remote/BUILD @@ -10,6 +10,7 @@ filegroup( "//src/main/java/com/google/devtools/build/lib/remote/common:srcs", "//src/main/java/com/google/devtools/build/lib/remote/downloader:srcs", "//src/main/java/com/google/devtools/build/lib/remote/disk:srcs", + "//src/main/java/com/google/devtools/build/lib/remote/grpc:srcs", "//src/main/java/com/google/devtools/build/lib/remote/http:srcs", "//src/main/java/com/google/devtools/build/lib/remote/logging:srcs", "//src/main/java/com/google/devtools/build/lib/remote/merkletree:srcs", diff --git a/src/main/java/com/google/devtools/build/lib/remote/grpc/BUILD b/src/main/java/com/google/devtools/build/lib/remote/grpc/BUILD new file mode 100644 index 00000000000000..cca37d8e1350a7 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/remote/grpc/BUILD @@ -0,0 +1,22 @@ +load("@rules_java//java:defs.bzl", "java_library") + +package( + default_visibility = ["//src:__subpackages__"], +) + +licenses(["notice"]) + +filegroup( + name = "srcs", + srcs = glob(["*"]), + visibility = ["//src:__subpackages__"], +) + +java_library( + name = "grpc", + srcs = glob(["*.java"]), + deps = [ + "//third_party:rxjava3", + "//third_party/grpc:grpc-jar", + ], +) diff --git a/src/main/java/com/google/devtools/build/lib/remote/grpc/Connection.java b/src/main/java/com/google/devtools/build/lib/remote/grpc/Connection.java new file mode 100644 index 00000000000000..995f4ed659da62 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/remote/grpc/Connection.java @@ -0,0 +1,37 @@ +// Copyright 2021 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.remote.grpc; + +import io.grpc.CallOptions; +import io.grpc.ClientCall; +import io.grpc.MethodDescriptor; +import java.io.Closeable; +import java.io.IOException; + +/** + * A single connection to a server. RPCs are executed within the context of a connection. A {@link + * Connection} object can consist of any number of transport connections. + * + *

Connections must be closed to ensure proper resource disposal. + */ +public interface Connection extends Closeable { + + /** Creates a new {@link ClientCall} for issuing RPC. */ + ClientCall call( + MethodDescriptor method, CallOptions options); + + /** Releases any resources held by the {@link Connection}. */ + @Override + void close() throws IOException; +} diff --git a/src/main/java/com/google/devtools/build/lib/remote/grpc/ConnectionFactory.java b/src/main/java/com/google/devtools/build/lib/remote/grpc/ConnectionFactory.java new file mode 100644 index 00000000000000..462a77e869f55e --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/remote/grpc/ConnectionFactory.java @@ -0,0 +1,32 @@ +// Copyright 2021 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.remote.grpc; + +import io.reactivex.rxjava3.core.Single; + +/** + * A {@link ConnectionFactory} represents a resource factory for connection creation. It may create + * connections by itself, wrap a {@link ConnectionFactory}, or apply connection pooling on top of a + * {@link ConnectionFactory}. + * + *

A {@link ConnectionFactory} uses deferred initialization and should initiate connection + * resource allocation after subscription. + * + *

Connection creation must be cancellable. Canceling connection creation must release (“close”) + * the connection and all associated resources. + */ +public interface ConnectionFactory { + /** Creates a new {@link Connection}. */ + Single create(); +} diff --git a/src/main/java/com/google/devtools/build/lib/remote/grpc/ConnectionPool.java b/src/main/java/com/google/devtools/build/lib/remote/grpc/ConnectionPool.java new file mode 100644 index 00000000000000..ee513847873e24 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/remote/grpc/ConnectionPool.java @@ -0,0 +1,30 @@ +// Copyright 2021 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.remote.grpc; + +import java.io.Closeable; +import java.io.IOException; + +/** + * A {@link ConnectionFactory} that apply connection pooling. Connections created by {@link + * ConnectionPool} will not be closed by {@link Connection#close()}, use {@link + * ConnectionPool#close()} instead to close all the connections in the pool. + * + *

Connections must be closed with {@link Connection#close()} in order to be reused later. + */ +public interface ConnectionPool extends ConnectionFactory, Closeable { + /** Closes the connection pool and closes all the underlying connections */ + @Override + void close() throws IOException; +}