backends

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Name returns the backend name
	Name() string

	// Initialize initializes the backend
	Initialize(ctx context.Context) error

	// Register registers a service instance
	Register(ctx context.Context, instance *ServiceInstance) error

	// Deregister deregisters a service instance
	Deregister(ctx context.Context, serviceID string) error

	// Discover discovers service instances by name
	Discover(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

	// DiscoverWithTags discovers service instances by name and tags
	DiscoverWithTags(ctx context.Context, serviceName string, tags []string) ([]*ServiceInstance, error)

	// Watch watches for changes to a service
	Watch(ctx context.Context, serviceName string, onChange func([]*ServiceInstance)) error

	// ListServices lists all registered services
	ListServices(ctx context.Context) ([]string, error)

	// Health checks backend health
	Health(ctx context.Context) error

	// Close closes the backend
	Close() error
}

Backend defines the interface for service discovery backends.

type ConsulBackend

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

ConsulBackend implements service discovery using Consul.

func NewConsulBackend

func NewConsulBackend(config ConsulConfig) (*ConsulBackend, error)

NewConsulBackend creates a new Consul backend.

func (*ConsulBackend) Close

func (b *ConsulBackend) Close() error

Close closes the backend.

func (*ConsulBackend) Deregister

func (b *ConsulBackend) Deregister(ctx context.Context, serviceID string) error

Deregister deregisters a service instance from Consul.

func (*ConsulBackend) Discover

func (b *ConsulBackend) Discover(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

Discover discovers service instances by name from Consul.

func (*ConsulBackend) DiscoverWithTags

func (b *ConsulBackend) DiscoverWithTags(ctx context.Context, serviceName string, tags []string) ([]*ServiceInstance, error)

DiscoverWithTags discovers service instances by name and tags from Consul.

func (*ConsulBackend) GetServiceByID

func (b *ConsulBackend) GetServiceByID(ctx context.Context, serviceID string) (*ServiceInstance, error)

GetServiceByID retrieves a specific service instance by ID.

func (*ConsulBackend) Health

func (b *ConsulBackend) Health(ctx context.Context) error

Health checks backend health.

func (*ConsulBackend) Initialize

func (b *ConsulBackend) Initialize(ctx context.Context) error

Initialize initializes the backend.

func (*ConsulBackend) ListServices

func (b *ConsulBackend) ListServices(ctx context.Context) ([]string, error)

ListServices lists all registered services in Consul.

func (*ConsulBackend) Name

func (b *ConsulBackend) Name() string

Name returns the backend name.

func (*ConsulBackend) Register

func (b *ConsulBackend) Register(ctx context.Context, instance *ServiceInstance) error

Register registers a service instance with Consul.

func (*ConsulBackend) UpdateServiceHealth

func (b *ConsulBackend) UpdateServiceHealth(ctx context.Context, serviceID string, status HealthStatus) error

UpdateServiceHealth updates the health status of a service instance.

func (*ConsulBackend) Watch

func (b *ConsulBackend) Watch(ctx context.Context, serviceName string, onChange func([]*ServiceInstance)) error

Watch watches for changes to a service in Consul.

type ConsulConfig

type ConsulConfig struct {
	// Address is the Consul agent address
	Address string `json:"address" yaml:"address"`

	// Token is the Consul ACL token
	Token string `json:"token" yaml:"token"`

	// Datacenter is the Consul datacenter
	Datacenter string `json:"datacenter" yaml:"datacenter"`

	// Namespace is the Consul namespace (Enterprise)
	Namespace string `json:"namespace" yaml:"namespace"`

	// TLS settings
	TLSEnabled bool   `json:"tls_enabled" yaml:"tls_enabled"`
	TLSCAFile  string `json:"tls_ca_file" yaml:"tls_ca_file"`
	TLSCert    string `json:"tls_cert"    yaml:"tls_cert"`
	TLSKey     string `json:"tls_key"     yaml:"tls_key"`
}

ConsulConfig holds Consul-specific configuration.

type EtcdBackend

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

EtcdBackend implements service discovery using etcd.

func NewEtcdBackend

func NewEtcdBackend(config EtcdConfig) (*EtcdBackend, error)

NewEtcdBackend creates a new etcd backend.

func (*EtcdBackend) Close

func (b *EtcdBackend) Close() error

Close closes the backend.

func (*EtcdBackend) Deregister

func (b *EtcdBackend) Deregister(ctx context.Context, serviceID string) error

Deregister deregisters a service instance from etcd.

func (*EtcdBackend) Discover

func (b *EtcdBackend) Discover(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

Discover discovers service instances by name from etcd.

func (*EtcdBackend) DiscoverWithTags

func (b *EtcdBackend) DiscoverWithTags(ctx context.Context, serviceName string, tags []string) ([]*ServiceInstance, error)

DiscoverWithTags discovers service instances by name and tags from etcd.

func (*EtcdBackend) GetServiceByID

func (b *EtcdBackend) GetServiceByID(ctx context.Context, serviceID string) (*ServiceInstance, error)

GetServiceByID retrieves a specific service instance by ID.

func (*EtcdBackend) Health

func (b *EtcdBackend) Health(ctx context.Context) error

Health checks backend health.

func (*EtcdBackend) Initialize

func (b *EtcdBackend) Initialize(ctx context.Context) error

Initialize initializes the backend.

func (*EtcdBackend) ListServices

func (b *EtcdBackend) ListServices(ctx context.Context) ([]string, error)

ListServices lists all registered services in etcd.

func (*EtcdBackend) Name

func (b *EtcdBackend) Name() string

Name returns the backend name.

func (*EtcdBackend) Register

func (b *EtcdBackend) Register(ctx context.Context, instance *ServiceInstance) error

Register registers a service instance with etcd.

func (*EtcdBackend) UpdateServiceHealth

func (b *EtcdBackend) UpdateServiceHealth(ctx context.Context, serviceID string, status HealthStatus) error

UpdateServiceHealth updates the health status of a service instance.

func (*EtcdBackend) Watch

func (b *EtcdBackend) Watch(ctx context.Context, serviceName string, onChange func([]*ServiceInstance)) error

Watch watches for changes to a service in etcd.

type EtcdConfig

type EtcdConfig struct {
	// Endpoints are the etcd endpoints
	Endpoints []string `json:"endpoints" yaml:"endpoints"`

	// Username for etcd authentication
	Username string `json:"username" yaml:"username"`

	// Password for etcd authentication
	Password string `json:"password" yaml:"password"`

	// DialTimeout is the timeout for dialing etcd
	DialTimeout time.Duration `json:"dial_timeout" yaml:"dial_timeout"`

	// KeyPrefix is the etcd key prefix for services
	KeyPrefix string `json:"key_prefix" yaml:"key_prefix"`

	// TLS settings
	TLSEnabled bool   `json:"tls_enabled" yaml:"tls_enabled"`
	TLSCAFile  string `json:"tls_ca_file" yaml:"tls_ca_file"`
	TLSCert    string `json:"tls_cert"    yaml:"tls_cert"`
	TLSKey     string `json:"tls_key"     yaml:"tls_key"`
}

EtcdConfig holds etcd-specific configuration.

type EurekaConfig

type EurekaConfig struct {
	// URLs are the Eureka server URLs
	URLs []string `json:"urls" yaml:"urls"`

	// RegistryFetchInterval is how often to fetch the registry
	RegistryFetchInterval time.Duration `json:"registry_fetch_interval" yaml:"registry_fetch_interval"`

	// InstanceInfoReplicationInterval is how often to replicate instance info
	InstanceInfoReplicationInterval time.Duration `json:"instance_info_replication_interval" yaml:"instance_info_replication_interval"`
}

EurekaConfig holds Eureka-specific configuration.

type HealthStatus

type HealthStatus string

HealthStatus represents service health status.

const (
	// HealthStatusPassing indicates the service is healthy.
	HealthStatusPassing HealthStatus = "passing"

	// HealthStatusWarning indicates the service has warnings.
	HealthStatusWarning HealthStatus = "warning"

	// HealthStatusCritical indicates the service is unhealthy.
	HealthStatusCritical HealthStatus = "critical"

	// HealthStatusUnknown indicates the health status is unknown.
	HealthStatusUnknown HealthStatus = "unknown"
)

type KubernetesConfig

type KubernetesConfig struct {
	// Namespace is the Kubernetes namespace
	Namespace string `json:"namespace" yaml:"namespace"`

	// InCluster determines if running in-cluster
	InCluster bool `json:"in_cluster" yaml:"in_cluster"`

	// KubeconfigPath is the path to kubeconfig (if not in-cluster)
	KubeconfigPath string `json:"kubeconfig_path" yaml:"kubeconfig_path"`

	// ServiceType is the service type to watch (ClusterIP, NodePort, LoadBalancer)
	ServiceType string `json:"service_type" yaml:"service_type"`

	// LabelSelector is the label selector for filtering services
	LabelSelector string `json:"label_selector" yaml:"label_selector"`
}

KubernetesConfig holds Kubernetes-specific configuration.

type MDNSBackend

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

MDNSBackend implements service discovery using mDNS/DNS-SD This works natively on: - macOS (Bonjour) - Linux (Avahi) - Windows (DNS-SD).

func NewMDNSBackend

func NewMDNSBackend(config MDNSConfig) (*MDNSBackend, error)

NewMDNSBackend creates a new mDNS service discovery backend.

func (*MDNSBackend) Close

func (b *MDNSBackend) Close() error

Close closes the backend.

func (*MDNSBackend) Deregister

func (b *MDNSBackend) Deregister(ctx context.Context, serviceID string) error

Deregister deregisters a service instance.

func (*MDNSBackend) Discover

func (b *MDNSBackend) Discover(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

Discover discovers service instances by name via mDNS.

func (*MDNSBackend) DiscoverAllTypes added in v0.5.0

func (b *MDNSBackend) DiscoverAllTypes(ctx context.Context) ([]*ServiceInstance, error)

DiscoverAllTypes discovers services across all configured service types This is useful for gateways and service meshes that need to discover multiple service types.

func (*MDNSBackend) DiscoverWithTags

func (b *MDNSBackend) DiscoverWithTags(ctx context.Context, serviceName string, tags []string) ([]*ServiceInstance, error)

DiscoverWithTags discovers service instances by name and tags.

func (*MDNSBackend) Health

func (b *MDNSBackend) Health(ctx context.Context) error

Health checks backend health.

func (*MDNSBackend) Initialize

func (b *MDNSBackend) Initialize(ctx context.Context) error

Initialize initializes the backend.

func (*MDNSBackend) ListServices

func (b *MDNSBackend) ListServices(ctx context.Context) ([]string, error)

ListServices lists all registered services (local only).

func (*MDNSBackend) Name

func (b *MDNSBackend) Name() string

Name returns the backend name.

func (*MDNSBackend) Register

func (b *MDNSBackend) Register(ctx context.Context, instance *ServiceInstance) error

Register registers a service instance via mDNS.

func (*MDNSBackend) Watch

func (b *MDNSBackend) Watch(ctx context.Context, serviceName string, onChange func([]*ServiceInstance)) error

Watch watches for changes to a service via mDNS.

type MDNSConfig

type MDNSConfig struct {
	// Domain is the mDNS domain (default: "local.")
	Domain string `json:"domain" yaml:"domain"`

	// ServiceType is the mDNS service type for registration (e.g., "_octopus._tcp", "_farp._tcp")
	// If empty, defaults to "_{service-name}._tcp"
	// This is used when registering a single service
	ServiceType string `json:"service_type" yaml:"service_type"`

	// ServiceTypes is a list of service types to discover (gateway/mesh use case)
	// Example: ["_octopus._tcp", "_farp._tcp", "_http._tcp"]
	// Used for discovering multiple service types simultaneously
	ServiceTypes []string `json:"service_types" yaml:"service_types"`

	// WatchInterval is how often to poll for service changes (default: 30s)
	WatchInterval time.Duration `json:"watch_interval" yaml:"watch_interval"`

	// Interface is the network interface to use (empty for all interfaces)
	Interface string `json:"interface" yaml:"interface"`

	// IPv6 enables IPv6 support
	IPv6 bool `json:"ipv6" yaml:"ipv6"`

	// BrowseTimeout is the timeout for browsing services (default: 3s)
	BrowseTimeout time.Duration `json:"browse_timeout" yaml:"browse_timeout"`

	// TTL is the time-to-live for service records (default: 120 seconds)
	TTL uint32 `json:"ttl" yaml:"ttl"`
}

MDNSConfig holds mDNS/DNS-SD-specific configuration.

type MemoryBackend

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

MemoryBackend is an in-memory service discovery backend This is useful for local development and testing.

func NewMemoryBackend

func NewMemoryBackend() (*MemoryBackend, error)

NewMemoryBackend creates a new memory backend.

func (*MemoryBackend) Close

func (b *MemoryBackend) Close() error

Close closes the backend.

func (*MemoryBackend) Deregister

func (b *MemoryBackend) Deregister(ctx context.Context, serviceID string) error

Deregister deregisters a service instance.

func (*MemoryBackend) Discover

func (b *MemoryBackend) Discover(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

Discover discovers service instances by name.

func (*MemoryBackend) DiscoverWithTags

func (b *MemoryBackend) DiscoverWithTags(ctx context.Context, serviceName string, tags []string) ([]*ServiceInstance, error)

DiscoverWithTags discovers service instances by name and tags.

func (*MemoryBackend) GetInstanceCount

func (b *MemoryBackend) GetInstanceCount(serviceName string) int

GetInstanceCount returns the number of instances for a service.

func (*MemoryBackend) GetTotalServiceCount

func (b *MemoryBackend) GetTotalServiceCount() int

GetTotalServiceCount returns the total number of registered services.

func (*MemoryBackend) Health

func (b *MemoryBackend) Health(ctx context.Context) error

Health checks backend health.

func (*MemoryBackend) Initialize

func (b *MemoryBackend) Initialize(ctx context.Context) error

Initialize initializes the backend.

func (*MemoryBackend) ListServices

func (b *MemoryBackend) ListServices(ctx context.Context) ([]string, error)

ListServices lists all registered services.

func (*MemoryBackend) Name

func (b *MemoryBackend) Name() string

Name returns the backend name.

func (*MemoryBackend) Register

func (b *MemoryBackend) Register(ctx context.Context, instance *ServiceInstance) error

Register registers a service instance.

func (*MemoryBackend) Watch

func (b *MemoryBackend) Watch(ctx context.Context, serviceName string, onChange func([]*ServiceInstance)) error

Watch watches for changes to a service.

type ServiceInstance

type ServiceInstance struct {
	// ID is the unique service instance ID
	ID string `json:"id"`

	// Name is the service name
	Name string `json:"name"`

	// Version is the service version
	Version string `json:"version"`

	// Address is the service address (IP or hostname)
	Address string `json:"address"`

	// Port is the service port
	Port int `json:"port"`

	// Tags are service tags for filtering
	Tags []string `json:"tags"`

	// Metadata is arbitrary service metadata
	Metadata map[string]string `json:"metadata"`

	// Status is the service health status
	Status HealthStatus `json:"status"`

	// LastHeartbeat is the timestamp of the last heartbeat
	LastHeartbeat int64 `json:"last_heartbeat"`
}

ServiceInstance represents a registered service instance.

func (*ServiceInstance) GetMetadata

func (si *ServiceInstance) GetMetadata(key string) (string, bool)

GetMetadata retrieves metadata by key.

func (*ServiceInstance) HasAllTags

func (si *ServiceInstance) HasAllTags(tags []string) bool

HasAllTags checks if the service has all specified tags.

func (*ServiceInstance) HasTag

func (si *ServiceInstance) HasTag(tag string) bool

HasTag checks if the service has a specific tag.

func (*ServiceInstance) IsHealthy

func (si *ServiceInstance) IsHealthy() bool

IsHealthy checks if the service is healthy.

func (*ServiceInstance) URL

func (si *ServiceInstance) URL(scheme string) string

URL returns the full URL for the service instance.

Jump to

Keyboard shortcuts

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