Skip to content

Commit

Permalink
🏗️ Receive shutdown signal via RPC and terminate the gRPC server.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoGraham committed Apr 10, 2024
1 parent e4ccec2 commit e50cee1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.robertograham.tpp4j;

import com.hashicorp.goplugin.Empty;
import com.hashicorp.goplugin.GRPCControllerGrpc.GRPCControllerImplBase;
import io.grpc.stub.StreamObserver;

final class DefaultGrpcController extends GRPCControllerImplBase {

final ProviderServer providerServer;

DefaultGrpcController(final ProviderServer providerServer) {
this.providerServer = providerServer;
}

@Override
public void shutdown(final Empty request, final StreamObserver<Empty> responseObserver) {
System.err.println("[INFO] Shutting down");

providerServer.stop();

responseObserver.onNext(Empty.newBuilder()
.build());
responseObserver.onCompleted();
}
}
12 changes: 2 additions & 10 deletions src/main/java/io/github/robertograham/tpp4j/ProviderServer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.github.robertograham.tpp4j;

import io.grpc.Attributes;
import io.grpc.Grpc;
import io.grpc.Server;
import io.grpc.ServerTransportFilter;
import io.grpc.TlsServerCredentials;
import java.io.IOException;
import java.io.StringWriter;
Expand Down Expand Up @@ -60,12 +58,7 @@ void start() throws IOException, GeneralSecurityException {
.clientAuth(TlsServerCredentials.ClientAuth.REQUIRE)
.build())
.addService(new DefaultProvider())
.addTransportFilter(new ServerTransportFilter() {
@Override
public void transportTerminated(final Attributes attributes) {
stop();
}
})
.addService(new DefaultGrpcController(this))
.build()
.start();
}
Expand All @@ -83,9 +76,8 @@ void blockUntilShutdown() throws InterruptedException {
}
}

private void stop() {
void stop() {
if (server != null) {
System.err.println("[INFO] Shutting down provider server");
server.shutdown();
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/proto/plugin/grpc_controller.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

syntax = "proto3";
package plugin;
option go_package = "./plugin";
option java_multiple_files = true;
option java_package = "com.hashicorp.goplugin";

message Empty {
}

// The GRPCController is responsible for telling the plugin server to shutdown.
service GRPCController {
rpc Shutdown(Empty) returns (Empty);
}

0 comments on commit e50cee1

Please sign in to comment.