github.com/evdatsion/aphelion-dpos-bft@v0.32.1/rpc/grpc/client_server.go (about)

     1  package core_grpc
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  
     7  	"google.golang.org/grpc"
     8  
     9  	cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common"
    10  )
    11  
    12  // Config is an gRPC server configuration.
    13  type Config struct {
    14  	MaxOpenConnections int
    15  }
    16  
    17  // StartGRPCServer starts a new gRPC BroadcastAPIServer using the given
    18  // net.Listener.
    19  // NOTE: This function blocks - you may want to call it in a go-routine.
    20  func StartGRPCServer(ln net.Listener) error {
    21  	grpcServer := grpc.NewServer()
    22  	RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{})
    23  	return grpcServer.Serve(ln)
    24  }
    25  
    26  // StartGRPCClient dials the gRPC server using protoAddr and returns a new
    27  // BroadcastAPIClient.
    28  func StartGRPCClient(protoAddr string) BroadcastAPIClient {
    29  	conn, err := grpc.Dial(protoAddr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return NewBroadcastAPIClient(conn)
    34  }
    35  
    36  func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
    37  	return cmn.Connect(addr)
    38  }