options: fix interceptors, add defaulter and recovery

This commit is contained in:
Adphi 2021-11-21 16:13:43 +01:00
parent dc78a2c688
commit d246bb3214
11 changed files with 165 additions and 40 deletions

View File

@ -9,28 +9,30 @@ Principles:
Features: Features:
- [x] simple configuration with options - [x] simple configuration with options
- [x] embeded gorm database with options (branch db) - [x] embedded gorm database with options (branch db)
- [x] simple TLS configuration - [x] simple TLS configuration
- [ ] TLS auth - [ ] TLS auth
- [ ] client connection pool - [ ] client connection pool
- [ ] registry / resolver resolution - [ ] registry / resolver resolution
- [ ] mdns - [ ] mdns
- [ ] kubernetes - [ ] kubernetes
- [ ] default interceptors implementation: - [ ] default interceptors implementation:
- [ ] default - [ ] context request id
- [ ] validation - [x] defaulter
- [x] validation
- [ ] health - [ ] health
- [ ] context logger - [ ] context logger
- [ ] sentry - [ ] sentry
- [ ] rate-limiting - [ ] rate-limiting
- [ ] ban
- [ ] auth claim in context - [ ] auth claim in context
- [ ] recovery - [x] recovery (server side only)
- [x] tracing (open-tracing) - [x] tracing (open-tracing)
- [x] metrics (prometheus) - [x] metrics (prometheus)
- [ ] retries - [ ] retries
- [ ] context DB / transaction - [ ] context DB / transaction
- ... - ...
- [ ] api gateway with middleware: - [ ] grpc web / api gateway with middleware:
- [ ] auth - [ ] auth
- [ ] cors - [ ] cors
- [ ] logging - [ ] logging
@ -43,3 +45,5 @@ Features:
- https://github.com/grpc-ecosystem/grpc-opentracing - https://github.com/grpc-ecosystem/grpc-opentracing
- https://github.com/grpc-ecosystem/go-grpc-prometheus - https://github.com/grpc-ecosystem/go-grpc-prometheus
- https://github.com/grpc-ecosystem/grpc-gateway - https://github.com/grpc-ecosystem/grpc-gateway
- https://github.com/jaredfolkins/badactor
- github.com/improbable-eng/grpc-web

View File

@ -14,6 +14,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.linka.cloud/grpc/client" "go.linka.cloud/grpc/client"
"go.linka.cloud/grpc/interceptors/defaulter"
metrics2 "go.linka.cloud/grpc/interceptors/metrics" metrics2 "go.linka.cloud/grpc/interceptors/metrics"
validation2 "go.linka.cloud/grpc/interceptors/validation" validation2 "go.linka.cloud/grpc/interceptors/validation"
"go.linka.cloud/grpc/logger" "go.linka.cloud/grpc/logger"
@ -69,6 +70,7 @@ func main() {
var err error var err error
metrics := metrics2.NewInterceptors() metrics := metrics2.NewInterceptors()
validation := validation2.NewInterceptors(true) validation := validation2.NewInterceptors(true)
defaulter := defaulter.NewInterceptors()
address := "0.0.0.0:9991" address := "0.0.0.0:9991"
svc, err = service.New( svc, err = service.New(
service.WithContext(ctx), service.WithContext(ctx),
@ -93,7 +95,7 @@ func main() {
service.WithGRPCWeb(true), service.WithGRPCWeb(true),
service.WithGRPCWebPrefix("/grpc"), service.WithGRPCWebPrefix("/grpc"),
service.WithMiddlewares(httpLogger), service.WithMiddlewares(httpLogger),
service.WithInterceptors(validation, metrics), service.WithInterceptors(metrics, defaulter, validation),
) )
if err != nil { if err != nil {
panic(err) panic(err)
@ -125,7 +127,7 @@ func main() {
if err == nil { if err == nil {
logrus.Fatal("expected validation error") logrus.Fatal("expected validation error")
} }
stream, err := g.SayHelloStream(context.Background(), &HelloStreamRequest{Name: "test", Count: 10}) stream, err := g.SayHelloStream(context.Background(), &HelloStreamRequest{Name: "test"})
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }

View File

@ -21,4 +21,7 @@ func (x *HelloReply) Default() {
} }
func (x *HelloStreamRequest) Default() { func (x *HelloStreamRequest) Default() {
if x.Count == 0 {
x.Count = 10
}
} }

View File

@ -11,6 +11,7 @@ import (
sync "sync" sync "sync"
_ "github.com/envoyproxy/protoc-gen-validate/validate" _ "github.com/envoyproxy/protoc-gen-validate/validate"
_ "go.linka.cloud/protoc-gen-defaults/defaults"
_ "google.golang.org/genproto/googleapis/api/annotations" _ "google.golang.org/genproto/googleapis/api/annotations"
protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl" protoimpl "google.golang.org/protobuf/runtime/protoimpl"
@ -180,32 +181,34 @@ var file_example_example_proto_rawDesc = []byte{
0x72, 0x6c, 0x64, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x72, 0x6c, 0x64, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f,
0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x1a, 0x17, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2f, 0x76, 0x61, 0x6c, 0x69, 0x6f, 0x1a, 0x17, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2f, 0x76, 0x61, 0x6c, 0x69,
0x64, 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2d, 0x0a, 0x0c, 0x48, 0x65, 0x64, 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x17, 0x64, 0x65, 0x66, 0x61,
0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1d, 0x0a, 0x04, 0x6e, 0x61, 0x75, 0x6c, 0x74, 0x73, 0x2f, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x73, 0x2e, 0x70, 0x72,
0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x09, 0xfa, 0x42, 0x06, 0x72, 0x04, 0x10, 0x6f, 0x74, 0x6f, 0x22, 0x2d, 0x0a, 0x0c, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75,
0x02, 0x18, 0x28, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x26, 0x0a, 0x0a, 0x48, 0x65, 0x6c, 0x65, 0x73, 0x74, 0x12, 0x1d, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
0x6c, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x09, 0x42, 0x09, 0xfa, 0x42, 0x06, 0x72, 0x04, 0x10, 0x02, 0x18, 0x28, 0x52, 0x04, 0x6e, 0x61,
0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x6d, 0x65, 0x22, 0x26, 0x0a, 0x0a, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79,
0x65, 0x22, 0x49, 0x0a, 0x12, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1d, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x59, 0x0a, 0x12, 0x48, 0x65,
0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x09, 0xfa, 0x42, 0x06, 0x72, 0x04, 0x10, 0x02, 0x18, 0x28, 0x6c, 0x6c, 0x6f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x12, 0x1d, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x09,
0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0xb7, 0x01, 0x0a, 0xfa, 0x42, 0x06, 0x72, 0x04, 0x10, 0x02, 0x18, 0x28, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,
0x07, 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x12, 0x5e, 0x0a, 0x08, 0x53, 0x61, 0x79, 0x48, 0x24, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x42, 0x0e,
0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x18, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0xfa, 0x42, 0x06, 0x22, 0x04, 0x18, 0x0a, 0x28, 0x01, 0x9a, 0x49, 0x02, 0x20, 0x0a, 0x52, 0x05,
0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0xb7, 0x01, 0x0a, 0x07, 0x47, 0x72, 0x65, 0x65, 0x74, 0x65,
0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x72, 0x12, 0x5e, 0x0a, 0x08, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x18, 0x2e,
0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x20, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1a, 0x22, 0x15, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f,
0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77,
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x3a, 0x01, 0x2a, 0x12, 0x4c, 0x0a, 0x0e, 0x53, 0x61, 0x79, 0x48, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22,
0x65, 0x6c, 0x6c, 0x6f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x1e, 0x2e, 0x68, 0x65, 0x6c, 0x20, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1a, 0x22, 0x15, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31,
0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x53, 0x74, 0x72, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x3a, 0x01,
0x65, 0x61, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x68, 0x65, 0x6c, 0x2a, 0x12, 0x4c, 0x0a, 0x0e, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x53, 0x74, 0x72,
0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x70, 0x65, 0x61, 0x6d, 0x12, 0x1e, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64,
0x6c, 0x79, 0x22, 0x00, 0x30, 0x01, 0x42, 0x22, 0x5a, 0x20, 0x67, 0x6f, 0x2e, 0x6c, 0x69, 0x6e, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, 0x71, 0x75,
0x6b, 0x61, 0x2e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x65, 0x78, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x30, 0x01, 0x42,
0x6f, 0x33, 0x22, 0x5a, 0x20, 0x67, 0x6f, 0x2e, 0x6c, 0x69, 0x6e, 0x6b, 0x61, 0x2e, 0x63, 0x6c, 0x6f, 0x75,
0x64, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3b, 0x6d,
0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -249,7 +249,16 @@ func (m *HelloStreamRequest) Validate(all bool) error {
errors = append(errors, err) errors = append(errors, err)
} }
// no validation rules for Count if val := m.GetCount(); val < 1 || val > 10 {
err := HelloStreamRequestValidationError{
field: "Count",
reason: "value must be inside range [1, 10]",
}
if !all {
return err
}
errors = append(errors, err)
}
if len(errors) > 0 { if len(errors) > 0 {
return HelloStreamRequestMultiError(errors) return HelloStreamRequestMultiError(errors)

View File

@ -6,6 +6,7 @@ option go_package = "go.linka.cloud/grpc/example;main";
import "google/api/annotations.proto"; import "google/api/annotations.proto";
import "validate/validate.proto"; import "validate/validate.proto";
import "defaults/defaults.proto";
service Greeter { service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) { rpc SayHello (HelloRequest) returns (HelloReply) {
@ -27,5 +28,5 @@ message HelloReply {
message HelloStreamRequest { message HelloStreamRequest {
string name = 1 [(validate.rules).string = {min_len: 2, max_len: 40}]; string name = 1 [(validate.rules).string = {min_len: 2, max_len: 40}];
int64 count = 2; int64 count = 2 [(validate.rules).int64 = {gte: 1, lte: 10}, (defaults.value).int64 = 10];
} }

View File

@ -0,0 +1,77 @@
package defaulter
import (
"context"
"google.golang.org/grpc"
"go.linka.cloud/grpc/interceptors"
)
type interceptor struct{}
func NewInterceptors() interceptors.Interceptors {
return &interceptor{}
}
func defaults(v interface{}) {
if d, ok := v.(interface{ Default() }); v != nil && ok {
d.Default()
}
}
func (i interceptor) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defaults(req)
return handler(ctx, req)
}
}
func (i interceptor) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
defaults(req)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func (i interceptor) StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
wrapper := &recvWrapper{ServerStream: stream}
return handler(srv, wrapper)
}
}
func (i interceptor) StreamClientInterceptor() grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
desc.Handler = (&sendWrapper{handler: desc.Handler}).Handler()
return streamer(ctx, desc, cc, method)
}
}
type recvWrapper struct {
grpc.ServerStream
}
func (s *recvWrapper) RecvMsg(m interface{}) error {
if err := s.ServerStream.RecvMsg(m); err != nil {
return err
}
defaults(m)
return nil
}
type sendWrapper struct {
grpc.ServerStream
handler grpc.StreamHandler
}
func (s *sendWrapper) Handler() grpc.StreamHandler {
return func(srv interface{}, stream grpc.ServerStream) error {
return s.handler(srv, s)
}
}
func (s *sendWrapper) SendMsg(m interface{}) error {
defaults(m)
return s.ServerStream.SendMsg(m)
}

View File

@ -0,0 +1,29 @@
package recovery
import (
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"google.golang.org/grpc"
)
type interceptors struct {
opts grpc_recovery.Option
}
func (i *interceptors) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return grpc_recovery.UnaryServerInterceptor(i.opts)
}
func (i *interceptors) StreamServerInterceptor() grpc.StreamServerInterceptor {
return grpc_recovery.StreamServerInterceptor(i.opts)
}
func (i *interceptors) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
panic("not implemented")
}
func (i *interceptors) StreamClientInterceptor() grpc.StreamClientInterceptor {
panic("not implemented")
}

View File

@ -133,11 +133,9 @@ func (s *recvWrapper) RecvMsg(m interface{}) error {
if err := s.ServerStream.RecvMsg(m); err != nil { if err := s.ServerStream.RecvMsg(m); err != nil {
return err return err
} }
if err := s.i.validate(m); err != nil { if err := s.i.validate(m); err != nil {
return err return err
} }
return nil return nil
} }

View File

@ -370,7 +370,6 @@ type options struct {
serverOpts []grpc.ServerOption serverOpts []grpc.ServerOption
unaryServerInterceptors []grpc.UnaryServerInterceptor unaryServerInterceptors []grpc.UnaryServerInterceptor
streamServerInterceptors []grpc.StreamServerInterceptor streamServerInterceptors []grpc.StreamServerInterceptor

View File

@ -110,7 +110,7 @@ func newService(opts ...Option) (*service, error) {
ui := grpcmiddleware.ChainUnaryServer(s.opts.unaryServerInterceptors...) ui := grpcmiddleware.ChainUnaryServer(s.opts.unaryServerInterceptors...)
s.inproc = s.inproc.WithServerUnaryInterceptor(ui) s.inproc = s.inproc.WithServerUnaryInterceptor(ui)
si := grpcmiddleware.ChainStreamServer( /*TODO(adphi): add to options*/ ) si := grpcmiddleware.ChainStreamServer(s.opts.streamServerInterceptors... )
s.inproc = s.inproc.WithServerStreamInterceptor(si) s.inproc = s.inproc.WithServerStreamInterceptor(si)
gopts := []grpc.ServerOption{ gopts := []grpc.ServerOption{