connect

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 9 Imported by: 18

Documentation

Overview

Contains implementation of connection parameters, using various connection strings, which are stripped of all credentials. If we need to configure a service, the port, ip address, protocol, and other parameters – we use the ConnectionParams object, and relevant helper classes (like ConnectionResolver), for acquiring these parameters, and for discovery of objects, components (which store and retrieve connection parameters).

Discovery Service that store a registry of various end-points (what services are where, and how to connect to them). It knows the end-points, but doesn't have the credentials to connect to them. Separated for security reasons.

IDiscovery – interface for creating registries.

MemoryDiscovery – registry that is stored in memory.

There exist 2 types of discovery:

Static discovery: all services have static IP addresses (like DNS, which also works using static discovery) that are configured from the start and don't change along the way. As of lately, used more often than dynamic, because it is simpler to use and more reliable.

Proxy (or reverse proxy) is created with a dns name, and all the dynamics of starting/restarting/switching from one host to another – everything is nice and clear for the clients. Infrastructure does all the hard work out of the box. Configure sets the static registry. Dynamic discovery: every time a service starts, it registers its address in the discovery service ("Service name" at the following address "IP"). Clients then ask to resolve the address by which the requested service can be reached. The service has a general name, by which other services can resolve it.

If a service stops working, you need to refresh its address, clean stale addresses, heartbeats must be used – lots of problems and challenges. One service can have more than one address.

Index

Constants

This section is empty.

Variables

View Source
var ConnectionUtils = TConnectionUtils{}

Functions

func NewDefaultDiscoveryFactory

func NewDefaultDiscoveryFactory() *build.Factory

Create a new instance of the factory. Returns *build.Factory

Types

type CompositeConnectionResolver added in v1.1.0

type CompositeConnectionResolver struct {
	Overrides ICompositeConnectionResolverOverrides

	// The connection options
	Options *config.ConfigParams

	// The connections resolver.
	ConnectionResolver *ConnectionResolver

	// The credentials resolver.
	CredentialResolver *auth.CredentialResolver

	// The cluster support (multiple connections)
	ClusterSupported bool

	// The default protocol
	DefaultProtocol string

	// The default port number
	DefaultPort int

	// The list of supported protocols
	SupportedProtocols []string
}

*

  • Helper class that resolves connection and credential parameters,
  • validates them and generates connection options. *
  • ### Configuration parameters ### *
  • - connection(s):
  • - discovery_key: (optional) a key to retrieve the connection from IDiscovery]
  • - protocol: communication protocol
  • - host: host name or IP address
  • - port: port number
  • - uri: resource URI or connection string with all parameters in it
  • - credential(s):
  • - store_key: (optional) a key to retrieve the credentials from [ICredentialStore]]
  • - username: user name
  • - password: user password *
  • ### References ### *
  • - *:discovery:*:*:1.0 (optional) IDiscovery] services to resolve connections
  • - *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials

func InheritCompositeConnectionResolver added in v1.1.0

func InheritCompositeConnectionResolver(overrides ICompositeConnectionResolverOverrides) *CompositeConnectionResolver

InheritCompositeConnectionResolver creates new CompositeConnectionResolver Parameters:

  • overrides a child reference with overrides for virtual methods

return *CompositeConnectionResolver

func (*CompositeConnectionResolver) Compose added in v1.1.0

func (c *CompositeConnectionResolver) Compose(correlationId string, connections []*ConnectionParams, credential *auth.CredentialParams,
	parameters *config.ConfigParams) (options *config.ConfigParams, err error)

Composes Composite connection options from connection and credential parameters. - correlationId (optional) transaction id to trace execution through call chain. - connections connection parameters - credential credential parameters - parameters optional parameters - return resolved options or error.

func (*CompositeConnectionResolver) ComposeOptions added in v1.1.0

func (c *CompositeConnectionResolver) ComposeOptions(connections []*ConnectionParams, credential *auth.CredentialParams, parameters *config.ConfigParams) *config.ConfigParams

Composes connection and credential parameters into connection options. This method can be overriden in child classes. - connections a list of connection parameters - credential credential parameters - parameters optional parameters - returns a composed connection options.

func (*CompositeConnectionResolver) Configure added in v1.1.0

func (c *CompositeConnectionResolver) Configure(config *config.ConfigParams)

Configures component by passing configuration parameters. - config configuration parameters to be set.

func (*CompositeConnectionResolver) FinalizeOptions added in v1.1.0

func (c *CompositeConnectionResolver) FinalizeOptions(options *config.ConfigParams) *config.ConfigParams

Finalize merged options This method can be overriden in child classes. - options connection options - returns finalized connection options

func (*CompositeConnectionResolver) MergeConnection added in v1.1.0

func (c *CompositeConnectionResolver) MergeConnection(options *config.ConfigParams, connection *ConnectionParams) *config.ConfigParams

Merges connection options with connection parameters This method can be overriden in child classes. - options connection options - connection connection parameters to be merged - returns merged connection options.

func (*CompositeConnectionResolver) MergeCredential added in v1.1.0

func (c *CompositeConnectionResolver) MergeCredential(options *config.ConfigParams, credential *auth.CredentialParams) *config.ConfigParams

Merges connection options with credential parameters This method can be overriden in child classes. - options connection options - credential credential parameters to be merged - returns merged connection options.

func (*CompositeConnectionResolver) MergeOptional added in v1.1.0

func (c *CompositeConnectionResolver) MergeOptional(options *config.ConfigParams, parameters *config.ConfigParams) *config.ConfigParams

Merges connection options with optional parameters This method can be overriden in child classes. - options connection options - parameters optional parameters to be merged - returns merged connection options.

func (*CompositeConnectionResolver) Resolve added in v1.1.0

func (c *CompositeConnectionResolver) Resolve(correlationId string) (options *config.ConfigParams, err error)

Resolves connection options from connection and credential parameters. - correlationId (optional) transaction id to trace execution through call chain. - return resolved options or error.

func (*CompositeConnectionResolver) SetReferences added in v1.1.0

func (c *CompositeConnectionResolver) SetReferences(references refer.IReferences)

Sets references to dependent components. - references references to locate the component dependencies.

func (*CompositeConnectionResolver) ValidateConnection added in v1.1.0

func (c *CompositeConnectionResolver) ValidateConnection(correlationId string, connection *ConnectionParams) error

Validates connection parameters. This method can be overriden in child classes. - correlationId (optional) transaction id to trace execution through call chain. - connection connection parameters to be validated - returns error or nil if validation was successful

func (*CompositeConnectionResolver) ValidateCredential added in v1.1.0

func (c *CompositeConnectionResolver) ValidateCredential(correlationId string, credential *auth.CredentialParams) error

Validates credential parameters. This method can be overriden in child classes. - correlationId (optional) transaction id to trace execution through call chain. - credential credential parameters to be validated - returns error or nil if validation was successful

type ConnectionParams

type ConnectionParams struct {
	config.ConfigParams
}

Contains connection parameters to connect to external services. They are used together with credential parameters, but usually stored separately from more protected sensitive values.

Configuration parameters

discovery_key: key to retrieve parameters from discovery service
protocol: connection protocol like http, https, tcp, udp
host: host name or IP address
port: port number
uri: resource URI or connection string with all parameters in it

In addition to standard parameters ConnectionParams may contain any number of custom parameters

see ConfigParams

see CredentialParams

see ConnectionResolver

see IDiscovery

Example Example ConnectionParams object usage:

connection := NewConnectionParamsFromTuples(
    "protocol", "http",
    "host", "10.1.1.100",
    "port", "8080",
    "cluster", "mycluster"
);

host := connection.Host();                             // Result: "10.1.1.100"
port := connection.Port();                             // Result: 8080
cluster := connection.GetAsNullableString("cluster");     // Result: "mycluster"

func NewConnectionParams

func NewConnectionParams(values map[string]string) *ConnectionParams

Creates a new connection parameters and fills it with values. Parameters:

  • values map[string]string an object to be converted into key-value pairs to initialize this connection.

Returns *ConnectionParams

func NewConnectionParamsFromConfig

func NewConnectionParamsFromConfig(config *config.ConfigParams) *ConnectionParams

Retrieves a single ConnectionParams from configuration parameters from "connection" section. If "connections" section is present instead, then is returns only the first connection element. Parameters:

  • config *config.ConfigParams ConnectionParams, containing a section named "connection(s)".

Returns *ConnectionParams the generated ConnectionParams object.

func NewConnectionParamsFromMaps

func NewConnectionParamsFromMaps(maps ...map[string]string) *ConnectionParams

Static method for creating a StringValueMap using the maps passed as parameters. Parameters:

  • maps ...map[string]string the maps passed to this method to create a StringValueMap with.

Returns ConnectionParams the ConnectionParams created.

func NewConnectionParamsFromString

func NewConnectionParamsFromString(line string) *ConnectionParams

Creates a new ConnectionParams object filled with key-value pairs serialized as a string. Parameters:

  • line string a string with serialized key-value pairs as "key1=value1;key2=value2;..." Example: "Key1=123;Key2=ABC;Key3=2016-09-16T00:00:00.00Z"

Returns *ConnectionParams a new ConnectionParams object.

func NewConnectionParamsFromTuples

func NewConnectionParamsFromTuples(tuples ...interface{}) *ConnectionParams

Creates a new ConnectionParams object filled with provided key-value pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs. Parameters:

  • tuples ...interface{} the tuples to fill a new ConnectionParams object.

Returns *ConnectionParams a new ConnectionParams object.

func NewConnectionParamsFromTuplesArray

func NewConnectionParamsFromTuplesArray(tuples []interface{}) *ConnectionParams

Method for creating a StringValueMap from an array of tuples. Parameters:

  • tuples []interface{} the key-value tuples array to initialize the new StringValueMap with.

Returns *ConnectionParams the ConnectionParams created and filled by the 'tuples' array provided.

func NewConnectionParamsFromValue

func NewConnectionParamsFromValue(value interface{}) *ConnectionParams

Method that creates a ConfigParams object based on the values that are stored in the 'value' object's properties. see RecursiveObjectReader.getProperties Parameters:

  • value interface{} configuration parameters in the form of an object with properties.

Returns ConnectionParams generated ConnectionParams.

func NewEmptyConnectionParams

func NewEmptyConnectionParams() *ConnectionParams

Creates a new connection parameters and fills it with values. Returns *ConnectionParams

func NewManyConnectionParamsFromConfig

func NewManyConnectionParamsFromConfig(config *config.ConfigParams) []*ConnectionParams

Retrieves all ConnectionParams from configuration parameters from "connections" section. If "connection" section is present instead, than it returns a list with only one ConnectionParams. Parameters:

  • config *config.ConfigParams a configuration parameters to retrieve connections

Returns []*ConnectionParams a list of retrieved ConnectionParams

func (*ConnectionParams) DiscoveryKey

func (c *ConnectionParams) DiscoveryKey() string

Gets the key to retrieve this connection from DiscoveryService. If this key is null, than all parameters are already present. see UseDiscovery Returns string the discovery key to retrieve connection.

func (*ConnectionParams) Host

func (c *ConnectionParams) Host() string

Gets the host name or IP address. Returns string the host name or IP address.

func (*ConnectionParams) Port

func (c *ConnectionParams) Port() int

Gets the port number. Returns int the port number.

func (*ConnectionParams) PortWithDefault

func (c *ConnectionParams) PortWithDefault(defaultValue int) int

Gets the port number. Parameters:

  • defaultValue int default port number

Returns int the port number.

func (*ConnectionParams) Protocol

func (c *ConnectionParams) Protocol() string

Gets the connection protocol. Returns string the connection protocol or the default value if it's not set.

func (*ConnectionParams) ProtocolWithDefault

func (c *ConnectionParams) ProtocolWithDefault(defaultValue string) string

Gets the connection protocol. Parameters:

  • defaultValue string the default protocol

Returns string the connection protocol or the default value if it's not set.

func (*ConnectionParams) SetDiscoveryKey

func (c *ConnectionParams) SetDiscoveryKey(value string)

Sets the key to retrieve these parameters from DiscoveryService. Parameters:

  • value string a new key to retrieve connection.

func (*ConnectionParams) SetHost

func (c *ConnectionParams) SetHost(value string)

Sets the host name or IP address. Parameters:

  • value string a new host name or IP address.

func (*ConnectionParams) SetPort

func (c *ConnectionParams) SetPort(value int)

Sets the port number. see Host Parameters:

  • value int a new port number.

func (*ConnectionParams) SetProtocol

func (c *ConnectionParams) SetProtocol(value string)

Sets the connection protocol. Parameters:

  • value string a new connection protocol.

func (*ConnectionParams) SetUri

func (c *ConnectionParams) SetUri(value string)

Sets the resource URI or connection string. Parameters:

  • value string a new resource URI or connection string.

func (*ConnectionParams) Uri

func (c *ConnectionParams) Uri() string

Gets the resource URI or connection string. Usually it includes all connection parameters in it. Returns string the resource URI or connection string.

func (*ConnectionParams) UseDiscovery

func (c *ConnectionParams) UseDiscovery() bool

Checks if these connection parameters shall be retrieved from DiscoveryService. The connection parameters are redirected to DiscoveryService when discovery_key parameter is set. Returns bool true if connection shall be retrieved from DiscoveryService

type ConnectionResolver

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

Helper class to retrieve component connections.

If connections are configured to be retrieved from IDiscovery, it automatically locates IDiscovery in component references and retrieve connections from there using discovery_key parameter.

Configuration parameters

connection:

  discovery_key: (optional) a key to retrieve the connection from IDiscovery
    ... other connection parameters
  connections: alternative to connection

    [connection params 1]: first connection parameters
    ... connection parameters for key 1
    [connection params N]: Nth connection parameters
    ... connection parameters for key N

References *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connections see ConnectionParams

see IDiscovery

Example

config = NewConfigParamsFromTuples(
    "connection.host", "10.1.1.100",
    "connection.port", 8080
);

connectionResolver := NewConnectionResolver();
connectionResolver.Configure(config);
connectionResolver.SetReferences(references);

res, err := connectionResolver.Resolve("123");

func NewConnectionResolver

func NewConnectionResolver(config *config.ConfigParams,
	references refer.IReferences) *ConnectionResolver

Creates a new instance of connection resolver. Parameters:

  • config *config.ConfigParams component configuration parameters
  • references refer.IReferences component references

Returns *ConnectionResolver

func NewEmptyConnectionResolver

func NewEmptyConnectionResolver() *ConnectionResolver

Creates a new instance of connection resolver. Returns *ConnectionResolver

func (*ConnectionResolver) Add

func (c *ConnectionResolver) Add(connection *ConnectionParams)

Adds a new connection to component connections Parameters:

  • connection *ConnectionParams new connection parameters to be added

func (*ConnectionResolver) Configure

func (c *ConnectionResolver) Configure(config *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *config.ConfigParams configuration parameters to be set.

func (*ConnectionResolver) GetAll

func (c *ConnectionResolver) GetAll() []*ConnectionParams

Gets all connections configured in component configuration. Redirect to Discovery services is not done at this point. If you need fully fleshed connection use resolve method instead. Returns []*ConnectionParams a list with connection parameters

func (*ConnectionResolver) Register

func (c *ConnectionResolver) Register(correlationId string, connection *ConnectionParams) error

Registers the given connection in all referenced discovery services. This method can be used for dynamic service discovery. see IDiscovery Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • connection *ConnectionParams a connection to register.

Returns error

func (*ConnectionResolver) Resolve

func (c *ConnectionResolver) Resolve(correlationId string) (*ConnectionParams, error)

Resolves a single component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there. see IDiscovery Parameters:

  • correlationId: string transaction id to trace execution through call chain.

Returns *ConnectionParams, error resolved connection or error.

func (*ConnectionResolver) ResolveAll

func (c *ConnectionResolver) ResolveAll(correlationId string) ([]*ConnectionParams, error)

Resolves all component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there. see IDiscovery Parameters:

  • correlationId string transaction id to trace execution through call chain.

Returns []*ConnectionParams, error resolved connections or error.

func (*ConnectionResolver) SetReferences

func (c *ConnectionResolver) SetReferences(references refer.IReferences)

Sets references to dependent components. Parameters:

  • references refer.IReferences references to locate the component dependencies.

type ICompositeConnectionResolverOverrides added in v1.1.0

type ICompositeConnectionResolverOverrides interface {
	ValidateConnection(correlationId string, connection *ConnectionParams) error

	ValidateCredential(correlationId string, credential *auth.CredentialParams) error

	ComposeOptions(connections []*ConnectionParams, credential *auth.CredentialParams, parameters *config.ConfigParams) *config.ConfigParams

	MergeConnection(options *config.ConfigParams, connection *ConnectionParams) *config.ConfigParams

	MergeCredential(options *config.ConfigParams, credential *auth.CredentialParams) *config.ConfigParams

	MergeOptional(options *config.ConfigParams, parameters *config.ConfigParams) *config.ConfigParams

	FinalizeOptions(options *config.ConfigParams) *config.ConfigParams
}

type IDiscovery

type IDiscovery interface {
	// Registers connection parameters into the discovery service.
	Register(correlationId string, key string,
		connection *ConnectionParams) (result *ConnectionParams, err error)
	// Resolves a single connection parameters by its key.
	ResolveOne(correlationId string, key string) (result *ConnectionParams, err error)
	// Resolves all connection parameters by their key.
	ResolveAll(correlationId string, key string) (result []*ConnectionParams, err error)
}

Interface for discovery services which are used to store and resolve connection parameters to connect to external services.

type MemoryDiscovery

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

Discovery service that keeps connections in memory.

Configuration parameters

[connection key 1]:
... connection parameters for key 1
[connection key 2]:
... connection parameters for key N

see IDiscovery

see ConnectionParams

Example

config := NewConfigParamsFromTuples(
    "key1.host", "10.1.1.100",
    "key1.port", "8080",
    "key2.host", "10.1.1.100",
    "key2.port", "8082"
);

discovery := NewMemoryDiscovery();
discovery.ReadConnections(config);

discovery.Resolve("123", "key1", (err, connection) => {
    // Result: host=10.1.1.100;port=8080
});

func NewEmptyMemoryDiscovery

func NewEmptyMemoryDiscovery() *MemoryDiscovery

Creates a new instance of discovery service. Returns *MemoryDiscovery

func NewMemoryDiscovery

func NewMemoryDiscovery(config *config.ConfigParams) *MemoryDiscovery

Creates a new instance of discovery service. Parameters:

  • config *config.ConfigParams configuration with connection parameters.

Returns *MemoryDiscovery

func (*MemoryDiscovery) Configure

func (c *MemoryDiscovery) Configure(config *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *config.ConfigParams

configuration parameters to be set.

func (*MemoryDiscovery) ReadConnections

func (c *MemoryDiscovery) ReadConnections(config *config.ConfigParams)

Reads connections from configuration parameters. Each section represents an individual Connectionparams Parameters:

  • config *configure.ConfigParams

configuration parameters to be read

func (*MemoryDiscovery) Register

func (c *MemoryDiscovery) Register(correlationId string, key string,
	connection *ConnectionParams) (result *ConnectionParams, err error)

Registers connection parameters into the discovery service. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • key string a key to uniquely identify the connection parameters.
  • connection *ConnectionParams

Returns *ConnectionParams, error registered connection or error.

func (*MemoryDiscovery) ResolveAll

func (c *MemoryDiscovery) ResolveAll(correlationId string,
	key string) (result []*ConnectionParams, err error)

func (*MemoryDiscovery) ResolveOne

func (c *MemoryDiscovery) ResolveOne(correlationId string,
	key string) (result *ConnectionParams, err error)

Resolves a single connection parameters by its key. Parameters:

  • correlationId: string transaction id to trace execution through call chain.
  • key: string a key to uniquely identify the connection.

Returns *ConnectionParams, error receives found connection or error.

type TConnectionUtils added in v1.1.0

type TConnectionUtils struct{}

*

  • A set of utility functions to process connection parameters

func (*TConnectionUtils) ComposeUri added in v1.1.0

func (c *TConnectionUtils) ComposeUri(options *config.ConfigParams, defaultProtocol string, defaultPort int) string

*

  • Composes URI from config parameters.
  • The result URI will be in the following form:
  • protocol://username@password@host1:port1,host2:port2,...?param1=abc&param2=xyz&... *
  • @param options configuration parameters
  • @param defaultProtocol a default protocol
  • @param defaultPort a default port
  • @returns a composed URI

func (*TConnectionUtils) Concat added in v1.1.0

func (c *TConnectionUtils) Concat(options1 *config.ConfigParams, options2 *config.ConfigParams, keys ...string) *config.ConfigParams

*

  • Concatinates two options by combining duplicated properties into comma-separated list
  • @param options1 first options to merge
  • @param options2 second options to merge
  • @param keys when define it limits only to specific keys

func (*TConnectionUtils) Exclude added in v1.1.0

func (c *TConnectionUtils) Exclude(options *config.ConfigParams, keys ...string) *config.ConfigParams

*

  • Excludes specified keys from the config parameters.
  • @param options configuration parameters to be processed.
  • @param keys a list of keys to be excluded.
  • @returns a processed config parameters.

func (*TConnectionUtils) Include added in v1.1.0

func (c *TConnectionUtils) Include(options *config.ConfigParams, keys ...string) *config.ConfigParams

*

  • Includes specified keys from the config parameters.
  • @param options configuration parameters to be processed.
  • @param keys a list of keys to be included.
  • @returns a processed config parameters.

func (*TConnectionUtils) ParseUri added in v1.1.0

func (c *TConnectionUtils) ParseUri(uri string, defaultProtocol string, defaultPort int) *config.ConfigParams

*

  • Parses URI into config parameters.
  • The URI shall be in the following form:
  • protocol://username@password@host1:port1,host2:port2,...?param1=abc&param2=xyz&... *
  • @param uri the URI to be parsed
  • @param defaultProtocol a default protocol
  • @param defaultPort a default port
  • @returns a configuration parameters with URI elements

Jump to

Keyboard shortcuts

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