Skip to content

Commit 33666cb

Browse files
author
Norman Meier
committed
feat: enable bidi stream for blmod.RunModule
Signed-off-by: Norman Meier <[email protected]>
1 parent a071544 commit 33666cb

File tree

13 files changed

+202
-84
lines changed

13 files changed

+202
-84
lines changed

api/blmod/v1/blmod.proto

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ message ModuleInfo {
2020

2121
service LabsModulesService {
2222
rpc AllModules(AllModulesRequest) returns (AllModulesResponse) {}
23-
rpc RunModule(RunModuleRequest) returns (stream RunModuleResponse) {}
23+
rpc RunModule(stream RunModuleRequest) returns (stream RunModuleResponse) {}
2424
}
2525

2626

@@ -33,8 +33,11 @@ message AllModulesResponse {
3333
}
3434

3535
message RunModuleRequest {
36-
string name = 1;
37-
bytes args = 2;
36+
bytes payload = 1;
37+
38+
// Header data
39+
string name = 2;
40+
bytes args = 3;
3841
}
3942

4043
message RunModuleResponse {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/ipfs/go-ds-flatfs v0.5.1
1313
github.com/ipfs/go-ds-leveldb v0.5.0
1414
github.com/ipfs/go-ds-sql v0.3.0
15-
github.com/ipfs/go-ipfs-api v0.3.0
1615
github.com/ipfs/go-ipfs-chunker v0.0.5
1716
github.com/ipfs/go-ipfs-config v0.18.0
1817
github.com/multiformats/go-multiaddr v0.5.0
@@ -92,6 +91,7 @@ require (
9291
github.com/ipfs/go-fs-lock v0.0.7 // indirect
9392
github.com/ipfs/go-graphsync v0.11.0 // indirect
9493
github.com/ipfs/go-ipfs v0.11.0 // indirect
94+
github.com/ipfs/go-ipfs-api v0.3.0 // indirect
9595
github.com/ipfs/go-ipfs-blockstore v0.2.1 // indirect
9696
github.com/ipfs/go-ipfs-cmds v0.6.0 // indirect
9797
github.com/ipfs/go-ipfs-delay v0.0.1 // indirect
@@ -218,6 +218,7 @@ require (
218218
github.com/prometheus/common v0.30.0 // indirect
219219
github.com/prometheus/procfs v0.7.3 // indirect
220220
github.com/prometheus/statsd_exporter v0.21.0 // indirect
221+
github.com/rogpeppe/go-internal v1.8.0 // indirect
221222
github.com/rs/cors v1.7.0 // indirect
222223
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
223224
github.com/spaolacci/murmur3 v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,9 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqn
13401340
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
13411341
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
13421342
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
1343-
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
13441343
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
1344+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
1345+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
13451346
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
13461347
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
13471348
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=

go/cmd/client/main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ func main() {
6060
if len(*argsFlag) != 0 {
6161
args = []byte(*argsFlag)
6262
}
63-
cl, err := client.RunModule(context.Background(), &blmod.RunModuleRequest{
64-
Name: *runFlag,
65-
Args: args,
66-
})
63+
cl, err := client.RunModule(context.Background())
6764
if err != nil {
6865
log.Fatalf("fail to start running module: %v", err)
6966
}
67+
if err := cl.Send(&blmod.RunModuleRequest{
68+
Name: *runFlag,
69+
Args: args,
70+
}); err != nil {
71+
log.Fatalf("fail to send header: %v", err)
72+
}
73+
if err := cl.CloseSend(); err != nil {
74+
log.Fatalf("fail to close send: %v", err)
75+
}
76+
7077
for {
7178
reply, err := cl.Recv()
7279
if err == io.EOF {

go/pkg/blmod/blmod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
type ModuleContext interface {
1313
Send(interface{}) error
14+
Recv(interface{}) error
1415
}
1516

1617
type Module interface {

go/pkg/blmod/blmod.pb.go

Lines changed: 34 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/pkg/blmod/blmod_grpc.pb.go

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/pkg/blmod/server.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type moduleContext struct {
4848
srv LabsModulesService_RunModuleServer
4949
}
5050

51+
var _ ModuleContext = (*moduleContext)(nil)
52+
5153
func (mc *moduleContext) Send(v interface{}) error {
5254
bytes, err := json.Marshal(v)
5355
if err != nil {
@@ -56,7 +58,22 @@ func (mc *moduleContext) Send(v interface{}) error {
5658
return mc.srv.Send(&RunModuleResponse{Payload: bytes})
5759
}
5860

59-
func (s *Server) RunModule(req *RunModuleRequest, srv LabsModulesService_RunModuleServer) error {
61+
func (mc *moduleContext) Recv(v interface{}) error {
62+
reply, err := mc.srv.Recv()
63+
if err != nil {
64+
return err
65+
}
66+
if err := json.Unmarshal(reply.GetPayload(), v); err != nil {
67+
return errors.Wrap(err, "unmarshal JSON")
68+
}
69+
return nil
70+
}
71+
72+
func (s *Server) RunModule(srv LabsModulesService_RunModuleServer) error {
73+
req, err := srv.Recv()
74+
if err != nil {
75+
return errors.Wrap(err, "read header")
76+
}
6077
mod, err := s.reg.Get(req.GetName())
6178
if err != nil {
6279
return errors.Wrap(err, "get module")

rn/src/api/blmod/v1/blmod_pb.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ export namespace AllModulesResponse {
9494
}
9595

9696
export class RunModuleRequest extends jspb.Message {
97+
getPayload(): Uint8Array | string
98+
getPayload_asU8(): Uint8Array
99+
getPayload_asB64(): string
100+
setPayload(value: Uint8Array | string): void
101+
97102
getName(): string
98103
setName(value: string): void
99104

@@ -117,6 +122,7 @@ export class RunModuleRequest extends jspb.Message {
117122

118123
export namespace RunModuleRequest {
119124
export type AsObject = {
125+
payload: Uint8Array | string
120126
name: string
121127
args: Uint8Array | string
122128
}

0 commit comments

Comments
 (0)