Replacing implicit grpc client initialization with explicit package local variables.
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
mgrpcc "github.com/go-micro/plugins/v4/client/grpc"
|
||||
mbreaker "github.com/go-micro/plugins/v4/wrapper/breaker/gobreaker"
|
||||
@@ -16,11 +15,6 @@ import (
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultClient client.Client
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// ClientOptions represent options (e.g. tls settings) for the grpc clients
|
||||
type ClientOptions struct {
|
||||
tlsMode string
|
||||
@@ -56,60 +50,6 @@ func WithTraceProvider(tp trace.TracerProvider) ClientOption {
|
||||
}
|
||||
}
|
||||
|
||||
// Configure configures the default oOCIS grpc client (e.g. TLS settings)
|
||||
func Configure(opts ...ClientOption) error {
|
||||
var options ClientOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
var outerr error
|
||||
once.Do(func() {
|
||||
reg := registry.GetRegistry()
|
||||
var tlsConfig *tls.Config
|
||||
cOpts := []client.Option{
|
||||
client.Registry(reg),
|
||||
client.Wrap(mbreaker.NewClientWrapper()),
|
||||
client.Wrap(mtracer.NewClientWrapper(
|
||||
mtracer.WithTraceProvider(options.tp),
|
||||
)),
|
||||
}
|
||||
switch options.tlsMode {
|
||||
case "insecure":
|
||||
tlsConfig = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
cOpts = append(cOpts, mgrpcc.AuthTLS(tlsConfig))
|
||||
case "on":
|
||||
tlsConfig = &tls.Config{}
|
||||
// Note: If caCert is empty we use the system's default set of trusted CAs
|
||||
if options.caCert != "" {
|
||||
certs := x509.NewCertPool()
|
||||
pemData, err := os.ReadFile(options.caCert)
|
||||
if err != nil {
|
||||
outerr = err
|
||||
return
|
||||
}
|
||||
if !certs.AppendCertsFromPEM(pemData) {
|
||||
outerr = errors.New("could not initialize default client, adding CA cert failed")
|
||||
return
|
||||
}
|
||||
tlsConfig.RootCAs = certs
|
||||
}
|
||||
cOpts = append(cOpts, mgrpcc.AuthTLS(tlsConfig))
|
||||
// case "off":
|
||||
// default:
|
||||
}
|
||||
|
||||
defaultClient = mgrpcc.NewClient(cOpts...)
|
||||
})
|
||||
return outerr
|
||||
}
|
||||
|
||||
// DefaultClient returns a custom oCIS grpc configured client.
|
||||
func DefaultClient() client.Client {
|
||||
return defaultClient
|
||||
}
|
||||
|
||||
func GetClientOptions(t *shared.GRPCClientTLS) []ClientOption {
|
||||
opts := []ClientOption{
|
||||
WithTLSMode(t.Mode),
|
||||
|
||||
@@ -21,11 +21,6 @@ type Service struct {
|
||||
micro.Service
|
||||
}
|
||||
|
||||
// NewService initializes a new grpc service.
|
||||
func NewService(opts ...Option) (Service, error) {
|
||||
return NewServiceWithClient(DefaultClient(), opts...)
|
||||
}
|
||||
|
||||
// NewServiceWithClient initializes a new grpc service with explicit client.
|
||||
func NewServiceWithClient(client client.Client, opts ...Option) (Service, error) {
|
||||
var mServer server.Server
|
||||
|
||||
Reference in New Issue
Block a user