Skip to content
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

use protobuf while communicate between peers #30

Merged
merged 8 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 37 additions & 19 deletions network/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ import (
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
"github.com/topology-gg/gram/config"
ex "github.com/topology-gg/gram/execution"
pbcodec "github.com/topology-gg/gram/proto"
"github.com/topology-gg/gram/proto/gen/gram/base"
)

type P2P struct {
ctx context.Context
errCh chan error
executor ex.Execution
host host.Host
namespace string
maxPeers int
port int
pubsub *pubsub.PubSub
streams []Stream
ctx context.Context
errCh chan error
executor ex.Execution
host host.Host
namespace string
maxPeers int
port int
pubsub *pubsub.PubSub
streams []Stream
serializer pbcodec.Serializer
}

type Stream struct {
Expand Down Expand Up @@ -59,16 +62,19 @@ func NewP2P(ctx context.Context, errCh chan error, executor ex.Execution, cfg *c
streams[i] = Stream{name: name}
}

serializer := &pbcodec.ProtoBufSerializer{}

return &P2P{
ctx: ctx,
errCh: errCh,
executor: executor,
host: host,
namespace: namespace,
maxPeers: maxPeers,
port: port,
pubsub: gossipsub,
streams: streams,
ctx: ctx,
errCh: errCh,
executor: executor,
host: host,
namespace: namespace,
maxPeers: maxPeers,
port: port,
pubsub: gossipsub,
streams: streams,
serializer: serializer,
}, nil
}

Expand All @@ -78,8 +84,14 @@ func (p2p *P2P) Start() {
}

func (p2p *P2P) Publish(message string) {
msg, err := p2p.serializer.Marshal(&base.HelloRequest{Name: message})
if err != nil {
p2p.errCh <- err
return
}

for i := range p2p.streams {
if err := p2p.streams[i].topic.Publish(p2p.ctx, []byte(message)); err != nil {
if err := p2p.streams[i].topic.Publish(p2p.ctx, msg); err != nil {
fmt.Println("(Network) Failed to publish to topic:", p2p.streams[i].name, err)
}
}
Expand Down Expand Up @@ -211,6 +223,12 @@ func (p2p *P2P) p2pMessageHandler(subscription *pubsub.Subscription) {
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, was L221~L224 in affect? I wonder if we still need these after the last refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a node publishes a message, it has already subscribed to the relevant topic. That's why this code needed. no?

}

var msg base.HelloRequest
if err := p2p.serializer.Unmarshal(message.Data, &msg); err != nil {
p2p.errCh <- err
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case the error should be logged and that's it I think. errCh is used to send an error that terminates the app, so if there will be an error in parsing data it will result into the whole gram node to shutdown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! Also, I've noticed that some fmt.Printf statements lack the \n newline character. Adding it would be nice for readability. wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt.Printf is being replaced with the logging

}

p2p.executor.Execute(string(message.Message.Data))
}
}
Expand Down
6 changes: 6 additions & 0 deletions proto/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pbcodec

type Serializer interface {
Marshal(data interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
13 changes: 13 additions & 0 deletions proto/pbcodec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pbcodec

import "google.golang.org/protobuf/proto"

type ProtoBufSerializer struct{}

func (p *ProtoBufSerializer) Marshal(data interface{}) ([]byte, error) {
return proto.Marshal(data.(proto.Message))
}

func (p *ProtoBufSerializer) Unmarshal(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message))
}
Loading