registry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package registry provides service discovery and registration capabilities. It supports both local (in-memory) and distributed (Redis) backends for dynamic service discovery with TTL-based health checking and automatic heartbeats.

Example usage:

// Create local registry for development
registry := registry.NewLocalRegistry()

// Register a service
info := registry.ServiceInfo{
    Name:           "api-service",
    Version:        "1.0.0",
    Address:        "192.168.1.10",
    Port:           8080,
    HealthEndpoint: "/health/ready",
}
if err := registry.Register(ctx, info); err != nil {
    log.Fatal(err)
}

// Discover services
services, err := registry.Discover(ctx, "api-service")
if err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LocalRegistry

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

LocalRegistry implements Registry using an in-memory sync.Map. It's suitable for development, testing, and single-instance deployments. For distributed service discovery, use RedisRegistry instead.

func NewLocalRegistry

func NewLocalRegistry() *LocalRegistry

NewLocalRegistry creates a new in-memory registry.

func (*LocalRegistry) Close

func (r *LocalRegistry) Close() error

Close closes the local registry (no-op for local registry).

func (*LocalRegistry) Deregister

func (r *LocalRegistry) Deregister(ctx context.Context, serviceID string) error

Deregister removes a service instance from the local registry.

func (*LocalRegistry) Discover

func (r *LocalRegistry) Discover(ctx context.Context, serviceName string) ([]ServiceInfo, error)

Discover returns all registered instances of a service by name. Results are sorted by registration time (oldest first).

func (*LocalRegistry) Register

func (r *LocalRegistry) Register(ctx context.Context, service ServiceInfo) error

Register registers a service instance in the local registry. If the service ID is empty, a UUID will be generated.

type RedisRegistry

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

RedisRegistry implements Registry using Redis as the backend. It supports TTL-based health checking and automatic heartbeats to keep services alive in the registry.

func NewRedisRegistry

func NewRedisRegistry(ctx context.Context, cfg RedisRegistryConfig) (*RedisRegistry, error)

NewRedisRegistry creates a new Redis-backed registry with automatic heartbeats.

func (*RedisRegistry) Close

func (r *RedisRegistry) Close() error

Close stops all heartbeats and closes the Redis connection.

func (*RedisRegistry) Deregister

func (r *RedisRegistry) Deregister(ctx context.Context, serviceID string) error

Deregister removes a service instance from Redis and stops its heartbeat.

func (*RedisRegistry) Discover

func (r *RedisRegistry) Discover(ctx context.Context, serviceName string) ([]ServiceInfo, error)

Discover returns all healthy instances of a service by name. Only returns services that haven't expired (TTL still valid).

func (*RedisRegistry) Register

func (r *RedisRegistry) Register(ctx context.Context, service ServiceInfo) error

Register registers a service instance in Redis with TTL and starts a heartbeat goroutine.

type RedisRegistryConfig

type RedisRegistryConfig struct {
	// RedisAddr is the Redis server address (host:port).
	RedisAddr string

	// Password is the Redis password (optional).
	Password string

	// DB is the Redis database number.
	DB int

	// TTL is the time-to-live for service registrations.
	// Services must send heartbeats more frequently than this to stay registered.
	// Default: 30 seconds.
	TTL time.Duration

	// HeartbeatInterval is how often to send heartbeat updates.
	// Should be less than TTL to prevent expiration.
	// Default: 10 seconds.
	HeartbeatInterval time.Duration
}

RedisRegistryConfig configures the Redis registry.

type Registry

type Registry interface {
	// Register registers a service instance with the registry.
	// The service remains registered until explicitly deregistered or its TTL expires.
	// Returns an error if registration fails.
	Register(ctx context.Context, service ServiceInfo) error

	// Deregister removes a service instance from the registry.
	// If the service is not found, this is a no-op.
	// Returns an error if deregistration fails.
	Deregister(ctx context.Context, serviceID string) error

	// Discover returns all healthy instances of a service by name.
	// Results are sorted by registration time (oldest first).
	// Returns an empty slice if no instances are found.
	Discover(ctx context.Context, serviceName string) ([]ServiceInfo, error)

	// Close closes the registry and releases any resources.
	// After calling Close, the registry should not be used.
	Close() error
}

Registry provides service registration and discovery functionality. Implementations must be safe for concurrent access.

type RegistryType

type RegistryType string

RegistryType represents the type of registry backend.

const (
	// RegistryTypeLocal uses in-memory storage for development/testing.
	RegistryTypeLocal RegistryType = "local"

	// RegistryTypeRedis uses Redis for distributed service discovery.
	RegistryTypeRedis RegistryType = "redis"
)

type ServiceInfo

type ServiceInfo struct {
	// ID is a unique identifier for this service instance.
	// If empty during registration, one will be generated.
	ID string

	// Name is the service name (e.g., "api-service", "worker-service").
	// Multiple instances can share the same name.
	Name string

	// Version is the service version (e.g., "1.0.0", "v2.3.1").
	Version string

	// Address is the IP address or hostname where the service is reachable.
	Address string

	// Port is the primary port where the service listens.
	Port int

	// HealthEndpoint is the HTTP path for health checks (e.g., "/health/ready").
	// Optional but recommended for automatic health monitoring.
	HealthEndpoint string

	// Metadata contains additional key-value pairs for custom service information.
	// This can include tags, environment, region, etc.
	Metadata map[string]string

	// RegisteredAt is the timestamp when the service was registered.
	// This is set automatically during registration.
	RegisteredAt time.Time
}

ServiceInfo contains metadata about a registered service instance.

Jump to

Keyboard shortcuts

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