add more options for grpc
This commit is contained in:
@ -17,6 +17,7 @@ import (
|
||||
"github.com/go-gost/x/registry"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -67,6 +68,19 @@ func (l *grpcListener) Init(md md.Metadata) (err error) {
|
||||
if !l.md.insecure {
|
||||
opts = append(opts, grpc.Creds(credentials.NewTLS(l.options.TLSConfig)))
|
||||
}
|
||||
if l.md.keepalive {
|
||||
opts = append(opts,
|
||||
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
||||
MinTime: l.md.keepaliveMinTime,
|
||||
PermitWithoutStream: l.md.keepalivePermitWithoutStream,
|
||||
}),
|
||||
grpc.KeepaliveParams(keepalive.ServerParameters{
|
||||
MaxConnectionIdle: l.md.keepaliveMaxConnectionIdle,
|
||||
Time: l.md.keepaliveTime,
|
||||
Timeout: l.md.keepaliveTimeout,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
l.server = grpc.NewServer(opts...)
|
||||
l.addr = ln.Addr()
|
||||
|
@ -1,6 +1,8 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
)
|
||||
@ -10,24 +12,44 @@ const (
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
backlog int
|
||||
insecure bool
|
||||
path string
|
||||
backlog int
|
||||
insecure bool
|
||||
path string
|
||||
keepalive bool
|
||||
keepaliveMinTime time.Duration
|
||||
keepaliveTime time.Duration
|
||||
keepaliveTimeout time.Duration
|
||||
keepalivePermitWithoutStream bool
|
||||
keepaliveMaxConnectionIdle time.Duration
|
||||
}
|
||||
|
||||
func (l *grpcListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
backlog = "backlog"
|
||||
insecure = "grpcInsecure"
|
||||
path = "path"
|
||||
)
|
||||
|
||||
l.md.backlog = mdutil.GetInt(md, backlog)
|
||||
l.md.backlog = mdutil.GetInt(md, "grpc.backlog", "backlog")
|
||||
if l.md.backlog <= 0 {
|
||||
l.md.backlog = defaultBacklog
|
||||
}
|
||||
|
||||
l.md.insecure = mdutil.GetBool(md, insecure)
|
||||
l.md.path = mdutil.GetString(md, path)
|
||||
l.md.insecure = mdutil.GetBool(md, "grpc.insecure", "grpcInsecure", "insecure")
|
||||
l.md.path = mdutil.GetString(md, "grpc.path", "path")
|
||||
|
||||
l.md.keepalive = mdutil.GetBool(md, "grpc.keepalive", "keepalive", "keepAlive")
|
||||
if l.md.keepalive {
|
||||
l.md.keepaliveMinTime = mdutil.GetDuration(md, "grpc.keepalive.minTime", "keepalive.minTime")
|
||||
if l.md.keepaliveMinTime <= 0 {
|
||||
l.md.keepaliveMinTime = 30 * time.Second
|
||||
}
|
||||
l.md.keepaliveTime = mdutil.GetDuration(md, "grpc.keepalive.time", "keepalive.time")
|
||||
if l.md.keepaliveTime <= 0 {
|
||||
l.md.keepaliveTime = 60 * time.Second
|
||||
}
|
||||
l.md.keepaliveTimeout = mdutil.GetDuration(md, "grpc.keepalive.timeout", "keepalive.timeout")
|
||||
if l.md.keepaliveTimeout <= 0 {
|
||||
l.md.keepaliveTimeout = 30 * time.Second
|
||||
}
|
||||
|
||||
l.md.keepalivePermitWithoutStream = mdutil.GetBool(md, "grpc.keepalive.permitWithoutStream", "keepalive.permitWithoutStream")
|
||||
l.md.keepaliveMaxConnectionIdle = mdutil.GetDuration(md, "grpc.keepalive.maxConnectionIdle", "keepalive.maxConnectionIdle")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user