Skip to content

adding go-plugin example using C# #102

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions examples/grpc/plugin-dotnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
obj
go-plugins
29 changes: 29 additions & 0 deletions examples/grpc/plugin-dotnet/HealthService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Grpc.Core;

namespace plugin_dotnet
{
public static class HealthService
{
public static IHealthService Get() =>
new Grpc.HealthCheck.HealthServiceImpl();

public static ServerServiceDefinition BindService(IHealthService health) =>
Grpc.Health.V1.Health.BindService((Grpc.Health.V1.Health.HealthBase)health);
}

public interface IHealthService
{
void ClearAll();
void ClearStatus(string service);
void SetStatus(string service, HealthStatus status);


}

public enum HealthStatus
{
Unknown = 0,
Serving = 1,
NotServing = 2,
}
}
64 changes: 64 additions & 0 deletions examples/grpc/plugin-dotnet/Plugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

using System;
using System.IO;
using System.Threading.Tasks;
using Google.Protobuf;
using Grpc.Core;
using Proto;

namespace plugin_dotnet
{
class Plugin : KV.KVBase
{
public const string ServiceHost = "localhost";
public const int ServicePort = 1234;
public const int AppProtoVersion = 1;

public override async Task<Empty> Put(PutRequest request, ServerCallContext context)
{
var filename = $"kv_{request.Key}";
await File.WriteAllTextAsync(filename,
$"{request.Value.ToStringUtf8()}\n\nWritten from plugin-dotnet\n");

return new Empty();
}

public override async Task<GetResponse> Get(GetRequest request, ServerCallContext context)
{
var filename = $"kv_{request.Key}";
return new GetResponse
{
Value = ByteString.CopyFromUtf8(await File.ReadAllTextAsync(filename)),
};
}

static async Task Main(string[] args)
{
// go-plugin semantics depend on the Health Check service from gRPC
var health = HealthService.Get();
health.SetStatus("plugin", HealthStatus.Serving);

// Build a server to host the plugin over gRPC
var server = new Server
{
Ports = { { ServiceHost, ServicePort, ServerCredentials.Insecure } },
Services = {
{ HealthService.BindService(health) },
{ KV.BindService(new Plugin()) },
},
};

server.Start();

// Part of the go-plugin handshake:
// https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md#4-output-handshake-information
await Console.Out.WriteAsync($"1|1|tcp|{ServiceHost}:{ServicePort}|grpc\n");
await Console.Out.FlushAsync();

while (Console.Read() == -1)
await Task.Delay(1000);

await server.ShutdownAsync();
}
}
}
Loading