middleware/

directory
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: MIT

README

Middleware

  1. rpclog
  2. duration
  3. tags
  4. validator
  5. errors
  6. opentelemetry
  7. opentracing
  8. go-concurrency-limits

Get

go get github.com/xmlking/toolkit

Usage

Interceptors will be executed from left to right: e.g., logging, monitoring and auth.

grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(loggingUnary, monitoringUnary, authUnary),)

Add interceptors in following order

  1. Around interceptors - from outer to inner — e.g., duration, retry
  2. Before interceptors - rate-limit, auth, validation , tagging
  3. After interceptors - rpclog, translog, recovery
import (
    grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
    grpc_validator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
    "github.com/xmlking/toolkit/middleware/rpclog"
)

server := grpc.NewServer(
    grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
        // Execution is done in left-to-right order
        grpc_validator.UnaryServerInterceptor(),
        // keep it last in the interceptor chain
        rpclog.UnaryServerInterceptor(rpclog.WithExcludeMethods("/grpc.health.v1.Health/Check", "/api.MyService/*")),
    )),
    grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
        // keep it last in the interceptor chain
        rpclog.StreamServerInterceptor()
    )),
)
concurrency-limits
import (
    gclGrpc "github.com/platnummonkey/go-concurrency-limits/grpc"
)

// setup grpc server with this option
serverOption := grpc.UnaryInterceptor(
    gclGrpc.UnaryServerInterceptor(
        gclGrpc.WithLimiter(...),
        gclGrpc.WithServerResponseTypeClassifier(..),
    ),
)

// setup grpc client with this option
dialOption := grpc.WithUnaryInterceptor(
    gclGrpc.UnaryClientInterceptor(
        gclGrpc.WithLimiter(...),
        gclGrpc.WithClientResponseTypeClassifier(...),
    ),
)

Directories

Path Synopsis
tags

Jump to

Keyboard shortcuts

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