GrpcService

Abstract service that receives remote calls via the GRPC protocol.

Description

The GrpcService class allows you to create services that receive remote calls via the GRPC protocol.

Configuration parameters

  • dependencies:
    • endpoint: override for GRPC Endpoint dependency
    • controller: override for Controller dependency
  • connection(s):
    • discovery_key: (optional) key to retrieve the connection from 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
  • credentials:
    • ssl_key_file: SSL private key in PEM
    • ssl_crt_file: SSL certificate in PEM
    • ssl_ca_file: certificate authorities (root cerfiticates) in PEM

References

  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurementsand as specified by the counter’s source.

Constructors

InheritGrpcService methods are creates new instance NewGrpcService

InheritGrpcService(overrides IGrpcServiceOverrides, serviceName string) *GrpcService

  • overrides: IGrpcServiceOverrides - a reference to child class that overrides virtual methods
  • serviceName: string - service name from XYZ.pb.go, set "" for use default gRPC commandable protobuf

Fields

Endpoint

The GRPC endpoint that exposes this service.

Endpoint: *GrpcEndpoint

DependencyResolver

The dependency resolver.

DependencyResolver: *DependencyResolver

Logger

The logger.

Logger: *CompositeLogger

Counters

The Counters counters.

Counters: *CompositeCounters

Tracer

The tracer.

Tracer: *CompositeTracer

Methods

Close

Closes the component and frees used resources.

(c *GrpcService) Close(correlationId string) (err error)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • returns: (err error) - error or nil no errors occured.

Configure

Configures component by passing configuration parameters.

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

  • ctx: context.Context - operation context.
  • config: *cconf.ConfigParams - configuration parameters to be set.

IsOpen

Checks if the component is open.

(c *GrpcService) IsOpen() bool

  • returns: bool -True if the endpoint is open with an actively listening GRPC server.

Instrument

Adds instrumentation to log calls and measures call time. It returns a CounterTiming object that is used to end the time measurement.

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

  • ctx: context.Context - operation context.
  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • name: string - method name.
  • returns: *rpcserv.InstrumentTiming -Timing object to end the time measurement.

Open

Opens the component.

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

  • ctx: context.Context - operation context.
  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • returns: (err error) - error or nil no errors occured.

Register

Register method are registers all service routes in HTTP endpoint.

(c *GrpcService) Register()

RegisterUnaryInterceptor

Registers a middleware for methods in GRPC endpoint.

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

  • action: func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) - an action function that is called when middleware is invoked.

RegisterCommandableMethod

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

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

  • method: string - the GRPC method name.
  • schema: *cvalid.Schema - validation schema to validate received parameters.
  • action: func(ctx context.Context, correlationId string, data *crun.Parameters) (result any, err error) - action function that is called when operation is invoked.

SetReferences

Sets references to dependent components.

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

  • ctx: context.Context - operation context.
  • references: cref.IReferences - references to locate the component dependencies.

UnsetReferences

Unsets (clears) previously set references to dependent components.

(c *GrpcService) UnsetReferences()

Examples

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");
}