Documentation
¶
Index ¶
Constants ¶
const ( PermitWithoutStreamFieldName = "client.grpc.permit_without_stream" KeepaliveTimeFieldName = "client.grpc.keepalive.time" KeepaliveTimeoutFieldName = "client.grpc.keepalive.timeout" AddressFieldName = "client.grpc.address" PortFieldName = "client.grpc.port" PermitWithoutStreamDefault = true KeepaliveTimeDefault = time.Second * 10 KeepaliveTimeoutDefault = time.Second * 20 AddressDefault = "127.0.0.1" PortDefault = uint16(9090) )
Variables ¶
var Component = &component.Component{ Init: component.StepFunc(func(container container.Container) error { return container.Provides( NewConfig, NewClient, func(client *Client) grpc.ClientConnInterface { return client }, ) }), BindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error { return container.Invoke(func(config *Config) { flagSet.StringVar(&config.Address, AddressFieldName, AddressDefault, "") flagSet.Uint16Var(&config.Port, PortFieldName, PortDefault, "") flagSet.BoolVar(&config.PermitWithoutStream, PermitWithoutStreamFieldName, PermitWithoutStreamDefault, "") flagSet.DurationVar(&config.KeepaliveTime, KeepaliveTimeFieldName, KeepaliveTimeDefault, "") flagSet.DurationVar(&config.KeepaliveTimeout, KeepaliveTimeoutFieldName, KeepaliveTimeoutDefault, "") }) }), Configuration: component.StepFunc(func(container container.Container) error { return container.Invoke(Configuration) }), Stop: component.StepFunc(func(container container.Container) error { return container.Invoke(func(client *Client) error { return client.Close() }) }), }
Component is a ready-to-use Compogo component that provides a gRPC client. It automatically:
- Registers Config and Client in the DI container
- Adds command-line flags for client configuration
- Configures the client during Configuration phase
- Closes the connection during Stop phase
Usage:
compogo.WithComponents(
grpc_client.Component,
// ... your service components that need gRPC client
)
Then in your service component:
type UserService struct {
client *grpc_client.Client
}
func (s *UserService) GetUser(ctx context.Context, id int) (*User, error) {
var resp pb.GetUserResponse
err := s.client.Invoke(ctx, "/users.UserService/Get", &req, &resp)
// ...
}
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a gRPC client connection with Compogo lifecycle support. It provides a simplified interface for making RPC calls and handles connection lifecycle (creation, keepalive, cleanup).
func NewClient ¶
NewClient creates a new gRPC client connection with the given configuration. It sets up:
- Connection target (address:port)
- Keepalive parameters (ping interval, timeout, behavior)
- Insecure credentials (for trusted networks only)
For TLS support, wrap this component or use a service mesh.
func (*Client) NewStream ¶
func (client *Client) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)
type Config ¶
type Config struct {
PermitWithoutStream bool
Port uint16
Address string
KeepaliveTime time.Duration
KeepaliveTimeout time.Duration
}
func Configuration ¶
func Configuration(config *Config, configurator configurator.Configurator) *Config
Configuration applies configuration values to the Config struct. It reads from the provided configurator and sets defaults if values are not present. This function is designed to be used with container.Invoke in the Configuration phase.