libp2pgrpc

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 7 Imported by: 2

README

go-libp2p-grpc

build codecov

⚙ GRPC/Protobuf on Libp2p.

Table of Contents

Install

go get github.com/drgomesp/go-libp2p-grpc

Usage

Given an RPC service:

service EchoService {
  // Echo asks a node to respond with a message.
  rpc Echo(EchoRequest) returns (EchoReply) {}
}
type EchoService struct {}

func (EchoService) Echo(context.Context, *EchoRequest) (*EchoReply, error) {
	...
}

And a libp2p host to act as the server:

ma, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10000")

serverHost, err := libp2p.New(libp2p.ListenAddrs(ma))
if err != nil {
    log.Fatal(err)
}
defer serverHost.Close()

srv, err := libp2pgrpc.NewGrpcServer(ctx, serverHost)
if err != nil {
    log.Fatal(err)
}

Register the GRPC service to the host server:

pb.RegisterEchoServiceServer(srv, &EchoService{})

A libp2p host to act as the client:

ma, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10000")

clientHost, err := libp2p.New(libp2p.ListenAddrs(ma))
if err != nil {
    log.Fatal(err)
}

Dial the server and initiate the request:

client := libp2pgrpc.NewClient(cliHost, libp2pgrpc.ProtocolID, libp2pgrpc.WithServer(srv))
conn, err := client.Dial(ctx, srvHost.ID(), grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, err)
defer conn.Close()

c := pb.NewEchoServiceClient(conn)
res, err := c.Echo(ctx, &pb.EchoRequest{Message: "give me something"})

Contributing

PRs accepted.

License

MIT © Daniel Ribeiro

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ProtocolID protocol.ID = "/libp2p/grpc/1.0.0"

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(h host.Host, p protocol.ID, opts ...ClientOption) *Client

func (*Client) Dial

func (c *Client) Dial(
	ctx context.Context,
	peerID peer.ID,
	dialOpts ...grpc.DialOption,
) (*grpc.ClientConn, error)

func (*Client) GetDialOption

func (c *Client) GetDialOption(ctx context.Context) grpc.DialOption

type ClientOption

type ClientOption func(*Client)

ClientOption allows for functional setting of options on a Client.

func WithServer

func WithServer(s *Server) ClientOption

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewGrpcServer

func NewGrpcServer(ctx context.Context, h host.Host, opts ...ServerOption) (*Server, error)

NewGrpcServer creates a Server object with the given LibP2P host and protocol.

func (*Server) RegisterService

func (s *Server) RegisterService(serviceDesc *grpc.ServiceDesc, srv interface{})

type ServerOption

type ServerOption func(*Server)

ServerOption allows for functional setting of options on a Server.

Directories

Path Synopsis
examples
pb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL