Repository: grpc/grpc-go
File: xds/internal/clients/grpctransport/grpc_transport.go (Go)

Code before:
// clients.ServerIdentifier.Extensions field (See Example).
type ServerIdentifierExtension struct {
	// ConfigName is the name of the configuration to use for this transport.
	// It must be present as a key in the map of configs passed to NewBuilder.
	ConfigName string
}

// Builder creates gRPC-based Transports. It must be paired with ServerIdentifiers
// that contain an Extension field of type ServerIdentifierExtension.
type Builder struct {
	// configs is a map of configuration names to their respective Config.
	configs map[string]Config

	mu sync.Mutex
	// connections is a map of clients.ServerIdentifiers in use by the Builder
	// to connect to different servers.
	connections map[clients.ServerIdentifier]*grpc.ClientConn
	// refs tracks the number of active references to each connection.
	refs map[clients.ServerIdentifier]int
}

// Config defines the configuration for a gRPC transport, including credentials
// and an optional custom new client function.
type Config struct {
	// Credential is the credentials bundle to be used for the connection.
	Credential credentials.Bundle
	// GRPCNewClient is an optional custom function to establish a gRPC connection.
	// If nil, grpc.NewClient will be used as the default.
	GRPCNewClient func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
}

// NewBuilder provides a builder for creating gRPC-based Transports using
// the credentials from provided map of credentials names to
// credentials.Bundle.
func NewBuilder(configs map[string]Config) *Builder {
	return &Builder{
		configs:     configs,
		connections: make(map[clients.ServerIdentifier]*grpc.ClientConn),
		refs:        make(map[clients.ServerIdentifier]int),
	}
}

// Build returns a gRPC-based clients.Transport.
//
// The Extension field of the ServerIdentifier must be a ServerIdentifierExtension.
func (b *Builder) Build(si clients.ServerIdentifier) (clients.Transport, error) {
	if si.ServerURI == "" {
		return nil, fmt.Errorf("grpctransport: ServerURI is not set in ServerIdentifier")
	}
	if si.Extensions == nil {
		return nil, fmt.Errorf("grpctransport: Extensions is not set in ServerIdentifier")

Review comment (security):
Nit:
```suggestion
	Credentials credentials.Bundle
```

Code after:
// clients.ServerIdentifier.Extensions field (See Example).
type ServerIdentifierExtension struct {
	// ConfigName is the name of the configuration to use for this transport.
	// It must be present as a key in the map of configs passed to NewBuilder.
	ConfigName string
}

// Builder creates gRPC-based Transports. It must be paired with ServerIdentifiers
// that contain an Extension field of type ServerIdentifierExtension.
type Builder struct {
	// configs is a map of configuration names to their respective Config.
	configs map[string]Config

	mu sync.Mutex
	// connections is a map of clients.ServerIdentifiers in use by the Builder
	// to connect to different servers.
	connections map[clients.ServerIdentifier]*grpc.ClientConn
	// refs tracks the number of active references to each connection.
	refs map[clients.ServerIdentifier]int
}

// Config defines the configuration for connecting to a gRPC server, including
// credentials and an optional custom new client function.
type Config struct {
	// Credentials is the credentials bundle to be used for the connection.
	Credentials credentials.Bundle
	// GRPCNewClient is an optional custom function to establish a gRPC connection.
	// If nil, grpc.NewClient will be used as the default.
	GRPCNewClient func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
}

// NewBuilder provides a builder for creating gRPC-based Transports using
// the credentials from provided map of credentials names to
// credentials.Bundle.
func NewBuilder(configs map[string]Config) *Builder {
	return &Builder{
		configs:     configs,
		connections: make(map[clients.ServerIdentifier]*grpc.ClientConn),
		refs:        make(map[clients.ServerIdentifier]int),
	}
}

// Build returns a gRPC-based clients.Transport.
//
// The Extension field of the ServerIdentifier must be a ServerIdentifierExtension.
func (b *Builder) Build(si clients.ServerIdentifier) (clients.Transport, error) {
	if si.ServerURI == "" {
		return nil, fmt.Errorf("grpctransport: ServerURI is not set in ServerIdentifier")
	}
	if si.Extensions == nil {
		return nil, fmt.Errorf("grpctransport: Extensions is not set in ServerIdentifier")