Skip to content

Commit

Permalink
fix: remote message must be proto.Message
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Dec 28, 2023
1 parent ed887f4 commit 6d4a8c5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 38 deletions.
26 changes: 16 additions & 10 deletions _examples/cluster-reentrancy/hello_grain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions _examples/cluster-reentrancy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ func main() {
helloKind := NewHelloKind(NewHelloGrain, 0)
clusterConfig := cluster.Configure("core", provider, lookup, config, cluster.WithKinds(
helloKind))
cluster := cluster.New(system, clusterConfig)
cluster.StartMember()

cst := cluster.New(system, clusterConfig)
cst.StartMember()
// self call: request -> 1 -> 1
client := GetHelloGrainClient(cluster, "1")
client := GetHelloGrainClient(cst, "1")
resp, err := client.InvokeService(&InvokeServiceRequest{Name: "Alice"})
if err != nil {
panic(err)
}
fmt.Printf("%v\n", resp)

// reenter call: request -> 2 -> 1 -> 2
client = GetHelloGrainClient(cluster, "2")
client = GetHelloGrainClient(cst, "2")
resp, err = client.InvokeService(&InvokeServiceRequest{Name: "Alice"})
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions protobuf/protoc-gen-go-grain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func generateContent(gen *protogen.Plugin, g *protogen.GeneratedFile, file *prot
g.QualifiedGoIdent(fmtPackage.Ident(""))
g.QualifiedGoIdent(timePackage.Ident(""))
g.QualifiedGoIdent(slogPackage.Ident(""))
g.QualifiedGoIdent(errorsPackage.Ident(""))

for _, service := range file.Services {
generateService(service, file, g)
Expand Down
16 changes: 10 additions & 6 deletions protobuf/protoc-gen-go-grain/templates/grain.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (g *{{ $service.Name }}GrainClient) {{ $method.Name }}(r *{{ $method.Input
switch msg := resp.(type) {
case *{{ $method.Output }}:
return msg, nil
case error:
return nil, fmt.Errorf("error response: %w", msg)
case *cluster.GrainErrorResponse:
return nil, errors.New(msg.Err)
default:
return nil, fmt.Errorf("unknown response type %T", resp)
}
Expand Down Expand Up @@ -132,19 +132,22 @@ func (a *{{ $service.Name }}Actor) Receive(ctx actor.Context) {
err := proto.Unmarshal(msg.MessageData, req)
if err != nil {
ctx.Logger().Error("[Grain] {{ $method.Name }}({{ $method.Input }}) proto.Unmarshal failed.", slog.Any("error", err))
ctx.Respond(err)
resp := &cluster.GrainErrorResponse{Err: err.Error()}
ctx.Respond(resp)
return
}
{{ if $method.Options.Reenterable -}}
err = a.inner.{{ $method.Name }}(req, respond[*{{ $method.Output }}](a.ctx), a.onError, a.ctx)
if err != nil {
ctx.Respond(err)
resp := &cluster.GrainErrorResponse{Err: err.Error()}
ctx.Respond(resp)
return
}
{{ else }}
r0, err := a.inner.{{ $method.Name }}(req, a.ctx)
if err != nil {
ctx.Respond(err)
resp := &cluster.GrainErrorResponse{Err: err.Error()}
ctx.Respond(resp)
return
}
ctx.Respond(r0)
Expand All @@ -159,7 +162,8 @@ func (a *{{ $service.Name }}Actor) Receive(ctx actor.Context) {
// onError should be used in ctx.ReenterAfter
// you can just return error in reenterable method for other errors
func (a *{{ $service.Name }}Actor) onError(err error) {
a.ctx.Respond(err)
resp := &cluster.GrainErrorResponse{Err: err.Error()}
a.ctx.Respond(resp)
}

func respond[T proto.Message](ctx cluster.GrainContext) func (T) {
Expand Down
15 changes: 9 additions & 6 deletions protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protobuf/protoc-gen-go-grain/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const version = "v0.3.0"
const version = "v0.4.0"

0 comments on commit 6d4a8c5

Please sign in to comment.