Pool: add --txpool.v2 flag to rpcdaemon (#57)

* grpc connect func

* grpc connect func

* grpc connect func

* grpc connect func

* grpc connect func

* grpc connect func
This commit is contained in:
Alex Sharov 2021-09-03 10:24:49 +07:00 committed by GitHub
parent c935d9ff65
commit affb189c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,18 @@
package grpcutil
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"time"
"github.com/c2h5oh/datasize"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
@ -17,6 +20,9 @@ import (
func TLS(tlsCACert, tlsCertFile, tlsKeyFile string) (credentials.TransportCredentials, error) {
// load peer cert/key, ca cert
if tlsCACert == "" {
if tlsCertFile == "" && tlsKeyFile == "" {
return nil, nil
}
return credentials.NewServerTLSFromFile(tlsCertFile, tlsKeyFile)
}
var caCert []byte
@ -35,10 +41,12 @@ func TLS(tlsCACert, tlsCertFile, tlsKeyFile string) (credentials.TransportCreden
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
MinVersion: tls.VersionTLS12,
//nolint:gosec
InsecureSkipVerify: true, // This is to make it work when Common Name does not match - remove when procedure is updated for common name
}), nil
}
func NewServer(rateLimit uint32, creds *credentials.TransportCredentials) *grpc.Server {
func NewServer(rateLimit uint32, creds credentials.TransportCredentials) *grpc.Server {
var (
streamInterceptors []grpc.StreamServerInterceptor
unaryInterceptors []grpc.UnaryServerInterceptor
@ -66,11 +74,7 @@ func NewServer(rateLimit uint32, creds *credentials.TransportCredentials) *grpc.
}),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamInterceptors...)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
}
if creds == nil {
// no specific opts
} else {
opts = append(opts, grpc.Creds(*creds))
grpc.Creds(creds),
}
grpcServer = grpc.NewServer(opts...)
@ -80,3 +84,32 @@ func NewServer(rateLimit uint32, creds *credentials.TransportCredentials) *grpc.
return grpcServer
}
func Connect(creds credentials.TransportCredentials, dialAddress string) (*grpc.ClientConn, error) {
var dialOpts []grpc.DialOption
backoffCfg := backoff.DefaultConfig
backoffCfg.BaseDelay = 500 * time.Millisecond
backoffCfg.MaxDelay = 10 * time.Second
dialOpts = []grpc.DialOption{
grpc.WithConnectParams(grpc.ConnectParams{Backoff: backoffCfg, MinConnectTimeout: 10 * time.Minute}),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(15 * datasize.MB))),
grpc.WithKeepaliveParams(keepalive.ClientParameters{}),
}
if creds == nil {
dialOpts = append(dialOpts, grpc.WithInsecure())
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds))
}
//if opts.inMemConn != nil {
// dialOpts = append(dialOpts, grpc.WithContextDialer(func(ctx context.Context, url string) (net.Conn, error) {
// return opts.inMemConn.Dial()
// }))
//}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return grpc.DialContext(ctx, dialAddress, dialOpts...)
}