Documentation
¶
Index ¶
Constants ¶
const ( RealmUnspecified = microv1.ServiceAddress_REALM_UNSPECIFIED // Should not be used RealmInternal = microv1.ServiceAddress_REALM_INTERNAL // Reserved for internal services RealmWorld = microv1.ServiceAddress_REALM_WORLD // Publicly accessible services that are )
Variables ¶
var (
ErrEndpointAlreadyExists = eris.New("endpoint already exists")
)
var (
ErrInvalidAddress = eris.New("invalid service address format")
)
Functions ¶
func Endpoint ¶
func Endpoint(s *ServiceAddress, endpoint string) string
Endpoint returns the full service address including the endpoint.
func String ¶
func String(s *ServiceAddress) string
String returns the string representation of a ServiceAddress without the endpoint.
Types ¶
type Client ¶
Client represents a NATS client with enhanced logging and error handling.
func NewClient ¶
func NewClient(opts ...ClientOption) (*Client, error)
NewClient creates a new NATS client with the given configuration. It handles connection setup, error handling, and logging.
func (*Client) Close ¶
func (c *Client) Close()
Close gracefully closes the NATS connection and logs the event.
func (*Client) Request ¶
func (c *Client) Request( ctx context.Context, address *ServiceAddress, endpoint string, payload proto.Message, ) (*microv1.Response, error)
Request sends a request to a subject and waits for a response (request-reply pattern). The timeout should be set in ctx.
func (*Client) RequestAndSubscribe ¶ added in v0.6.0
func (c *Client) RequestAndSubscribe( ctx context.Context, sendAddress *ServiceAddress, sendEndpoint string, receiveAddress *ServiceAddress, receiveEndpoint string, payload proto.Message, ) (*nats.Msg, error)
RequestAndSubscribe sends a message to the send endpoint and waits for a message on the receive endpoint. This is useful when the response comes on a different subject than the request (e.g., events). The timeout should be set in ctx.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption defines a function that can modify a Client.
func WithLogger ¶
func WithLogger(log zerolog.Logger) ClientOption
WithLogger returns a ClientOption that sets the logger.
func WithNATSConfig ¶
func WithNATSConfig(cfg NATSConfig) ClientOption
WithNATSConfig returns a ClientOption that sets the NATS configuration.
type Handler ¶
Handler defines the signature for all service endpoint handlers. It guarantees that all responses follow the protobuf schema defined in response.proto.
type NATSConfig ¶
type NATSConfig struct {
Name string `env:"NATS_NAME" envDefault:"isc"`
URL string `env:"NATS_URL" envDefault:"nats://nats:4222"`
CredentialsFile string `env:"NATS_CREDENTIALS_FILE"`
}
NATSConfig holds the configuration for the NATS client.
func (NATSConfig) Validate ¶
func (cfg NATSConfig) Validate() error
Validate validates the NATS configuration and returns an error if invalid.
type Realm ¶
type Realm = microv1.ServiceAddress_Realm
Realm represents the access scope of the service.
type Request ¶
type Request struct {
// Raw is the original NATS message.
Raw *nats.Msg
// RequestID is extracted from the request if available.
RequestID string
// ServiceAddress contains the service address information.
ServiceAddress *microv1.ServiceAddress
// Payload contains the decoded request payload, if any.
Payload *anypb.Any
}
Request represents an incoming service request with its metadata.
func NewRequestFromNATSMsg ¶ added in v0.7.0
NewRequestFromNATSMsg converts a nats.Msg to a Request, parsing the payload if present.
type Response ¶
type Response struct {
// RequestID is copied from the request.
RequestID string
// ServiceAddress contains the service address information.
ServiceAddress *microv1.ServiceAddress
// Status contains the response status information.
Status *spb.Status
// Payload contains the response payload, if any.
Payload *anypb.Any
}
Response represents a structured response that follows the protobuf schema.
func NewErrorResponse ¶
NewErrorResponse creates an error response with the given error. The code parameter must not be codes.OK, as this function is only for error responses.
func NewSuccessResponse ¶
NewSuccessResponse creates a successful response with optional payload.
type Service ¶
type Service struct {
Address *ServiceAddress // This service's address
Version string // Version represents the service version.
// contains filtered or unexported fields
}
Service represents a micro service that can serve requests.
func NewService ¶
func NewService(client *Client, address *ServiceAddress, tel *telemetry.Telemetry) (*Service, error)
NewService creates a new service with the given NATS client, service address, and telemetry.
func (*Service) AddEndpoint ¶
AddEndpoint adds an endpoint to the service. The endpoint will be registered under the service's address as a prefix.
Example:
svc.AddEndpoint("ping", handlePing) // -> "<service_address>.ping"
func (*Service) AddGroup ¶
func (s *Service) AddGroup(name string) *ServiceEndpointGroup
AddGroup returns a helper struct that allows registering a group of endpoints with a common prefix. For example, all endpoints in the "message" group will be registered as "message.<endpoint_name>".
Example:
messageGroup := svc.AddGroup("message")
messageGroup.AddEndpoint("send", handleMessageSend) // -> "<service_address>.message.send"
messageGroup.AddEndpoint("delete", handleMessageDelete) // -> "<service_address>.message.delete"
messageGroup.AddEndpoint("update", handleMessageUpdate) // -> "<service_address>.message.update"
type ServiceAddress ¶
type ServiceAddress = microv1.ServiceAddress
ServiceAddress is an alias to the protobuf-generated ServiceAddress type.
func GetAddress ¶
func GetAddress(region string, realm Realm, organization, project string, serviceID ServiceID) *ServiceAddress
GetAddress creates a new ServiceAddress with the given realm, organization, project, and service ID.
func ParseAddress ¶
func ParseAddress(address string) (*ServiceAddress, error)
ParseAddress parses a string into a ServiceAddress. The format should be "<region>.<realm>.<organization>.<project>.<service_id>".
type ServiceEndpointGroup ¶
type ServiceEndpointGroup struct {
// contains filtered or unexported fields
}
ServiceEndpointGroup is a helper struct that allows registering a group of endpoints with a common prefix.
func (*ServiceEndpointGroup) AddEndpoint ¶
func (g *ServiceEndpointGroup) AddEndpoint(name string, handler Handler) error