connection

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Connected indicates that the connection to the service is currently established.
	Connected = 1.0
	// Disconnected indicates that the connection to the service is currently not established.
	Disconnected = 0

	// TODO: All services including the orderer must use the same default maximum message size.
	//       Hence, we need to move this constant to fabric-x-common.
	// MaxMsgSize is set to 100MB.
	MaxMsgSize = 100 * 1024 * 1024
)
View Source
const (
	UnmentionedTLSMode = ""
	NoneTLSMode        = "none"
	OneSideTLSMode     = "tls"
	MutualTLSMode      = "mtls"
	DefaultTLSMode     = NoneTLSMode

	// DefaultTLSMinVersion is the minimum version required to achieve secure connections.
	DefaultTLSMinVersion = tls.VersionTLS12
)

usage: TLS configuration modes.

View Source
const DefaultHost = "localhost"

DefaultHost is the default hostname used for service endpoints.

Variables

This section is empty.

Functions

func AddressString

func AddressString[T WithAddress](addresses ...T) string

AddressString returns the addresses as a string with comma as a separator between them.

func BuildCertPool added in v1.0.0

func BuildCertPool(rootCAs ...[]byte) (*x509.CertPool, error)

BuildCertPool creates a new x509 certificate pool from the given root CA certificates. If no root CA certificates are provided, an error is returned. If any of the root CA certificates cannot be parsed, an error is returned. Otherwise, the function returns the created certificate pool.

func CalcMaxAttempts added in v0.2.0

func CalcMaxAttempts(initialInterval, maxInterval, multiplier, maxElapsedTime float64) int

CalcMaxAttempts calculates the number of attempts given the following parameters: - initialInterval > 0 - maxInterval >= i - multiplier > 1 - maxElapsedTime > i.

func CloseConnections

func CloseConnections[T io.Closer](connections ...T) error

CloseConnections calls [closer.Close()] for all the given connections and return the close errors.

func CloseConnectionsLog

func CloseConnectionsLog[T io.Closer](connections ...T)

CloseConnectionsLog calls [closer.Close()] for all the given connections and log the close errors.

func ExtendCertPool added in v1.0.0

func ExtendCertPool(certPool *x509.CertPool, rootCAs ...[]byte) bool

ExtendCertPool appends the given root CA certificates to the given certificate pool. If any of the root CA certificates cannot be parsed, the function returns false. Otherwise, the function returns true.

func FilterStreamRPCError

func FilterStreamRPCError(rpcErr error) error

FilterStreamRPCError filters RPC errors that caused due to ending stream.

func MakeGrpcRetryPolicyJSON added in v0.2.0

func MakeGrpcRetryPolicyJSON(p *retry.Profile) string

MakeGrpcRetryPolicyJSON defines the retry policy for a gRPC client connection. The retry policy applies to all subsequent gRPC calls made through the client connection. Our GRPC retry policy is applicable only for the following status codes:

(1) UNAVAILABLE	The service is currently unavailable (e.g., transient network issue, server down).
(2) DEADLINE_EXCEEDED	Operation took too long (deadline passed).
(3) RESOURCE_EXHAUSTED	Some resource (e.g., quota) has been exhausted; the operation cannot proceed.

func NewClientGRPCTransportCredentials added in v1.0.0

func NewClientGRPCTransportCredentials(c *TLSCredentials) (credentials.TransportCredentials, error)

NewClientGRPCTransportCredentials returns the gRPC transport credentials to be used by a client, based on the provided TLS credentials.

func NewConnection added in v0.1.7

func NewConnection(p ClientParameters) (*grpc.ClientConn, error)

NewConnection creates a connection with the given parameters. It will not attempt to create a connection with the remote.

func NewConnectionPerEndpoint added in v0.1.7

func NewConnectionPerEndpoint(config *MultiClientConfig) ([]*grpc.ClientConn, error)

NewConnectionPerEndpoint creates a list of connections; one for each endpoint in the given config.

func NewLoadBalancedConnection added in v0.1.7

func NewLoadBalancedConnection(config *MultiClientConfig) (*grpc.ClientConn, error)

NewLoadBalancedConnection creates a connection with load balancing between the endpoints in the given config.

func NewServerGRPCTransportCredentials added in v1.0.0

func NewServerGRPCTransportCredentials(c *TLSCredentials) (credentials.TransportCredentials, error)

NewServerGRPCTransportCredentials returns the gRPC transport credentials to be used by a server, based on the provided TLS credentials.

func NewSingleConnection added in v0.1.7

func NewSingleConnection(config *ClientConfig) (*grpc.ClientConn, error)

NewSingleConnection creates a single connection given a client config.

func RunHealthCheck added in v0.2.0

func RunHealthCheck(ctx context.Context, endpoint Endpoint, tlsConfig TLSConfig) error

RunHealthCheck dials the given endpoint and performs a gRPC health check. Returns nil if the service reports SERVING, otherwise returns an error.

Types

type ClientConfig

type ClientConfig struct {
	Endpoint *Endpoint      `mapstructure:"endpoint"`
	TLS      TLSConfig      `mapstructure:"tls"`
	Retry    *retry.Profile `mapstructure:"reconnect"`
}

ClientConfig contains a single endpoint, TLS config, and retry profile.

type ClientParameters added in v0.1.9

type ClientParameters struct {
	Address        string
	Creds          credentials.TransportCredentials
	Retry          *retry.Profile
	AdditionalOpts []grpc.DialOption
}

ClientParameters contain connection parameters.

type DialInfo added in v1.0.0

type DialInfo struct {
	Endpoints []*Endpoint
	TLS       TLSCredentials
	Retry     *retry.Profile
}

DialInfo contains the parameters to dial a connection.

func NewDialInfo added in v1.0.0

func NewDialInfo(config *MultiClientConfig) (*DialInfo, error)

NewDialInfo creates dial info from a client config.

func (*DialInfo) NewConnectionPerEndpoint added in v1.0.0

func (d *DialInfo) NewConnectionPerEndpoint() ([]*grpc.ClientConn, error)

NewConnectionPerEndpoint creates a list of connections; one for each endpoint.

func (*DialInfo) NewLoadBalancedConnection added in v1.0.0

func (d *DialInfo) NewLoadBalancedConnection() (*grpc.ClientConn, error)

NewLoadBalancedConnection creates a connection with load balancing between the endpoints.

type Endpoint

type Endpoint struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
}

Endpoint describes a remote endpoint.

func (*Endpoint) Address

func (e *Endpoint) Address() string

Address returns a string representation of the endpoint's address.

func (*Endpoint) Empty

func (e *Endpoint) Empty() bool

Empty returns true if no host and no port are assigned.

func (*Endpoint) String

func (e *Endpoint) String() string

String returns a string representation of the endpoint.

type MultiClientConfig added in v0.1.7

type MultiClientConfig struct {
	Endpoints []*Endpoint    `mapstructure:"endpoints"`
	TLS       TLSConfig      `mapstructure:"tls"`
	Retry     *retry.Profile `mapstructure:"reconnect"`
}

MultiClientConfig contains the endpoints, TLS config, and retry profile. This config allows the support of number of different endpoints to multiple service instances.

type TLSConfig added in v0.1.7

type TLSConfig struct {
	Mode string `mapstructure:"mode" validate:"omitempty,oneof=tls mtls none"`
	// CertPath is the path to the certificate file (public key).
	CertPath string `mapstructure:"cert-path"`
	// KeyPath is the path to the key file (private key).
	KeyPath     string   `mapstructure:"key-path"`
	CACertPaths []string `mapstructure:"ca-cert-paths"`
}

TLSConfig holds the TLS options and certificate paths used for secure communication between servers and clients. Credentials are built based on the configuration mode. For example, If only server-side TLS is required, the certificate pool (certPool) is not built (for a server), since the relevant certificates paths are defined in the YAML according to the selected mode.

func (TLSConfig) ClientCredentials added in v0.1.7

func (c TLSConfig) ClientCredentials() (credentials.TransportCredentials, error)

ClientCredentials converts TLSConfig into a TLSCredentials struct and generates client creds.

func (TLSConfig) ServerCredentials added in v0.1.7

func (c TLSConfig) ServerCredentials() (credentials.TransportCredentials, error)

ServerCredentials converts TLSConfig into a TLSCredentials struct and generates server creds.

type TLSCredentials added in v1.0.0

type TLSCredentials struct {
	Mode    string
	Cert    []byte
	Key     []byte
	CACerts [][]byte
}

TLSCredentials holds the loaded runtime TLS credentials (certificate, key, CA certs).

func NewClientTLSCredentials added in v1.0.0

func NewClientTLSCredentials(c TLSConfig) (*TLSCredentials, error)

NewClientTLSCredentials converts a client TLSConfig with path fields into a struct that holds the actual bytes of the certificates.

Certificate loading behavior by mode:

  • none/unmentioned: No certificates loaded
  • tls (one-way): Loads CA certs only for server verification (client cert + key NOT loaded)
  • mtls (mutual): Loads CA certs + client cert + key for mutual authentication

func NewServerTLSCredentials added in v1.0.0

func NewServerTLSCredentials(c TLSConfig) (*TLSCredentials, error)

NewServerTLSCredentials converts a server TLSConfig with path fields into a struct that holds the actual bytes of the certificates.

Certificate loading behavior by mode:

  • none/unmentioned: No certificates loaded
  • tls (one-way): Loads server cert + key only (CA certs NOT loaded)
  • mtls (mutual): Loads server cert + key + CA certs for client verification

func (*TLSCredentials) CreateClientTLSConfig added in v1.0.0

func (c *TLSCredentials) CreateClientTLSConfig() (*tls.Config, error)

CreateClientTLSConfig returns a TLS config to be used by a client.

func (*TLSCredentials) CreateServerTLSConfig added in v1.0.0

func (c *TLSCredentials) CreateServerTLSConfig() (*tls.Config, error)

CreateServerTLSConfig returns a TLS config to be used by a server.

type WithAddress

type WithAddress interface {
	Address() string
}

WithAddress represents any type that can generate an address.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL