All Projects → MarquisIO → go-grpcmw

MarquisIO / go-grpcmw

Licence: MIT license
A Go package and protobuf generator for managing grpc interceptors.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to go-grpcmw

grpc-jwt-spring-boot-starter
Spring boot starter for gRPC framework with JWT authorization
Stars: ✭ 24 (+26.32%)
Mutual labels:  protobuf, interceptor
metastore
A protobuf schema registry on steroids. It will keep track of the contracts throughout your organization, making sure no contract is broken.
Stars: ✭ 43 (+126.32%)
Mutual labels:  protobuf
multicode
💱 Decode bits, bytes, hex, base64 and protobuf recursively with a single command
Stars: ✭ 16 (-15.79%)
Mutual labels:  protobuf
protoc-gen-persist
GRPC SQL and Spanner persistence layer
Stars: ✭ 60 (+215.79%)
Mutual labels:  protobuf
sisyphus-js
Sisyphus customized protobuf and gRPC runtime and code generator for JavaScript/TypeScript
Stars: ✭ 19 (+0%)
Mutual labels:  protobuf
j2cl-protobuf
Protocol Buffers implementation for J2CL
Stars: ✭ 23 (+21.05%)
Mutual labels:  protobuf
raster
A micro server framework, support coroutine, and parallel-computing, used for building flatbuffers/thrift/protobuf/http protocol service.
Stars: ✭ 19 (+0%)
Mutual labels:  protobuf
aerospike-scala
Typesafe DSL for work with Aerospike Database
Stars: ✭ 40 (+110.53%)
Mutual labels:  protobuf
libcluon
libcluon is a small and efficient, single-file and header-only library written in modern C++ to power microservices.
Stars: ✭ 81 (+326.32%)
Mutual labels:  protobuf
rules proto grpc
Bazel rules for building Protobuf and gRPC code and libraries from proto_library targets
Stars: ✭ 201 (+957.89%)
Mutual labels:  protobuf
esa-httpclient
An asynchronous event-driven HTTP client based on netty.
Stars: ✭ 82 (+331.58%)
Mutual labels:  interceptor
molch
An implementation of the axolotl ratchet based on libsodium.
Stars: ✭ 24 (+26.32%)
Mutual labels:  protobuf
stellarstation-api
The API definition for StellarStation.
Stars: ✭ 22 (+15.79%)
Mutual labels:  protobuf
GameTracking-HalfLifeAlyx
📥 Game Tracker: Half-Life: Alyx
Stars: ✭ 24 (+26.32%)
Mutual labels:  protobuf
WebApiClient.Extensions
WebApiClient项目的第三方扩展:Autofac、DependencyInjection、HttpClientFactory、SteeltoeOSS.Discovery、MessagePack、Protobuf、Json-Rpc
Stars: ✭ 73 (+284.21%)
Mutual labels:  protobuf
go-chat
go-chat.使用Go基于WebSocket开发的web聊天应用。单聊,群聊。文字,图片,语音,视频消息,屏幕共享,剪切板图片,基于WebRTC的P2P语音通话,视频聊天。
Stars: ✭ 516 (+2615.79%)
Mutual labels:  protobuf
Meshtastic-protobufs
Protobuf definitions for the Meshtastic project
Stars: ✭ 32 (+68.42%)
Mutual labels:  protobuf
MicroServicePractice
微服务实践的demo
Stars: ✭ 40 (+110.53%)
Mutual labels:  protobuf
edap
No description or website provided.
Stars: ✭ 22 (+15.79%)
Mutual labels:  protobuf
go2gql
graphql-go schema generator by proto files
Stars: ✭ 33 (+73.68%)
Mutual labels:  protobuf

go-grpcmw

go-grpcmw provides a package and a protobuf generator for managing easily grpc interceptors.

The package can be used without the protobuf generator. However, using both together will allow you to avoid writing redundant code.

Prerequisites

  • Protobuf 3.0.0 or later.

Installation

go get -u github.com/MarquisIO/go-grpcmw/protoc-gen-grpc-middleware
go get -u github.com/golang/protobuf/protoc-gen-go

Quick start

Write your gRPC service definition:

syntax = "proto3";

import "github.com/MarquisIO/go-grpcmw/annotations/annotations.proto";

package pb;

option (grpcmw.package_interceptors) = {
  indexes: ["index"]
};

service SomeService {
  option (grpcmw.service_interceptors) = {
    indexes: ["index"]
  };

  rpc SomeMethod (Message) returns (Message) {
    option (grpcmw.method_interceptors) = {
      indexes: ["index"]
    };
  }
}

message Message {
	string msg = 1;
}

Generate the stubs:

protoc --go_out=plugins=grpc:. --grpc-middleware_out=:. path/to/you/file.proto

Use the code generated to add your own middlewares:

// Register an interceptor in the registry
registry.GetClientInterceptor("index").
  AddGRPCUnaryInterceptor(SomeUnaryClientInterceptor).
  AddGRPCStreamInterceptor(SomeStreamClientInterceptor)
registry.GetServerInterceptor("index").
  AddGRPCUnaryInterceptor(SomeUnaryServerInterceptor).
  AddGRPCStreamInterceptor(SomeStreamServerInterceptor)

// Client
clientRouter := grpcmw.NewClientRouter()
clientStub := pb.RegisterClientInterceptors(clientRouter)
clientStub.RegisterSomeService().
	SomeMethod().
	AddGRPCInterceptor(clientUnaryMiddleware)
grpc.Dial(address,
	grpc.WithStreamInterceptor(clientRouter.StreamResolver()),
	grpc.WithUnaryInterceptor(clientRouter.UnaryResolver()),
)

// Server
serverRouter := grpcmw.NewServerRouter()
serverStub := pb.RegisterServerInterceptors(serverRouter)
serverStub.RegisterSomeService().
	SomeMethod().
	AddGRPCInterceptor(serverUnaryMiddleware)
grpc.NewServer(
	grpc.UnaryInterceptor(serverRouter.UnaryResolver()),
	grpc.StreamInterceptor(serverRouter.StreamResolver()),
)

Chaining

Four types of interceptors are provided: ServerUnaryInterceptor, ServerStreamInterceptor, ClientUnaryInterceptor and ClientStreamInterceptor (corresponding to those defined in google.golang.org/grpc). They allow you to chain multiple gRPC interceptors of the same type:

// grpcInterceptor1 -> grpcInterceptor2 -> interceptor1 -> grpcInterceptor3
intcp := grpcmw.NewUnaryServerInterceptor(grpcInterceptor1, grpcInterceptor2).
	AddInterceptor(interceptor1).
	AddGRPCInterceptor(grpcInterceptor3)

Routing

This package also provides a routing feature so that interceptors can be bound either to:

  • a protobuf package: all requests to any service that have been declared in this package will go through the interceptor.
  • a gRPC service: all requests to this service will go through the interceptor.
  • a gRPC method: all requests to this method will go through the interceptor.

ServerRouter and ClientRouter provide one more global level in addition to the three described above. These implementations are based on the route construction as defined in the official gRPC repository.

serverRouter := grpcmw.NewServerRouter()
pkgInterceptor := grpcmw.NewServerInterceptor("pb")
serviceInterceptor := grpcmw.NewServerInterceptor("Service")

// Interceptors that have been added to `serviceInterceptor` will be called
// each time the gRPC service `Service` will be requested. In other words, all
// requests to "/pb.Service/*" will go through these interceptors.
pkgInterceptor.Register(serviceInterceptor)
serverRouter.GetRegister().
  Register(pkgInterceptor)

// In order to use the router, you have to create the server with it.
grpc.NewServer(
	grpc.UnaryInterceptor(serverRouter.UnaryResolver()),
	grpc.StreamInterceptor(serverRouter.StreamResolver()),
)

Registry

The registry package provides an interceptor registry for both server and client side.

registry.GetServerInterceptor("index").
  AddGRPCUnaryInterceptor(SomeUnaryServerInterceptor).
  AddGRPCStreamInterceptor(SomeStreamServerInterceptor)

Protobuf generation

In order to ease the use of the registry and routing features, a protobuf generator is provided which you can use with the following command:

protoc --grpc-middleware_out=:. path/to/you/file.proto

Routing

Say we have the following protobuf file:

syntax = "proto3";

package pb;

service SomeService {
  rpc SomeMethod (Message) returns (Message) {}
}

message Message {
	string msg = 1;
}

It will create some helpers for adding interceptors to a package, service or method.

serverRouter := grpcmw.NewServerRouter()
serverStub := pb.RegisterServerInterceptors(serverRouter)
serverStub.AddGRPCInterceptor(pkgUnaryMiddleware)
serviceStub := serverStub.RegisterSomeService()
serviceStub.AddGRPCInterceptor(serviceUnaryMiddleware)
methodStub := serviceStub.SomeMethod()
methodStub.AddGRPCInterceptor(methodUnaryMiddleware)

Registry

Three annotations are provided in annotations/annotations.proto:

  • package_interceptors: for the package level.
  • service_interceptors: for the service level.
  • method_interceptors: for the method level.

These annotations have an array of index (indexes) that tells the generator which interceptors from the registry have to be added to the router.

Say we have the following protobuf file:

syntax = "proto3";

import "github.com/MarquisIO/go-grpcmw/annotations/annotations.proto";

package pb;

option (grpcmw.package_interceptors) = {
  indexes: ["index"]
};

service SomeService {
  option (grpcmw.service_interceptors) = {
    indexes: ["index"]
  };

  rpc SomeMethod (Message) returns (Message) {
    option (grpcmw.method_interceptors) = {
      indexes: ["index"]
    };
  }
}

message Message {
	string msg = 1;
}

You can then register interceptors in the registry at the index "index".

// Register an interceptor in the registry
registry.GetServerInterceptor("index").
  AddGRPCUnaryInterceptor(SomeUnaryServerInterceptor).
  AddGRPCStreamInterceptor(SomeStreamServerInterceptor)

// You have to call `RegisterServerInterceptors` and `RegisterSomeService` so
// that interceptors are added to the router at the package, servive and method
// levels.
serverRouter := grpcmw.NewServerRouter()
serverStub := pb.RegisterServerInterceptors(serverRouter)
serverStub.RegisterSomeService()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].