first commit
This commit is contained in:
98
pkg/grpclient/client.go
Normal file
98
pkg/grpclient/client.go
Normal file
@ -0,0 +1,98 @@
|
||||
package grpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultDialTimeout = time.Second * 2
|
||||
)
|
||||
|
||||
type Option func(*option)
|
||||
|
||||
type option struct {
|
||||
credential credentials.TransportCredentials
|
||||
keepalive *keepalive.ClientParameters
|
||||
dialTimeout time.Duration
|
||||
unaryInterceptor grpc.UnaryClientInterceptor
|
||||
}
|
||||
|
||||
// WithCredential setup credential for tls
|
||||
func WithCredential(credential credentials.TransportCredentials) Option {
|
||||
return func(opt *option) {
|
||||
opt.credential = credential
|
||||
}
|
||||
}
|
||||
|
||||
// WithKeepAlive setup keepalive parameters
|
||||
func WithKeepAlive(keepalive *keepalive.ClientParameters) Option {
|
||||
return func(opt *option) {
|
||||
opt.keepalive = keepalive
|
||||
}
|
||||
}
|
||||
|
||||
// WithDialTimeout set up the dial timeout
|
||||
func WithDialTimeout(timeout time.Duration) Option {
|
||||
return func(opt *option) {
|
||||
opt.dialTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
func WithUnaryInterceptor(unaryInterceptor grpc.UnaryClientInterceptor) Option {
|
||||
return func(opt *option) {
|
||||
opt.unaryInterceptor = unaryInterceptor
|
||||
}
|
||||
}
|
||||
|
||||
func New(target string, options ...Option) (*grpc.ClientConn, error) {
|
||||
if target == "" {
|
||||
return nil, errors.New("target required")
|
||||
}
|
||||
|
||||
opt := new(option)
|
||||
for _, f := range options {
|
||||
f(opt)
|
||||
}
|
||||
|
||||
kacp := defaultKeepAlive
|
||||
if opt.keepalive != nil {
|
||||
kacp = opt.keepalive
|
||||
}
|
||||
|
||||
dialTimeout := defaultDialTimeout
|
||||
if opt.dialTimeout > 0 {
|
||||
dialTimeout = opt.dialTimeout
|
||||
}
|
||||
|
||||
dialOptions := []grpc.DialOption{
|
||||
grpc.WithBlock(),
|
||||
grpc.WithKeepaliveParams(*kacp),
|
||||
}
|
||||
|
||||
if opt.unaryInterceptor != nil {
|
||||
dialOptions = append(dialOptions, grpc.WithUnaryInterceptor(opt.unaryInterceptor))
|
||||
}
|
||||
|
||||
if opt.credential == nil {
|
||||
dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
} else {
|
||||
dialOptions = append(dialOptions, grpc.WithTransportCredentials(opt.credential))
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), dialTimeout)
|
||||
defer cancel()
|
||||
|
||||
conn, err := grpc.DialContext(ctx, target, dialOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
15
pkg/grpclient/keepalive.go
Normal file
15
pkg/grpclient/keepalive.go
Normal file
@ -0,0 +1,15 @@
|
||||
package grpclient
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultKeepAlive = &keepalive.ClientParameters{
|
||||
Time: 10 * time.Second,
|
||||
Timeout: time.Second,
|
||||
PermitWithoutStream: true,
|
||||
}
|
||||
)
|
Reference in New Issue
Block a user