Ai
1 Star 0 Fork 0

brucewang/go公共库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
options.go 4.62 KB
一键复制 编辑 原始数据 按行查看 历史
brucewang 提交于 2022-01-05 08:17 +08:00 . 改包名
/*
* @Author: lwnmengjing
* @Date: 2021/6/2 4:30 下午
* @Last Modified by: lwnmengjing
* @Last Modified time: 2021/6/2 4:30 下午
*/
package grpc
import (
"context"
"crypto/tls"
"math"
"time"
pbErr "gitee.com/brucewangzhihua1/go-public-library/errors"
log "gitee.com/brucewangzhihua1/go-public-library/logger"
"gitee.com/brucewangzhihua1/go-public-library/server/grpc/interceptors/logging"
requesttag "gitee.com/brucewangzhihua1/go-public-library/server/grpc/interceptors/request_tag"
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
)
const (
infinity = time.Duration(math.MaxInt64)
defaultMaxMsgSize = 4 << 20
defaultMaxConcurrentStreams = 100000
defaultKeepAliveTime = 30 * time.Second
defaultConnectionIdleTime = 10 * time.Second
defaultMaxServerConnectionAgeGrace = 10 * time.Second
defaultMiniKeepAliveTimeRate = 2
)
type Option func(*Options)
type Options struct {
id string
domain string
addr string
tls *tls.Config
keepAlive time.Duration
timeout time.Duration
maxConnectionAge time.Duration
maxConnectionAgeGrace time.Duration
maxConcurrentStreams int
maxMsgSize int
unaryServerInterceptors []grpc.UnaryServerInterceptor
streamServerInterceptors []grpc.StreamServerInterceptor
ctx context.Context
}
func WithContextOption(c context.Context) Option {
return func(o *Options) {
o.ctx = c
}
}
func WithIDOption(s string) Option {
return func(o *Options) {
o.id = s
}
}
func WithDomainOption(s string) Option {
return func(o *Options) {
o.domain = s
}
}
func WithAddrOption(s string) Option {
return func(o *Options) {
o.addr = s
}
}
func WithTlsOption(tls *tls.Config) Option {
return func(o *Options) {
o.tls = tls
}
}
func WithKeepAliveOption(t time.Duration) Option {
return func(o *Options) {
o.keepAlive = t
}
}
func WithTimeoutOption(t time.Duration) Option {
return func(o *Options) {
o.keepAlive = t
}
}
func WithMaxConnectionAgeOption(t time.Duration) Option {
return func(o *Options) {
o.maxConnectionAge = t
}
}
func WithMaxConnectionAgeGraceOption(t time.Duration) Option {
return func(o *Options) {
o.maxConnectionAgeGrace = t
}
}
func WithMaxConcurrentStreamsOption(i int) Option {
return func(o *Options) {
o.maxConcurrentStreams = i
}
}
func WithMaxMsgSizeOption(i int) Option {
return func(o *Options) {
o.maxMsgSize = i
}
}
func WithUnaryServerInterceptorsOption(u ...grpc.UnaryServerInterceptor) Option {
return func(o *Options) {
if o.unaryServerInterceptors == nil {
o.unaryServerInterceptors = make([]grpc.UnaryServerInterceptor, 0)
}
o.unaryServerInterceptors = append(o.unaryServerInterceptors, u...)
}
}
func WithStreamServerInterceptorsOption(u ...grpc.StreamServerInterceptor) Option {
return func(o *Options) {
if o.streamServerInterceptors == nil {
o.streamServerInterceptors = make([]grpc.StreamServerInterceptor, 0)
}
o.streamServerInterceptors = append(o.streamServerInterceptors, u...)
}
}
func defaultOptions() *Options {
return &Options{
addr: ":0",
keepAlive: defaultKeepAliveTime,
timeout: defaultConnectionIdleTime,
maxConnectionAge: infinity,
maxConnectionAgeGrace: defaultMaxServerConnectionAgeGrace,
maxConcurrentStreams: defaultMaxConcurrentStreams,
maxMsgSize: defaultMaxMsgSize,
unaryServerInterceptors: []grpc.UnaryServerInterceptor{
requesttag.UnaryServerInterceptor(),
ctxtags.UnaryServerInterceptor(),
opentracing.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(),
prometheus.UnaryServerInterceptor,
recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(customRecovery("", ""))),
},
streamServerInterceptors: []grpc.StreamServerInterceptor{
requesttag.StreamServerInterceptor(),
ctxtags.StreamServerInterceptor(),
opentracing.StreamServerInterceptor(),
logging.StreamServerInterceptor(),
prometheus.StreamServerInterceptor,
recovery.StreamServerInterceptor(recovery.WithRecoveryHandler(customRecovery("", ""))),
},
}
}
func customRecovery(id, domain string) recovery.RecoveryHandlerFunc {
return func(p interface{}) (err error) {
log.Errorf("panic triggered: %v", p)
return pbErr.New(id, domain, pbErr.InternalServerError)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/brucewangzhihua1/go-public-library.git
git@gitee.com:brucewangzhihua1/go-public-library.git
brucewangzhihua1
go-public-library
go公共库
v1.0.1

搜索帮助