services

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandableGrpcService

type CommandableGrpcService struct {
	*GrpcService
	// contains filtered or unexported fields
}
type MyCommandableGrpcService struct {
	*CommandableGrpcService
}
func NewCommandableGrpcService() *CommandableGrpcService {
	c := DummyCommandableGrpcService{}
	c.CommandableGrpcService = grpcservices.NewCommandableGrpcService("myservice")
	c.DependencyResolver.Put("controller", cref.NewDescriptor("mygroup", "controller", "default", "*", "*"))
	return &c
}

service := NewMyCommandableGrpcService(); service.Configure(ctx, cconf.NewConfigParamsFromTuples(

"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", "8080",

)); service.SetReferences(ctx, cref.NewReferencesFromTuples(

cref.NewDescriptor("mygroup","controller","default","default","1.0"), controller

));

opnErr := service.Open(ctx, "123")

if opnErr == nil {
	fmt.Println("The GRPC service is running on port 8080");
}

func InheritCommandableGrpcService

func InheritCommandableGrpcService(overrides IGrpcServiceOverrides, name string) *CommandableGrpcService

NewCommandableGrpcService method are creates a new instance of the service.

  • name a service name.

func (*CommandableGrpcService) Register

func (c *CommandableGrpcService) Register()

Register method are registers all service command in gRPC endpoint.

type GrpcEndpoint

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

GrpcEndpoint used for creating GRPC endpoints. An endpoint is a URL, at which a given service can be accessed by a client.

Configuration parameters:

Parameters to pass to the configure method for component configuration:

	Configuration parameters

	- connection(s) - the connection resolver"s connections:
		- discovery_key - the key to use for connection resolving in a discovery service;
		- protocol - the connection"s protocol
		- host - the target host;
		- port - the target port;
		- uri - the target URI.
	- credential(s) - the HTTPS credentials:
		- ssl_key_file - the SSL private key in PEM
		- ssl_crt_file - the SSL certificate in PEM
		- ssl_ca_file - the certificate authorities (root cerfiticates) in PEM

	References:

		- logger: "*:logger:*:*:1.0";
		- counters: "*:counters:*:*:1.0";
		- discovery: "*:discovery:*:*:1.0" (for the connection resolver).

	Examples:

   func (c* Endpoint) MyMethod(ctx context.Context, config ConfigParams, references IReferences) {
       endpoint := NewGrpcEndpoint();
       if c.config != nil {
           endpoint.Configure(ctx, c._config);
       }
       if c.references != nil {
           endpoint.SetReferences(ctx, c.references);
       }
       ...

       err := c.endpoint.Open(ctx, correlationId)
       if err != nil {
           // error ocured
           return err
       }
       c.Opened = true
       return nil
       ...
   }

func NewGrpcEndpoint

func NewGrpcEndpoint() *GrpcEndpoint

NewGrpcEndpoint method are creates new instance of GrpcEndpoint

func (*GrpcEndpoint) AddInterceptors

func (c *GrpcEndpoint) AddInterceptors(interceptors ...grpc.ServerOption)

AddInterceptors method are registers a middleware for methods in GRPC endpoint. See https://github.com/grpc/grpc-go/tree/master/examples/features/interceptor Parameters:

  • interceptors ...grpc.ServerOption

interceptor functions (Stream or Unary use grpc.UnaryInterceptor() or grpc.StreamInterceptor() for inflate in grpc.ServerOption)

func (*GrpcEndpoint) Close

func (c *GrpcEndpoint) Close(ctx context.Context, correlationId string) (err error)

Close methods are closes c endpoint and the GRPC server (service) that was opened earlier.

Parameters:
	- ctx context.Context	operation context
	- correlationId     (optional) transaction id to trace execution through call chain.

Returns: an error if one is raised.

func (*GrpcEndpoint) Configure

func (c *GrpcEndpoint) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure method are configures c HttpEndpoint using the given configuration parameters. Configuration parameters:

  • connection(s) - the connection resolver"s connections;
  • "connection.discovery_key" - the key to use for connection resolving in a discovery service;
  • "connection.protocol" - the connection"s protocol;
  • "connection.host" - the target host;
  • "connection.port" - the target port;
  • "connection.uri" - the target URI.
  • "credential.ssl_key_file" - SSL private key in PEM
  • "credential.ssl_crt_file" - SSL certificate in PEM
  • "credential.ssl_ca_file" - Certificate authority (root certificate) in PEM Parameters:
  • ctx context.Context operation context
  • config configuration parameters, containing a "connection(s)" section.

See ConfigParams (in the PipServices "Commons" package)

func (*GrpcEndpoint) GetServer

func (c *GrpcEndpoint) GetServer() *grpc.Server

GetServer return working gRPC server for register services Note: this server is async working in goroutione, wrap into locks if you want change this variable Returns *grpc.Server

func (*GrpcEndpoint) IsOpen

func (c *GrpcEndpoint) IsOpen() bool

IsOpen method are return whether or not c endpoint is open with an actively listening GRPC server.

func (*GrpcEndpoint) Open

func (c *GrpcEndpoint) Open(ctx context.Context, correlationId string) (err error)

Open method are opens a connection using the parameters resolved by the referenced connection resolver and creates a GRPC server (service) using the set options and parameters.

Parameters:
	- ctx context.Context	operation context
	- correlationId     (optional) transaction id to trace execution through call chain.

Retunrns: an error if one is raised.

func (*GrpcEndpoint) Register

func (c *GrpcEndpoint) Register(registration IRegisterable)

Register method are registers a registerable object for dynamic endpoint discovery.

  • registration the registration to add.

See IRegisterable

func (*GrpcEndpoint) RegisterCommandableMethod

func (c *GrpcEndpoint) RegisterCommandableMethod(method string, schema *cvalid.Schema,
	action func(ctx context.Context, correlationId string, args *crun.Parameters) (result any, err error))

RegisterCommandableMethod method are registers a commandable method in c objects GRPC server (service) by the given name.

Parameters:
	- ctx context.Context	operation context
	- method        the GRPC method name.
	- schema        the schema to use for parameter validation.
	- action        the action to perform at the given route.

func (*GrpcEndpoint) RegisterService

func (c *GrpcEndpoint) RegisterService(sd *grpc.ServiceDesc, implementation any)

RegisterService method are registers a service with related implementation

  • implementation the service implementation method Invoke.

func (*GrpcEndpoint) SetReferences

func (c *GrpcEndpoint) SetReferences(ctx context.Context, references cref.IReferences)

SetReferences method are sets references to c endpoint"s logger, counters, and connection resolver. References:

  • logger: "*:logger:*:*:1.0"
  • counters: "*:counters:*:*:1.0"
  • discovery: "*:discovery:*:*:1.0" (for the connection resolver) Parameters:
  • ctx context.Context operation context
  • references an IReferences object, containing references to a logger, counters, and a connection resolver.

See IReferences (in the PipServices "Commons" package)

func (*GrpcEndpoint) Unregister

func (c *GrpcEndpoint) Unregister(registration IRegisterable)

Unregister mwthod are unregisters a registerable object, so that it is no longer used in dynamic endpoint discovery.

  • registration the registration to remove.

See IRegisterable

type GrpcService

type GrpcService struct {
	Overrides IGrpcServiceOverrides

	//  The GRPC endpoint that exposes c service.
	Endpoint *GrpcEndpoint
	//  The dependency resolver.
	DependencyResolver *cref.DependencyResolver
	//  The logger.
	Logger *clog.CompositeLogger
	//  The performance counters.
	Counters *ccount.CompositeCounters
	// The tracer.
	Tracer *ctrace.CompositeTracer
	// contains filtered or unexported fields
}

GrpcService abstract service that receives remove calls via GRPC protocol.

Configuration parameters:

- dependencies:
	- endpoint:              override for GRPC Endpoint dependency
	- controller:            override for Controller dependency
- connection(s):
	- discovery_key:         (optional) a key to retrieve the connection from connect.idiscovery.html IDiscovery
	- protocol:              connection protocol: http or https
	- host:                  host name or IP address
	- port:                  port number
	- uri:                   resource URI or connection string with all parameters in it
- credential - the HTTPS credentials:
	- ssl_key_file:         the SSL private key in PEM
	- ssl_crt_file:         the SSL certificate in PEM
	- ssl_ca_file:          the certificate authorities (root cerfiticates) in PEM

References:

	- *:logger:*:*:1.0               (optional) ILogger components to pass log messages
	- *:counters:*:*:1.0             (optional) ICounters components to pass collected measurements
	- *:discovery:*:*:1.0            (optional) IDiscovery services to resolve connection
	- *:endpoint:grpc:*:1.0           (optional) GrpcEndpoint reference

See GrpcClient

Example:

type MyGrpcService struct{
   *GrpcService
   controller IMyController;
}
...

func NewMyGrpcService() *MyGrpcService {
	c := NewMyGrpcService{}
	c.GrpcService = grpcservices.NewGrpcService("Mydata.Mydatas")
	c.GrpcService.IRegisterable = &c
	c.numberOfCalls = 0
	c.DependencyResolver.Put(context.Context(), "controller", cref.NewDescriptor("mygroup", "controller", "*", "*", "*"))
	return &c
}

func (c*MyGrpcService) SetReferences(ctx context.Context, references: IReferences) {
	c.GrpcService.SetReferences(references);
	resolv, err := c.DependencyResolver.GetOneRequired("controller")
	if err == nil && resolv != nil {
	    c.controller = resolv.(grpctest.IMyController)
	    return
	}
	panic("Can't resolve 'controller' reference")
}

func (c*MyGrpcService) Register() {
	protos.RegisterMyDataServer(c.Endpoint.GetServer(), c)
	...
}

service := NewMyGrpcService();
service.Configure(ctx, cconf.NewConfigParamsFromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080,
));
service.SetReferences(ctx, cref.NewReferencesFromTuples(
   cref.NewDescriptor("mygroup","controller","default","default","1.0"), controller
));

err := service.Open(ctx, "123")
if  err == nil {
   fmt.Println("The GRPC service is running on port 8080");
}

func InheritGrpcService

func InheritGrpcService(overrides IGrpcServiceOverrides, serviceName string) *GrpcService

InheritGrpcService methods are creates new instance NewGrpcService Parameters:

  • overrides a reference to child class that overrides virtual methods
  • serviceName string

service name from XYZ.pb.go, set "" for use default gRPC commandable protobuf Return *GrpcService

func (*GrpcService) Close

func (c *GrpcService) Close(ctx context.Context, correlationId string) (err error)

Close method are closes component and frees used resources.

Parameters:
	- ctx context.Context	operation context
	- correlationId 	(optional) transaction id to trace execution through call chain.

Returns: error or nil no errors occured.

func (*GrpcService) Configure

func (c *GrpcService) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure method are configures component by passing configuration parameters.

Parameters:
	- ctx context.Context	operation context
	- config   configuration parameters to be set.

func (*GrpcService) Instrument

func (c *GrpcService) Instrument(ctx context.Context, correlationId string, name string) *rpcserv.InstrumentTiming

Instrument method are adds instrumentation to log calls and measure call time. It returns a Timing object that is used to end the time measurement.

Parameters:
	- ctx context.Context	operation context
	- correlationId     (optional) transaction id to trace execution through call chain.
	- name              a method name.

Return Timing object to end the time measurement.

func (*GrpcService) IsOpen

func (c *GrpcService) IsOpen() bool

IsOpen method are checks if the component is opened. Return true if the component has been opened and false otherwise.

func (*GrpcService) Open

func (c *GrpcService) Open(ctx context.Context, correlationId string) (err error)

Open method are opens the component.

Parameters:
	- ctx context.Context	operation context
	- correlationId 	(optional) transaction id to trace execution through call chain.

Returns: error or nil no errors occured.

func (*GrpcService) Register

func (c *GrpcService) Register()

Register method are registers all service routes in HTTP endpoint.

func (*GrpcService) RegisterCommandableMethod

func (c *GrpcService) RegisterCommandableMethod(method string, schema *cvalid.Schema,
	action func(ctx context.Context, correlationId string, data *crun.Parameters) (result any, err error))

RegisterCommandableMethod method are registers a commandable method in c objects GRPC server (service) by the given name.,

Parameters:
	- ctx context.Context	operation context
	- method        the GRPC method name.
	- schema        the schema to use for parameter validation.
	- action        the action to perform at the given route.

func (*GrpcService) RegisterUnaryInterceptor

func (c *GrpcService) RegisterUnaryInterceptor(action func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error))

Registers a middleware for methods in GRPC endpoint.

Parameters:
	- action        an action function that is called when middleware is invoked.

func (*GrpcService) SetReferences

func (c *GrpcService) SetReferences(ctx context.Context, references cref.IReferences)

SetReferences method are sets references to dependent components.

Parameters:
	- ctx context.Context	operation context
	- references 	references to locate the component dependencies.

func (*GrpcService) UnsetReferences

func (c *GrpcService) UnsetReferences()

UnsetReferences method are unsets (clears) previously set references to dependent components.

func (*GrpcService) ValidateRequest

func (c *GrpcService) ValidateRequest(request any, schema *cvalid.Schema) error

type IGrpcServiceOverrides

type IGrpcServiceOverrides interface {
	Register()
}

type IRegisterable

type IRegisterable interface {
	// Perform required registration steps.
	Register()
}

IRegisterable is interface to perform on-demand registrations.

type InvokeComandMediator

type InvokeComandMediator struct {
	InvokeFunc func(ctx context.Context, request *grpcproto.InvokeRequest) (response *grpcproto.InvokeReply, err error)
	grpcproto.CommandableServer
}

InvokeComandMediator Helper class for implements invoke method in CommandableGrpc

func (*InvokeComandMediator) Invoke

func (c *InvokeComandMediator) Invoke(ctx context.Context, request *grpcproto.InvokeRequest) (response *grpcproto.InvokeReply, err error)

Jump to

Keyboard shortcuts

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