connection

package
v0.34.0-crescendo-prev... Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AccessClient clientType = iota
	ExecutionClient
)
View Source
const DefaultClientTimeout = 3 * time.Second

DefaultClientTimeout is used when making a GRPC request to a collection node or an execution node.

Variables

This section is empty.

Functions

func WithClientTimeoutOption

func WithClientTimeoutOption(timeout time.Duration) grpc.DialOption

WithClientTimeoutOption is a helper function to create a GRPC dial option with the specified client timeout interceptor.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache represents a cache of CachedClient instances with a given maximum size.

func NewCache

func NewCache(cache *lru.Cache[string, *CachedClient], size int) *Cache

NewCache creates a new Cache with the specified maximum size and the underlying LRU cache.

func (*Cache) Add

func (c *Cache) Add(address string, client *CachedClient) (evicted bool)

Add adds a CachedClient to the cache with the given address. It returns a boolean indicating whether an existing entry was evicted.

func (*Cache) Contains

func (c *Cache) Contains(address string) (containKey bool)

Contains checks if the cache contains an entry with the given address. It returns a boolean indicating whether the address is present in the cache.

func (*Cache) Get

func (c *Cache) Get(address string) (*CachedClient, bool)

Get retrieves the CachedClient for the given address from the cache. It returns the CachedClient and a boolean indicating whether the entry exists in the cache.

func (*Cache) GetOrAdd

func (c *Cache) GetOrAdd(address string, timeout time.Duration) (*CachedClient, bool)

GetOrAdd atomically gets the CachedClient for the given address from the cache, or adds a new one if none existed. New entries are added to the cache with their mutex locked. This ensures that the caller gets priority when working with the new client, allowing it to create the underlying connection. Clients retrieved from the cache are returned without modifying their lock.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of CachedClient entries in the cache.

func (*Cache) MaxSize

func (c *Cache) MaxSize() int

MaxSize returns the maximum size of the cache.

func (*Cache) Remove

func (c *Cache) Remove(address string) (present bool)

Remove removes the CachedClient entry from the cache with the given address. It returns a boolean indicating whether the entry was present and removed.

type CachedClient

type CachedClient struct {
	ClientConn *grpc.ClientConn
	Address    string
	// contains filtered or unexported fields
}

CachedClient represents a gRPC client connection that is cached for reuse.

func (*CachedClient) Close

func (cc *CachedClient) Close()

Close closes the CachedClient connection. It marks the connection for closure and waits asynchronously for ongoing requests to complete before closing the connection.

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	// Enabled specifies whether the circuit breaker is enabled for collection and execution API clients.
	Enabled bool
	// RestoreTimeout specifies the duration after which the circuit breaker will restore the connection to the client
	// after closing it due to failures.
	RestoreTimeout time.Duration
	// MaxFailures specifies the maximum number of failed calls to the client that will cause the circuit breaker
	// to close the connection.
	MaxFailures uint32
	// MaxRequests specifies the maximum number of requests to check if connection restored after timeout.
	MaxRequests uint32
}

CircuitBreakerConfig is a configuration struct for the circuit breaker.

type ConnectionFactory

type ConnectionFactory interface {
	// GetAccessAPIClient gets an access API client for the specified address using the default CollectionGRPCPort, networkPubKey is optional,
	// and it is used for secure gRPC connection. Can be nil for an unsecured connection.
	// The returned io.Closer should close the connection after the call if no error occurred during client creation.
	GetAccessAPIClient(address string, networkPubKey crypto.PublicKey) (access.AccessAPIClient, io.Closer, error)
	// GetAccessAPIClientWithPort gets an access API client for the specified address with port, networkPubKey is optional,
	// and it is used for secure gRPC connection. Can be nil for an unsecured connection.
	// The returned io.Closer should close the connection after the call if no error occurred during client creation.
	GetAccessAPIClientWithPort(address string, networkPubKey crypto.PublicKey) (access.AccessAPIClient, io.Closer, error)
	// GetExecutionAPIClient gets an execution API client for the specified address using the default ExecutionGRPCPort.
	// The returned io.Closer should close the connection after the call if no error occurred during client creation.
	GetExecutionAPIClient(address string) (execution.ExecutionAPIClient, io.Closer, error)
}

ConnectionFactory is an interface for creating access and execution API clients.

type ConnectionFactoryImpl

type ConnectionFactoryImpl struct {
	CollectionGRPCPort        uint
	ExecutionGRPCPort         uint
	CollectionNodeGRPCTimeout time.Duration
	ExecutionNodeGRPCTimeout  time.Duration
	AccessMetrics             module.AccessMetrics
	Log                       zerolog.Logger
	Manager                   Manager
}

func (*ConnectionFactoryImpl) GetAccessAPIClient

func (cf *ConnectionFactoryImpl) GetAccessAPIClient(address string, networkPubKey crypto.PublicKey) (access.AccessAPIClient, io.Closer, error)

GetAccessAPIClient gets an access API client for the specified address using the default CollectionGRPCPort. The networkPubKey is the public key used for secure gRPC connection. Can be nil for an unsecured connection. The returned io.Closer should close the connection after the call if no error occurred during client creation.

func (*ConnectionFactoryImpl) GetAccessAPIClientWithPort

func (cf *ConnectionFactoryImpl) GetAccessAPIClientWithPort(address string, networkPubKey crypto.PublicKey) (access.AccessAPIClient, io.Closer, error)

GetAccessAPIClientWithPort gets an access API client for the specified address with port. The networkPubKey is the public key used for secure gRPC connection. Can be nil for an unsecured connection. The returned io.Closer should close the connection after the call if no error occurred during client creation.

func (*ConnectionFactoryImpl) GetExecutionAPIClient

func (cf *ConnectionFactoryImpl) GetExecutionAPIClient(address string) (execution.ExecutionAPIClient, io.Closer, error)

GetExecutionAPIClient gets an execution API client for the specified address using the default ExecutionGRPCPort. The returned io.Closer should close the connection after the call if no error occurred during client creation.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager provides methods for getting and managing gRPC client connections.

func NewManager

func NewManager(
	cache *Cache,
	logger zerolog.Logger,
	metrics module.AccessMetrics,
	maxMsgSize uint,
	circuitBreakerConfig CircuitBreakerConfig,
	compressorName string,
) Manager

NewManager creates a new Manager with the specified parameters.

func (*Manager) GetConnection

func (m *Manager) GetConnection(
	grpcAddress string,
	timeout time.Duration,
	clientType clientType,
	networkPubKey crypto.PublicKey,
) (*grpc.ClientConn, io.Closer, error)

GetConnection returns a gRPC client connection for the given grpcAddress and timeout. If a cache is used, it retrieves a cached connection, otherwise creates a new connection. It returns the client connection and an io.Closer to close the connection when done. The networkPubKey is the public key used for creating secure gRPC connection. Can be nil for an unsecured connection.

func (*Manager) HasCache

func (m *Manager) HasCache() bool

HasCache returns true if the Manager has a cache, false otherwise.

func (*Manager) Remove

func (m *Manager) Remove(grpcAddress string) bool

Remove removes the gRPC client connection associated with the given grpcAddress from the cache. It returns true if the connection was removed successfully, false otherwise.

type ProxyConnectionFactory

type ProxyConnectionFactory struct {
	ConnectionFactory
	// contains filtered or unexported fields
}

ProxyConnectionFactory wraps an existing ConnectionFactory and allows getting API clients for a target address.

func (*ProxyConnectionFactory) GetAccessAPIClient

func (p *ProxyConnectionFactory) GetAccessAPIClient(address string, networkPubKey crypto.PublicKey) (access.AccessAPIClient, io.Closer, error)

GetAccessAPIClient gets an access API client for a target address using the default CollectionGRPCPort. The networkPubKey is the public key used for a secure gRPC connection. It can be nil for an unsecured connection. The returned io.Closer should close the connection after the call if no error occurred during client creation.

func (*ProxyConnectionFactory) GetExecutionAPIClient

func (p *ProxyConnectionFactory) GetExecutionAPIClient(address string) (execution.ExecutionAPIClient, io.Closer, error)

GetExecutionAPIClient gets an execution API client for a target address using the default ExecutionGRPCPort. The returned io.Closer should close the connection after the call if no error occurred during client creation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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