Microservice Networking Interface
Relevant source files
The following files were used as context for generating this wiki page:
The Microservice Networking Interface provides a standardized communication layer for services within the Worldbase network. It utilizes NATS as the underlying transport mechanism and enforces a specific addressing convention to ensure unique service identification and efficient routing.
Sources: proto/worldengine/micro/v1/service.proto:7-9, pkg/micro/client.go:17-18
Service Addressing Convention
The networking interface uses a hierarchical addressing scheme that maps directly to NATS subjects. Every service instance is identified by a ServiceAddress composed of five distinct segments.
Address Structure
A complete service address follows this format:
<region>.<realm>.<organization>.<project>.<service_id>.<endpoint>
The individual fields are subject to the following validation rules:
- All fields must contain only lowercase alphanumeric characters, hyphens, or underscores.
- The
endpointsegment is the only part allowed to contain periods (.) for NATS routing.
Sources: proto/worldengine/micro/v1/service.proto:11-30, pkg/micro/service_address.go:10-23
Realm Scopes
The Realm enum defines the access scope of a service instance.
| Realm | Value | Description |
|---|---|---|
REALM_INTERNAL | 1 | Reserved for services used only within the internal infrastructure. |
REALM_WORLD | 2 | Publicly accessible services that form part of the Worldbase network. |
Sources: proto/worldengine/micro/v1/service.proto:44-55, pkg/micro/service_address.go:30-35
Implementation Logic
The micro package provides utilities to manipulate these addresses:
String(): Generates the base NATS subject (excluding endpoint).Endpoint(): Joins the base address with a specific functional endpoint.ParseAddress(): Validates and converts a string into aServiceAddressstruct.
Sources: pkg/micro/service_address.go:48-93
Messaging Protocol
All communication utilizes Protobuf messages to ensure cross-language compatibility and type safety.
Request and Response Models
The system defines two generic containers for microservice operations.
This diagram shows the relationship between generic message types and the addressing system.
- Request: Carries an optional unique ID, the target address, and a protobuf-encoded payload.
- Response: Returns the status of the operation using standard Google RPC status codes.
Sources: proto/worldengine/micro/v1/response.proto:12-34, proto/gen/go/worldengine/micro/v1/response.pb.go:73-82
Networking Client
The micro.Client manages NATS connections and provides high-level abstractions for common messaging patterns.
Client Configuration
The client is configured via environment variables or functional options.
| Option | Environment Variable | Default Value | Description |
|---|---|---|---|
Name | NATS_NAME | isc | Identifier for the client connection. |
URL | NATS_URL | nats://nats:4222 | The address of the NATS server. |
CredentialsFile | NATS_CREDENTIALS_FILE | None | Path to NATS security credentials. |
Sources: pkg/micro/client.go:22-31
Connection Management
The client implements an automatic reconnection strategy with exponential backoff. It ramps up from 500ms to 20 seconds over 44 steps.
Sources: pkg/micro/client.go:193-216
Messaging Patterns
1. Request-Reply
The Request() method performs a synchronous call. It encodes the payload, sends it to the target subject, and waits for a response. If the response status.Code is non-zero, the client returns an error containing the status message.
Sources: pkg/micro/client.go:88-124
2. Request and Subscribe
This pattern handles cases where a response is emitted on a different subject, such as when a command triggers an event.
The sequence ensures the subscription is active before the request is sent to prevent race conditions.
Sources: pkg/micro/client.go:132-161, pkg/micro/client_test.go:106-136
Summary
The Microservice Networking Interface standardizes service interactions through a strictly defined ServiceAddress and a robust micro.Client. By enforcing naming conventions and utilizing Protobuf-wrapped NATS messages, it provides reliable, scope-aware communication across the World Engine infrastructure.
Sources: pkg/micro/client.go:17-18, proto/worldengine/micro/v1/service.proto:11-30