relab / gorums

Gorums simplify fault-tolerant quorum-based protocols
MIT License
138 stars 14 forks source link

feat: Generate ServiceNameClient interface akin to gRPC's plugin #178

Open meling opened 8 months ago

meling commented 8 months ago

The gRPC plugin generates a ServiceNameClient interface like this one:

// MultiPaxosClient is the client API for MultiPaxos service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type MultiPaxosClient interface {
    Prepare(ctx context.Context, in *PrepareMsg, opts ...grpc.CallOption) (*PromiseMsg, error)
    Accept(ctx context.Context, in *AcceptMsg, opts ...grpc.CallOption) (*LearnMsg, error)
    Commit(ctx context.Context, in *LearnMsg, opts ...grpc.CallOption) (*Empty, error)
    ClientHandle(ctx context.Context, in *Value, opts ...grpc.CallOption) (*Response, error)
}

Such an interface can be useful for testing.

Since the Gorums plugin replaces the gRPC plugin because it generates overlapping types, we should provide the relevant interface as part of code generation in the _gorums.pb.go file. This should be straightforward since we already generate methods on the Configuration type, such as this one:

var quorumCallSignature = `func (c *Configuration) {{$method}}(` +
    `ctx {{$context}}, in *{{$in}}` +
    `{{perNodeFnType .GenFile .Method ", f"}})` +
    `(resp *{{$customOut}}, err error) {
`

We already generate the corresponding server-side interface here:

var serverInterface = `
{{$genFile := .GenFile}}
{{range .Services -}}
{{$service := .GoName}}
// {{$service}} is the server-side API for the {{$service}} Service
type {{$service}} interface {
    {{- range .Methods}}
    {{- if isOneway .}}
    {{.GoName}}(ctx {{$context}}, request *{{in $genFile .}})
    {{- else if correctableStream .}}
    {{.GoName}}(ctx {{$context}}, request *{{in $genFile .}}, send func(response *{{out $genFile .}}) error) error
    {{- else}}
    {{.GoName}}(ctx {{$context}}, request *{{in $genFile .}}) (response *{{out $genFile .}}, err error)
    {{- end}}
    {{- end}}
}
{{- end}}
`