grpcdynamic

package module
v0.0.0-...-70a2c32 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2020 License: MIT Imports: 5 Imported by: 1

README

grpcdynamic

Dynamic gRPC registration mechanism.

Usage

grpcdynamic provides dynamic gRPC service registration with arbitrary implementations.

package main

import (
	"context"
	"fmt"
	"log"
	"net"

	"github.com/ktr0731/grpcdynamic"

	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

type req struct {
	Name string
}

type res struct {
	Message string
}

func main() {
	service := grpcdynamic.NewService("api.Example")
	service.RegisterUnaryMethod("Unary", new(req), new(res), func(ctx context.Context, in interface{}) (interface{}, error) {
		req := in.(*req)
		return &res{Message: fmt.Sprintf("hi, %s", req.Name)}, nil
	})
	srv := grpcdynamic.NewServer([]*grpcdynamic.Service{service})
	reflection.Register(srv)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	done := make(chan struct{})
	go func() {
		if err := startServer(ctx, srv); err != nil {
			log.Fatal(err)
		}
		close(done)
	}()

	res, err := callUnaryMethod(service.FullMethodName("Unary"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Message)

	cancel()
	<-done
}

func startServer(ctx context.Context, srv *grpc.Server) error {
	l, err := net.Listen("tcp", ":50051")
	if err != nil {
		return err
	}

	go func() {
		<-ctx.Done()
		srv.Stop()
	}()

	if err := srv.Serve(l); err != nil {
		return err
	}
	return nil
}

func callUnaryMethod(name string) (*res, error) {
	conn, err := grpc.Dial(
		":50051",
		grpc.WithInsecure(),
		grpc.WithDefaultCallOptions(grpc.CallContentSubtype(grpcdynamic.CodecName)),
	)
	if err != nil {
		return nil, err
	}
	defer conn.Close()

	var res res
	if err := conn.Invoke(context.Background(), name, &req{Name: "foo"}, &res); err != nil {
		return nil, err
	}
	return &res, nil
}

Documentation

Index

Constants

View Source
const CodecName = "grpcdynamic"

Variables

This section is empty.

Functions

func NewServer

func NewServer(services []*Service, opt ...grpc.ServerOption) *grpc.Server

Types

type Service

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

func NewService

func NewService(name string) *Service

func (*Service) FullMethodName

func (s *Service) FullMethodName(methodName string) string

func (*Service) RegisterUnaryMethod

func (s *Service) RegisterUnaryMethod(name string, req, res interface{}, h grpc.UnaryHandler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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