add more options for grpc
This commit is contained in:
@ -66,6 +66,7 @@ func (c *conn) Close() error {
|
||||
case <-c.closed:
|
||||
default:
|
||||
close(c.closed)
|
||||
return c.c.CloseSend()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/dialer"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
@ -14,6 +13,7 @@ import (
|
||||
"google.golang.org/grpc/backoff"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -82,7 +82,7 @@ func (d *grpcDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialO
|
||||
grpc.WithAuthority(host),
|
||||
grpc.WithConnectParams(grpc.ConnectParams{
|
||||
Backoff: backoff.DefaultConfig,
|
||||
MinConnectTimeout: 10 * time.Second,
|
||||
MinConnectTimeout: d.md.minConnectTimeout,
|
||||
}),
|
||||
grpc.FailOnNonTempDialError(true),
|
||||
}
|
||||
@ -92,6 +92,14 @@ func (d *grpcDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialO
|
||||
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
}
|
||||
|
||||
if d.md.keepalive {
|
||||
grpcOpts = append(grpcOpts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: d.md.keepaliveTime,
|
||||
Timeout: d.md.keepaliveTimeout,
|
||||
PermitWithoutStream: d.md.keepalivePermitWithoutStream,
|
||||
}))
|
||||
}
|
||||
|
||||
cc, err := grpc.DialContext(ctx, addr, grpcOpts...)
|
||||
if err != nil {
|
||||
d.options.Logger.Error(err)
|
||||
|
@ -1,26 +1,43 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
insecure bool
|
||||
host string
|
||||
path string
|
||||
insecure bool
|
||||
host string
|
||||
path string
|
||||
keepalive bool
|
||||
keepaliveTime time.Duration
|
||||
keepaliveTimeout time.Duration
|
||||
keepalivePermitWithoutStream bool
|
||||
minConnectTimeout time.Duration
|
||||
}
|
||||
|
||||
func (d *grpcDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
insecure = "grpcInsecure"
|
||||
host = "host"
|
||||
path = "path"
|
||||
)
|
||||
|
||||
d.md.insecure = mdutil.GetBool(md, insecure)
|
||||
d.md.host = mdutil.GetString(md, host)
|
||||
d.md.path = mdutil.GetString(md, path)
|
||||
d.md.insecure = mdutil.GetBool(md, "grpc.insecure", "grpcInsecure", "insecure")
|
||||
d.md.host = mdutil.GetString(md, "grpc.authority", "grpc.host", "host")
|
||||
d.md.path = mdutil.GetString(md, "grpc.path", "path")
|
||||
d.md.keepalive = mdutil.GetBool(md, "grpc.keepalive", "keepalive", "keepAlive")
|
||||
if d.md.keepalive {
|
||||
d.md.keepaliveTime = mdutil.GetDuration(md, "grpc.keepalive.time", "keepalive.time")
|
||||
if d.md.keepaliveTime <= 0 {
|
||||
d.md.keepaliveTime = 30 * time.Second
|
||||
}
|
||||
d.md.keepaliveTimeout = mdutil.GetDuration(md, "grpc.keepalive.timeout", "keepalive.timeout")
|
||||
if d.md.keepaliveTimeout <= 0 {
|
||||
d.md.keepaliveTimeout = 30 * time.Second
|
||||
}
|
||||
d.md.keepalivePermitWithoutStream = mdutil.GetBool(md, "grpc.keepalive.permitWithoutStream", "keepalive.permitWithoutStream")
|
||||
}
|
||||
d.md.minConnectTimeout = mdutil.GetDuration(md, "grpc.minConnectTimeout", "minConnectTimeout")
|
||||
if d.md.minConnectTimeout <= 0 {
|
||||
d.md.minConnectTimeout = 30 * time.Second
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user